1 | /**************************************************************************/ |
2 | /* godot_area_pair_3d.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_3d.h" |
32 | |
33 | #include "godot_collision_solver_3d.h" |
34 | |
35 | bool GodotAreaPair3D::setup(real_t p_step) { |
36 | bool result = false; |
37 | if (area->collides_with(body) && GodotCollisionSolver3D::solve_static(body->get_shape(body_shape), body->get_transform() * body->get_shape_transform(body_shape), area->get_shape(area_shape), area->get_transform() * area->get_shape_transform(area_shape), nullptr, this)) { |
38 | result = true; |
39 | } |
40 | |
41 | process_collision = false; |
42 | has_space_override = false; |
43 | if (result != colliding) { |
44 | if ((int)area->get_param(PhysicsServer3D::AREA_PARAM_GRAVITY_OVERRIDE_MODE) != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) { |
45 | has_space_override = true; |
46 | } else if ((int)area->get_param(PhysicsServer3D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE) != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) { |
47 | has_space_override = true; |
48 | } else if ((int)area->get_param(PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE) != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) { |
49 | has_space_override = true; |
50 | } |
51 | process_collision = has_space_override; |
52 | |
53 | if (area->has_monitor_callback()) { |
54 | process_collision = true; |
55 | } |
56 | |
57 | colliding = result; |
58 | } |
59 | |
60 | return process_collision; |
61 | } |
62 | |
63 | bool GodotAreaPair3D::pre_solve(real_t p_step) { |
64 | if (!process_collision) { |
65 | return false; |
66 | } |
67 | |
68 | if (colliding) { |
69 | if (has_space_override) { |
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->remove_area(area); |
79 | } |
80 | |
81 | if (area->has_monitor_callback()) { |
82 | area->remove_body_from_query(body, body_shape, area_shape); |
83 | } |
84 | } |
85 | |
86 | return false; // Never do any post solving. |
87 | } |
88 | |
89 | void GodotAreaPair3D::solve(real_t p_step) { |
90 | // Nothing to do. |
91 | } |
92 | |
93 | GodotAreaPair3D::GodotAreaPair3D(GodotBody3D *p_body, int p_body_shape, GodotArea3D *p_area, int p_area_shape) { |
94 | body = p_body; |
95 | area = p_area; |
96 | body_shape = p_body_shape; |
97 | area_shape = p_area_shape; |
98 | body->add_constraint(this, 0); |
99 | area->add_constraint(this); |
100 | if (p_body->get_mode() == PhysicsServer3D::BODY_MODE_KINEMATIC) { |
101 | p_body->set_active(true); |
102 | } |
103 | } |
104 | |
105 | GodotAreaPair3D::~GodotAreaPair3D() { |
106 | if (colliding) { |
107 | if (has_space_override) { |
108 | body->remove_area(area); |
109 | } |
110 | if (area->has_monitor_callback()) { |
111 | area->remove_body_from_query(body, body_shape, area_shape); |
112 | } |
113 | } |
114 | body->remove_constraint(this); |
115 | area->remove_constraint(this); |
116 | } |
117 | |
118 | //////////////////////////////////////////////////// |
119 | |
120 | bool GodotArea2Pair3D::setup(real_t p_step) { |
121 | bool result_a = area_a->collides_with(area_b); |
122 | bool result_b = area_b->collides_with(area_a); |
123 | if ((result_a || result_b) && !GodotCollisionSolver3D::solve_static(area_a->get_shape(shape_a), area_a->get_transform() * area_a->get_shape_transform(shape_a), area_b->get_shape(shape_b), area_b->get_transform() * area_b->get_shape_transform(shape_b), nullptr, this)) { |
124 | result_a = false; |
125 | result_b = false; |
126 | } |
127 | |
128 | bool process_collision = false; |
129 | |
130 | process_collision_a = false; |
131 | if (result_a != colliding_a) { |
132 | if (area_a->has_area_monitor_callback() && area_b_monitorable) { |
133 | process_collision_a = true; |
134 | process_collision = true; |
135 | } |
136 | colliding_a = result_a; |
137 | } |
138 | |
139 | process_collision_b = false; |
140 | if (result_b != colliding_b) { |
141 | if (area_b->has_area_monitor_callback() && area_a_monitorable) { |
142 | process_collision_b = true; |
143 | process_collision = true; |
144 | } |
145 | colliding_b = result_b; |
146 | } |
147 | |
148 | return process_collision; |
149 | } |
150 | |
151 | bool GodotArea2Pair3D::pre_solve(real_t p_step) { |
152 | if (process_collision_a) { |
153 | if (colliding_a) { |
154 | area_a->add_area_to_query(area_b, shape_b, shape_a); |
155 | } else { |
156 | area_a->remove_area_from_query(area_b, shape_b, shape_a); |
157 | } |
158 | } |
159 | |
160 | if (process_collision_b) { |
161 | if (colliding_b) { |
162 | area_b->add_area_to_query(area_a, shape_a, shape_b); |
163 | } else { |
164 | area_b->remove_area_from_query(area_a, shape_a, shape_b); |
165 | } |
166 | } |
167 | |
168 | return false; // Never do any post solving. |
169 | } |
170 | |
171 | void GodotArea2Pair3D::solve(real_t p_step) { |
172 | // Nothing to do. |
173 | } |
174 | |
175 | GodotArea2Pair3D::GodotArea2Pair3D(GodotArea3D *p_area_a, int p_shape_a, GodotArea3D *p_area_b, int p_shape_b) { |
176 | area_a = p_area_a; |
177 | area_b = p_area_b; |
178 | shape_a = p_shape_a; |
179 | shape_b = p_shape_b; |
180 | area_a_monitorable = area_a->is_monitorable(); |
181 | area_b_monitorable = area_b->is_monitorable(); |
182 | area_a->add_constraint(this); |
183 | area_b->add_constraint(this); |
184 | } |
185 | |
186 | GodotArea2Pair3D::~GodotArea2Pair3D() { |
187 | if (colliding_a) { |
188 | if (area_a->has_area_monitor_callback() && area_b_monitorable) { |
189 | area_a->remove_area_from_query(area_b, shape_b, shape_a); |
190 | } |
191 | } |
192 | |
193 | if (colliding_b) { |
194 | if (area_b->has_area_monitor_callback() && area_a_monitorable) { |
195 | area_b->remove_area_from_query(area_a, shape_a, shape_b); |
196 | } |
197 | } |
198 | |
199 | area_a->remove_constraint(this); |
200 | area_b->remove_constraint(this); |
201 | } |
202 | |
203 | //////////////////////////////////////////////////// |
204 | |
205 | bool GodotAreaSoftBodyPair3D::setup(real_t p_step) { |
206 | bool result = false; |
207 | if ( |
208 | area->collides_with(soft_body) && |
209 | GodotCollisionSolver3D::solve_static( |
210 | soft_body->get_shape(soft_body_shape), |
211 | soft_body->get_transform() * soft_body->get_shape_transform(soft_body_shape), |
212 | area->get_shape(area_shape), |
213 | area->get_transform() * area->get_shape_transform(area_shape), |
214 | nullptr, |
215 | this)) { |
216 | result = true; |
217 | } |
218 | |
219 | process_collision = false; |
220 | has_space_override = false; |
221 | if (result != colliding) { |
222 | if ((int)area->get_param(PhysicsServer3D::AREA_PARAM_GRAVITY_OVERRIDE_MODE) != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) { |
223 | has_space_override = true; |
224 | } else if (area->get_wind_force_magnitude() > CMP_EPSILON) { |
225 | has_space_override = true; |
226 | } |
227 | |
228 | if (area->has_monitor_callback()) { |
229 | process_collision = true; |
230 | } |
231 | |
232 | colliding = result; |
233 | } |
234 | |
235 | return process_collision; |
236 | } |
237 | |
238 | bool GodotAreaSoftBodyPair3D::pre_solve(real_t p_step) { |
239 | if (!process_collision) { |
240 | return false; |
241 | } |
242 | |
243 | if (colliding) { |
244 | if (has_space_override) { |
245 | soft_body->add_area(area); |
246 | } |
247 | |
248 | if (area->has_monitor_callback()) { |
249 | area->add_soft_body_to_query(soft_body, soft_body_shape, area_shape); |
250 | } |
251 | } else { |
252 | if (has_space_override) { |
253 | soft_body->remove_area(area); |
254 | } |
255 | |
256 | if (area->has_monitor_callback()) { |
257 | area->remove_soft_body_from_query(soft_body, soft_body_shape, area_shape); |
258 | } |
259 | } |
260 | |
261 | return false; // Never do any post solving. |
262 | } |
263 | |
264 | void GodotAreaSoftBodyPair3D::solve(real_t p_step) { |
265 | // Nothing to do. |
266 | } |
267 | |
268 | GodotAreaSoftBodyPair3D::GodotAreaSoftBodyPair3D(GodotSoftBody3D *p_soft_body, int p_soft_body_shape, GodotArea3D *p_area, int p_area_shape) { |
269 | soft_body = p_soft_body; |
270 | area = p_area; |
271 | soft_body_shape = p_soft_body_shape; |
272 | area_shape = p_area_shape; |
273 | soft_body->add_constraint(this); |
274 | area->add_constraint(this); |
275 | } |
276 | |
277 | GodotAreaSoftBodyPair3D::~GodotAreaSoftBodyPair3D() { |
278 | if (colliding) { |
279 | if (has_space_override) { |
280 | soft_body->remove_area(area); |
281 | } |
282 | if (area->has_monitor_callback()) { |
283 | area->remove_soft_body_from_query(soft_body, soft_body_shape, area_shape); |
284 | } |
285 | } |
286 | soft_body->remove_constraint(this); |
287 | area->remove_constraint(this); |
288 | } |
289 | |