Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/godot_physics_2d/godot_area_pair_2d.cpp
10277 views
1
/**************************************************************************/
2
/* godot_area_pair_2d.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "godot_area_pair_2d.h"
32
#include "godot_collision_solver_2d.h"
33
34
bool GodotAreaPair2D::setup(real_t p_step) {
35
bool result = false;
36
if (area->collides_with(body) && GodotCollisionSolver2D::solve(body->get_shape(body_shape), body->get_transform() * body->get_shape_transform(body_shape), Vector2(), area->get_shape(area_shape), area->get_transform() * area->get_shape_transform(area_shape), Vector2(), nullptr, this)) {
37
result = true;
38
}
39
40
process_collision = false;
41
has_space_override = false;
42
if (result != colliding) {
43
if ((int)area->get_param(PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE) != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
44
has_space_override = true;
45
} else if ((int)area->get_param(PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE) != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
46
has_space_override = true;
47
} else if ((int)area->get_param(PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE) != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
48
has_space_override = true;
49
}
50
process_collision = has_space_override;
51
52
if (area->has_monitor_callback()) {
53
process_collision = true;
54
}
55
56
colliding = result;
57
}
58
59
return process_collision;
60
}
61
62
bool GodotAreaPair2D::pre_solve(real_t p_step) {
63
if (!process_collision) {
64
return false;
65
}
66
67
if (colliding) {
68
if (has_space_override) {
69
body_has_attached_area = true;
70
body->add_area(area);
71
}
72
73
if (area->has_monitor_callback()) {
74
area->add_body_to_query(body, body_shape, area_shape);
75
}
76
} else {
77
if (has_space_override) {
78
body_has_attached_area = false;
79
body->remove_area(area);
80
}
81
82
if (area->has_monitor_callback()) {
83
area->remove_body_from_query(body, body_shape, area_shape);
84
}
85
}
86
87
return false; // Never do any post solving.
88
}
89
90
void GodotAreaPair2D::solve(real_t p_step) {
91
// Nothing to do.
92
}
93
94
GodotAreaPair2D::GodotAreaPair2D(GodotBody2D *p_body, int p_body_shape, GodotArea2D *p_area, int p_area_shape) {
95
body = p_body;
96
area = p_area;
97
body_shape = p_body_shape;
98
area_shape = p_area_shape;
99
body->add_constraint(this, 0);
100
area->add_constraint(this);
101
if (p_body->get_mode() == PhysicsServer2D::BODY_MODE_KINEMATIC) { //need to be active to process pair
102
p_body->set_active(true);
103
}
104
}
105
106
GodotAreaPair2D::~GodotAreaPair2D() {
107
if (colliding) {
108
if (body_has_attached_area) {
109
body_has_attached_area = false;
110
body->remove_area(area);
111
}
112
if (area->has_monitor_callback()) {
113
area->remove_body_from_query(body, body_shape, area_shape);
114
}
115
}
116
body->remove_constraint(this, 0);
117
area->remove_constraint(this);
118
}
119
120
//////////////////////////////////
121
122
bool GodotArea2Pair2D::setup(real_t p_step) {
123
bool result_a = area_a->collides_with(area_b);
124
bool result_b = area_b->collides_with(area_a);
125
if ((result_a || result_b) && !GodotCollisionSolver2D::solve(area_a->get_shape(shape_a), area_a->get_transform() * area_a->get_shape_transform(shape_a), Vector2(), area_b->get_shape(shape_b), area_b->get_transform() * area_b->get_shape_transform(shape_b), Vector2(), nullptr, this)) {
126
result_a = false;
127
result_b = false;
128
}
129
130
bool process_collision = false;
131
132
process_collision_a = false;
133
if (result_a != colliding_a) {
134
if (area_a->has_area_monitor_callback() && area_b_monitorable) {
135
process_collision_a = true;
136
process_collision = true;
137
}
138
colliding_a = result_a;
139
}
140
141
process_collision_b = false;
142
if (result_b != colliding_b) {
143
if (area_b->has_area_monitor_callback() && area_a_monitorable) {
144
process_collision_b = true;
145
process_collision = true;
146
}
147
colliding_b = result_b;
148
}
149
150
return process_collision;
151
}
152
153
bool GodotArea2Pair2D::pre_solve(real_t p_step) {
154
if (process_collision_a) {
155
if (colliding_a) {
156
area_a->add_area_to_query(area_b, shape_b, shape_a);
157
} else {
158
area_a->remove_area_from_query(area_b, shape_b, shape_a);
159
}
160
}
161
162
if (process_collision_b) {
163
if (colliding_b) {
164
area_b->add_area_to_query(area_a, shape_a, shape_b);
165
} else {
166
area_b->remove_area_from_query(area_a, shape_a, shape_b);
167
}
168
}
169
170
return false; // Never do any post solving.
171
}
172
173
void GodotArea2Pair2D::solve(real_t p_step) {
174
// Nothing to do.
175
}
176
177
GodotArea2Pair2D::GodotArea2Pair2D(GodotArea2D *p_area_a, int p_shape_a, GodotArea2D *p_area_b, int p_shape_b) {
178
area_a = p_area_a;
179
area_b = p_area_b;
180
shape_a = p_shape_a;
181
shape_b = p_shape_b;
182
area_a_monitorable = area_a->is_monitorable();
183
area_b_monitorable = area_b->is_monitorable();
184
area_a->add_constraint(this);
185
area_b->add_constraint(this);
186
}
187
188
GodotArea2Pair2D::~GodotArea2Pair2D() {
189
if (colliding_a) {
190
if (area_a->has_area_monitor_callback() && area_b_monitorable) {
191
area_a->remove_area_from_query(area_b, shape_b, shape_a);
192
}
193
}
194
195
if (colliding_b) {
196
if (area_b->has_area_monitor_callback() && area_a_monitorable) {
197
area_b->remove_area_from_query(area_a, shape_a, shape_b);
198
}
199
}
200
201
area_a->remove_constraint(this);
202
area_b->remove_constraint(this);
203
}
204
205