Debian Codegraph @

modules/godot_physics_3d/godot_physics_server_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* godot_physics_server_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_physics_server_3d.h"
32
33 #include "godot_body_direct_state_3d.h"
34 #include "godot_broad_phase_3d_bvh.h"
35 #include "joints/godot_cone_twist_joint_3d.h"
36 #include "joints/godot_generic_6dof_joint_3d.h"
37 #include "joints/godot_hinge_joint_3d.h"
38 #include "joints/godot_pin_joint_3d.h"
39 #include "joints/godot_slider_joint_3d.h"
40
41 #include "core/debugger/engine_debugger.h"
42 #include "core/os/os.h"
43
44 #define FLUSH_QUERY_CHECK(m_object) \
45 ERR_FAIL_COND_MSG(m_object->get_space() && flushing_queries, "Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.");
46
47 RID GodotPhysicsServer3D::world_boundary_shape_create() {
48 GodotShape3D *shape = memnew(GodotWorldBoundaryShape3D);
49 RID rid = shape_owner.make_rid(shape);
50 shape->set_self(rid);
51 return rid;
52 }
53 RID GodotPhysicsServer3D::separation_ray_shape_create() {
54 GodotShape3D *shape = memnew(GodotSeparationRayShape3D);
55 RID rid = shape_owner.make_rid(shape);
56 shape->set_self(rid);
57 return rid;
58 }
59 RID GodotPhysicsServer3D::sphere_shape_create() {
60 GodotShape3D *shape = memnew(GodotSphereShape3D);
61 RID rid = shape_owner.make_rid(shape);
62 shape->set_self(rid);
63 return rid;
64 }
65 RID GodotPhysicsServer3D::box_shape_create() {
66 GodotShape3D *shape = memnew(GodotBoxShape3D);
67 RID rid = shape_owner.make_rid(shape);
68 shape->set_self(rid);
69 return rid;
70 }
71 RID GodotPhysicsServer3D::capsule_shape_create() {
72 GodotShape3D *shape = memnew(GodotCapsuleShape3D);
73 RID rid = shape_owner.make_rid(shape);
74 shape->set_self(rid);
75 return rid;
76 }
77 RID GodotPhysicsServer3D::cylinder_shape_create() {
78 GodotShape3D *shape = memnew(GodotCylinderShape3D);
79 RID rid = shape_owner.make_rid(shape);
80 shape->set_self(rid);
81 return rid;
82 }
83 RID GodotPhysicsServer3D::convex_polygon_shape_create() {
84 GodotShape3D *shape = memnew(GodotConvexPolygonShape3D);
85 RID rid = shape_owner.make_rid(shape);
86 shape->set_self(rid);
87 return rid;
88 }
89 RID GodotPhysicsServer3D::concave_polygon_shape_create() {
90 GodotShape3D *shape = memnew(GodotConcavePolygonShape3D);
91 RID rid = shape_owner.make_rid(shape);
92 shape->set_self(rid);
93 return rid;
94 }
95 RID GodotPhysicsServer3D::heightmap_shape_create() {
96 GodotShape3D *shape = memnew(GodotHeightMapShape3D);
97 RID rid = shape_owner.make_rid(shape);
98 shape->set_self(rid);
99 return rid;
100 }
101 RID GodotPhysicsServer3D::custom_shape_create() {
102 ERR_FAIL_V(RID());
103 }
104
105 void GodotPhysicsServer3D::shape_set_data(RID p_shape, const Variant &p_data) {
106 GodotShape3D *shape = shape_owner.get_or_null(p_shape);
107 ERR_FAIL_NULL(shape);
108 shape->set_data(p_data);
109 }
110
111 void GodotPhysicsServer3D::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
112 GodotShape3D *shape = shape_owner.get_or_null(p_shape);
113 ERR_FAIL_NULL(shape);
114 shape->set_custom_bias(p_bias);
115 }
116
117 PhysicsServer3D::ShapeType GodotPhysicsServer3D::shape_get_type(RID p_shape) const {
118 const GodotShape3D *shape = shape_owner.get_or_null(p_shape);
119 ERR_FAIL_NULL_V(shape, SHAPE_CUSTOM);
120 return shape->get_type();
121 }
122
123 Variant GodotPhysicsServer3D::shape_get_data(RID p_shape) const {
124 const GodotShape3D *shape = shape_owner.get_or_null(p_shape);
125 ERR_FAIL_NULL_V(shape, Variant());
126 ERR_FAIL_COND_V(!shape->is_configured(), Variant());
127 return shape->get_data();
128 }
129
130 void GodotPhysicsServer3D::shape_set_margin(RID p_shape, real_t p_margin) {
131 }
132
133 real_t GodotPhysicsServer3D::shape_get_margin(RID p_shape) const {
134 return 0.0;
135 }
136
137 real_t GodotPhysicsServer3D::shape_get_custom_solver_bias(RID p_shape) const {
138 const GodotShape3D *shape = shape_owner.get_or_null(p_shape);
139 ERR_FAIL_NULL_V(shape, 0);
140 return shape->get_custom_bias();
141 }
142
143 RID GodotPhysicsServer3D::space_create() {
144 GodotSpace3D *space = memnew(GodotSpace3D);
145 RID id = space_owner.make_rid(space);
146 space->set_self(id);
147 RID area_id = area_create();
148 GodotArea3D *area = area_owner.get_or_null(area_id);
149 ERR_FAIL_NULL_V(area, RID());
150 space->set_default_area(area);
151 area->set_space(space);
152 area->set_priority(-1);
153 RID sgb = body_create();
154 body_set_space(sgb, id);
155 body_set_mode(sgb, BODY_MODE_STATIC);
156 space->set_static_global_body(sgb);
157
158 return id;
159 }
160
161 void GodotPhysicsServer3D::space_set_active(RID p_space, bool p_active) {
162 GodotSpace3D *space = space_owner.get_or_null(p_space);
163 ERR_FAIL_NULL(space);
164 if (p_active) {
165 active_spaces.insert(space);
166 } else {
167 active_spaces.erase(space);
168 }
169 }
170
171 bool GodotPhysicsServer3D::space_is_active(RID p_space) const {
172 GodotSpace3D *space = space_owner.get_or_null(p_space);
173 ERR_FAIL_NULL_V(space, false);
174
175 return active_spaces.has(space);
176 }
177
178 void GodotPhysicsServer3D::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) {
179 GodotSpace3D *space = space_owner.get_or_null(p_space);
180 ERR_FAIL_NULL(space);
181
182 space->set_param(p_param, p_value);
183 }
184
185 real_t GodotPhysicsServer3D::space_get_param(RID p_space, SpaceParameter p_param) const {
186 const GodotSpace3D *space = space_owner.get_or_null(p_space);
187 ERR_FAIL_NULL_V(space, 0);
188 return space->get_param(p_param);
189 }
190
191 PhysicsDirectSpaceState3D *GodotPhysicsServer3D::space_get_direct_state(RID p_space) {
192 GodotSpace3D *space = space_owner.get_or_null(p_space);
193 ERR_FAIL_NULL_V(space, nullptr);
194 ERR_FAIL_COND_V_MSG((using_threads && !doing_sync) || space->is_locked(), nullptr, "Space state is inaccessible right now, wait for iteration or physics process notification.");
195
196 return space->get_direct_state();
197 }
198
199 void GodotPhysicsServer3D::space_set_debug_contacts(RID p_space, int p_max_contacts) {
200 GodotSpace3D *space = space_owner.get_or_null(p_space);
201 ERR_FAIL_NULL(space);
202 space->set_debug_contacts(p_max_contacts);
203 }
204
205 Vector<Vector3> GodotPhysicsServer3D::space_get_contacts(RID p_space) const {
206 GodotSpace3D *space = space_owner.get_or_null(p_space);
207 ERR_FAIL_NULL_V(space, Vector<Vector3>());
208 return space->get_debug_contacts();
209 }
210
211 int GodotPhysicsServer3D::space_get_contact_count(RID p_space) const {
212 GodotSpace3D *space = space_owner.get_or_null(p_space);
213 ERR_FAIL_NULL_V(space, 0);
214 return space->get_debug_contact_count();
215 }
216
217 RID GodotPhysicsServer3D::area_create() {
218 GodotArea3D *area = memnew(GodotArea3D);
219 RID rid = area_owner.make_rid(area);
220 area->set_self(rid);
221 return rid;
222 }
223
224 void GodotPhysicsServer3D::area_set_space(RID p_area, RID p_space) {
225 GodotArea3D *area = area_owner.get_or_null(p_area);
226 ERR_FAIL_NULL(area);
227
228 GodotSpace3D *space = nullptr;
229 if (p_space.is_valid()) {
230 space = space_owner.get_or_null(p_space);
231 ERR_FAIL_NULL(space);
232 }
233
234 if (area->get_space() == space) {
235 return; //pointless
236 }
237
238 area->clear_constraints();
239 area->set_space(space);
240 }
241
242 RID GodotPhysicsServer3D::area_get_space(RID p_area) const {
243 GodotArea3D *area = area_owner.get_or_null(p_area);
244 ERR_FAIL_NULL_V(area, RID());
245
246 GodotSpace3D *space = area->get_space();
247 if (!space) {
248 return RID();
249 }
250 return space->get_self();
251 }
252
253 void GodotPhysicsServer3D::area_add_shape(RID p_area, RID p_shape, const Transform3D &p_transform, bool p_disabled) {
254 GodotArea3D *area = area_owner.get_or_null(p_area);
255 ERR_FAIL_NULL(area);
256
257 GodotShape3D *shape = shape_owner.get_or_null(p_shape);
258 ERR_FAIL_NULL(shape);
259
260 area->add_shape(shape, p_transform, p_disabled);
261 }
262
263 void GodotPhysicsServer3D::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
264 GodotArea3D *area = area_owner.get_or_null(p_area);
265 ERR_FAIL_NULL(area);
266
267 GodotShape3D *shape = shape_owner.get_or_null(p_shape);
268 ERR_FAIL_NULL(shape);
269 ERR_FAIL_COND(!shape->is_configured());
270
271 area->set_shape(p_shape_idx, shape);
272 }
273
274 void GodotPhysicsServer3D::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform3D &p_transform) {
275 GodotArea3D *area = area_owner.get_or_null(p_area);
276 ERR_FAIL_NULL(area);
277
278 area->set_shape_transform(p_shape_idx, p_transform);
279 }
280
281 int GodotPhysicsServer3D::area_get_shape_count(RID p_area) const {
282 GodotArea3D *area = area_owner.get_or_null(p_area);
283 ERR_FAIL_NULL_V(area, -1);
284
285 return area->get_shape_count();
286 }
287
288 RID GodotPhysicsServer3D::area_get_shape(RID p_area, int p_shape_idx) const {
289 GodotArea3D *area = area_owner.get_or_null(p_area);
290 ERR_FAIL_NULL_V(area, RID());
291
292 GodotShape3D *shape = area->get_shape(p_shape_idx);
293 ERR_FAIL_NULL_V(shape, RID());
294
295 return shape->get_self();
296 }
297
298 Transform3D GodotPhysicsServer3D::area_get_shape_transform(RID p_area, int p_shape_idx) const {
299 GodotArea3D *area = area_owner.get_or_null(p_area);
300 ERR_FAIL_NULL_V(area, Transform3D());
301
302 return area->get_shape_transform(p_shape_idx);
303 }
304
305 void GodotPhysicsServer3D::area_remove_shape(RID p_area, int p_shape_idx) {
306 GodotArea3D *area = area_owner.get_or_null(p_area);
307 ERR_FAIL_NULL(area);
308
309 area->remove_shape(p_shape_idx);
310 }
311
312 void GodotPhysicsServer3D::area_clear_shapes(RID p_area) {
313 GodotArea3D *area = area_owner.get_or_null(p_area);
314 ERR_FAIL_NULL(area);
315
316 while (area->get_shape_count()) {
317 area->remove_shape(0);
318 }
319 }
320
321 void GodotPhysicsServer3D::area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) {
322 GodotArea3D *area = area_owner.get_or_null(p_area);
323 ERR_FAIL_NULL(area);
324 ERR_FAIL_INDEX(p_shape_idx, area->get_shape_count());
325 FLUSH_QUERY_CHECK(area);
326 area->set_shape_disabled(p_shape_idx, p_disabled);
327 }
328
329 void GodotPhysicsServer3D::area_attach_object_instance_id(RID p_area, ObjectID p_id) {
330 if (space_owner.owns(p_area)) {
331 GodotSpace3D *space = space_owner.get_or_null(p_area);
332 p_area = space->get_default_area()->get_self();
333 }
334 GodotArea3D *area = area_owner.get_or_null(p_area);
335 ERR_FAIL_NULL(area);
336 area->set_instance_id(p_id);
337 }
338
339 ObjectID GodotPhysicsServer3D::area_get_object_instance_id(RID p_area) const {
340 if (space_owner.owns(p_area)) {
341 GodotSpace3D *space = space_owner.get_or_null(p_area);
342 p_area = space->get_default_area()->get_self();
343 }
344 GodotArea3D *area = area_owner.get_or_null(p_area);
345 ERR_FAIL_NULL_V(area, ObjectID());
346 return area->get_instance_id();
347 }
348
349 void GodotPhysicsServer3D::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
350 if (space_owner.owns(p_area)) {
351 GodotSpace3D *space = space_owner.get_or_null(p_area);
352 p_area = space->get_default_area()->get_self();
353 }
354 GodotArea3D *area = area_owner.get_or_null(p_area);
355 ERR_FAIL_NULL(area);
356 area->set_param(p_param, p_value);
357 }
358
359 void GodotPhysicsServer3D::area_set_transform(RID p_area, const Transform3D &p_transform) {
360 GodotArea3D *area = area_owner.get_or_null(p_area);
361 ERR_FAIL_NULL(area);
362 area->set_transform(p_transform);
363 }
364
365 Variant GodotPhysicsServer3D::area_get_param(RID p_area, AreaParameter p_param) const {
366 if (space_owner.owns(p_area)) {
367 GodotSpace3D *space = space_owner.get_or_null(p_area);
368 p_area = space->get_default_area()->get_self();
369 }
370 GodotArea3D *area = area_owner.get_or_null(p_area);
371 ERR_FAIL_NULL_V(area, Variant());
372
373 return area->get_param(p_param);
374 }
375
376 Transform3D GodotPhysicsServer3D::area_get_transform(RID p_area) const {
377 GodotArea3D *area = area_owner.get_or_null(p_area);
378 ERR_FAIL_NULL_V(area, Transform3D());
379
380 return area->get_transform();
381 }
382
383 void GodotPhysicsServer3D::area_set_collision_layer(RID p_area, uint32_t p_layer) {
384 GodotArea3D *area = area_owner.get_or_null(p_area);
385 ERR_FAIL_NULL(area);
386
387 area->set_collision_layer(p_layer);
388 }
389
390 uint32_t GodotPhysicsServer3D::area_get_collision_layer(RID p_area) const {
391 GodotArea3D *area = area_owner.get_or_null(p_area);
392 ERR_FAIL_NULL_V(area, 0);
393
394 return area->get_collision_layer();
395 }
396
397 void GodotPhysicsServer3D::area_set_collision_mask(RID p_area, uint32_t p_mask) {
398 GodotArea3D *area = area_owner.get_or_null(p_area);
399 ERR_FAIL_NULL(area);
400
401 area->set_collision_mask(p_mask);
402 }
403
404 uint32_t GodotPhysicsServer3D::area_get_collision_mask(RID p_area) const {
405 GodotArea3D *area = area_owner.get_or_null(p_area);
406 ERR_FAIL_NULL_V(area, 0);
407
408 return area->get_collision_mask();
409 }
410
411 void GodotPhysicsServer3D::area_set_monitorable(RID p_area, bool p_monitorable) {
412 GodotArea3D *area = area_owner.get_or_null(p_area);
413 ERR_FAIL_NULL(area);
414 FLUSH_QUERY_CHECK(area);
415
416 area->set_monitorable(p_monitorable);
417 }
418
419 void GodotPhysicsServer3D::area_set_monitor_callback(RID p_area, const Callable &p_callback) {
420 GodotArea3D *area = area_owner.get_or_null(p_area);
421 ERR_FAIL_NULL(area);
422
423 area->set_monitor_callback(p_callback.is_valid() ? p_callback : Callable());
424 }
425
426 void GodotPhysicsServer3D::area_set_ray_pickable(RID p_area, bool p_enable) {
427 GodotArea3D *area = area_owner.get_or_null(p_area);
428 ERR_FAIL_NULL(area);
429
430 area->set_ray_pickable(p_enable);
431 }
432
433 void GodotPhysicsServer3D::area_set_area_monitor_callback(RID p_area, const Callable &p_callback) {
434 GodotArea3D *area = area_owner.get_or_null(p_area);
435 ERR_FAIL_NULL(area);
436
437 area->set_area_monitor_callback(p_callback.is_valid() ? p_callback : Callable());
438 }
439
440 /* BODY API */
441
442 RID GodotPhysicsServer3D::body_create() {
443 GodotBody3D *body = memnew(GodotBody3D);
444 RID rid = body_owner.make_rid(body);
445 body->set_self(rid);
446 return rid;
447 }
448
449 void GodotPhysicsServer3D::body_set_space(RID p_body, RID p_space) {
450 GodotBody3D *body = body_owner.get_or_null(p_body);
451 ERR_FAIL_NULL(body);
452
453 GodotSpace3D *space = nullptr;
454 if (p_space.is_valid()) {
455 space = space_owner.get_or_null(p_space);
456 ERR_FAIL_NULL(space);
457 }
458
459 if (body->get_space() == space) {
460 return; //pointless
461 }
462
463 body->clear_constraint_map();
464 body->set_space(space);
465 }
466
467 RID GodotPhysicsServer3D::body_get_space(RID p_body) const {
468 GodotBody3D *body = body_owner.get_or_null(p_body);
469 ERR_FAIL_NULL_V(body, RID());
470
471 GodotSpace3D *space = body->get_space();
472 if (!space) {
473 return RID();
474 }
475 return space->get_self();
476 }
477
478 void GodotPhysicsServer3D::body_set_mode(RID p_body, BodyMode p_mode) {
479 GodotBody3D *body = body_owner.get_or_null(p_body);
480 ERR_FAIL_NULL(body);
481
482 body->set_mode(p_mode);
483 }
484
485 PhysicsServer3D::BodyMode GodotPhysicsServer3D::body_get_mode(RID p_body) const {
486 GodotBody3D *body = body_owner.get_or_null(p_body);
487 ERR_FAIL_NULL_V(body, BODY_MODE_STATIC);
488
489 return body->get_mode();
490 }
491
492 void GodotPhysicsServer3D::body_add_shape(RID p_body, RID p_shape, const Transform3D &p_transform, bool p_disabled) {
493 GodotBody3D *body = body_owner.get_or_null(p_body);
494 ERR_FAIL_NULL(body);
495
496 GodotShape3D *shape = shape_owner.get_or_null(p_shape);
497 ERR_FAIL_NULL(shape);
498
499 body->add_shape(shape, p_transform, p_disabled);
500 }
501
502 void GodotPhysicsServer3D::body_set_shape(RID p_body, int p_shape_idx, RID p_shape) {
503 GodotBody3D *body = body_owner.get_or_null(p_body);
504 ERR_FAIL_NULL(body);
505
506 GodotShape3D *shape = shape_owner.get_or_null(p_shape);
507 ERR_FAIL_NULL(shape);
508 ERR_FAIL_COND(!shape->is_configured());
509
510 body->set_shape(p_shape_idx, shape);
511 }
512 void GodotPhysicsServer3D::body_set_shape_transform(RID p_body, int p_shape_idx, const Transform3D &p_transform) {
513 GodotBody3D *body = body_owner.get_or_null(p_body);
514 ERR_FAIL_NULL(body);
515
516 body->set_shape_transform(p_shape_idx, p_transform);
517 }
518
519 int GodotPhysicsServer3D::body_get_shape_count(RID p_body) const {
520 GodotBody3D *body = body_owner.get_or_null(p_body);
521 ERR_FAIL_NULL_V(body, -1);
522
523 return body->get_shape_count();
524 }
525
526 RID GodotPhysicsServer3D::body_get_shape(RID p_body, int p_shape_idx) const {
527 GodotBody3D *body = body_owner.get_or_null(p_body);
528 ERR_FAIL_NULL_V(body, RID());
529
530 GodotShape3D *shape = body->get_shape(p_shape_idx);
531 ERR_FAIL_NULL_V(shape, RID());
532
533 return shape->get_self();
534 }
535
536 void GodotPhysicsServer3D::body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) {
537 GodotBody3D *body = body_owner.get_or_null(p_body);
538 ERR_FAIL_NULL(body);
539 ERR_FAIL_INDEX(p_shape_idx, body->get_shape_count());
540 FLUSH_QUERY_CHECK(body);
541
542 body->set_shape_disabled(p_shape_idx, p_disabled);
543 }
544
545 Transform3D GodotPhysicsServer3D::body_get_shape_transform(RID p_body, int p_shape_idx) const {
546 GodotBody3D *body = body_owner.get_or_null(p_body);
547 ERR_FAIL_NULL_V(body, Transform3D());
548
549 return body->get_shape_transform(p_shape_idx);
550 }
551
552 void GodotPhysicsServer3D::body_remove_shape(RID p_body, int p_shape_idx) {
553 GodotBody3D *body = body_owner.get_or_null(p_body);
554 ERR_FAIL_NULL(body);
555
556 body->remove_shape(p_shape_idx);
557 }
558
559 void GodotPhysicsServer3D::body_clear_shapes(RID p_body) {
560 GodotBody3D *body = body_owner.get_or_null(p_body);
561 ERR_FAIL_NULL(body);
562
563 while (body->get_shape_count()) {
564 body->remove_shape(0);
565 }
566 }
567
568 void GodotPhysicsServer3D::body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) {
569 GodotBody3D *body = body_owner.get_or_null(p_body);
570 ERR_FAIL_NULL(body);
571
572 body->set_continuous_collision_detection(p_enable);
573 }
574
575 bool GodotPhysicsServer3D::body_is_continuous_collision_detection_enabled(RID p_body) const {
576 GodotBody3D *body = body_owner.get_or_null(p_body);
577 ERR_FAIL_NULL_V(body, false);
578
579 return body->is_continuous_collision_detection_enabled();
580 }
581
582 void GodotPhysicsServer3D::body_set_collision_layer(RID p_body, uint32_t p_layer) {
583 GodotBody3D *body = body_owner.get_or_null(p_body);
584 ERR_FAIL_NULL(body);
585
586 body->set_collision_layer(p_layer);
587 }
588
589 uint32_t GodotPhysicsServer3D::body_get_collision_layer(RID p_body) const {
590 const GodotBody3D *body = body_owner.get_or_null(p_body);
591 ERR_FAIL_NULL_V(body, 0);
592
593 return body->get_collision_layer();
594 }
595
596 void GodotPhysicsServer3D::body_set_collision_mask(RID p_body, uint32_t p_mask) {
597 GodotBody3D *body = body_owner.get_or_null(p_body);
598 ERR_FAIL_NULL(body);
599
600 body->set_collision_mask(p_mask);
601 }
602
603 uint32_t GodotPhysicsServer3D::body_get_collision_mask(RID p_body) const {
604 const GodotBody3D *body = body_owner.get_or_null(p_body);
605 ERR_FAIL_NULL_V(body, 0);
606
607 return body->get_collision_mask();
608 }
609
610 void GodotPhysicsServer3D::body_set_collision_priority(RID p_body, real_t p_priority) {
611 GodotBody3D *body = body_owner.get_or_null(p_body);
612 ERR_FAIL_NULL(body);
613
614 body->set_collision_priority(p_priority);
615 }
616
617 real_t GodotPhysicsServer3D::body_get_collision_priority(RID p_body) const {
618 const GodotBody3D *body = body_owner.get_or_null(p_body);
619 ERR_FAIL_NULL_V(body, 0);
620
621 return body->get_collision_priority();
622 }
623
624 void GodotPhysicsServer3D::body_attach_object_instance_id(RID p_body, ObjectID p_id) {
625 GodotBody3D *body = body_owner.get_or_null(p_body);
626 if (body) {
627 body->set_instance_id(p_id);
628 return;
629 }
630
631 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
632 if (soft_body) {
633 soft_body->set_instance_id(p_id);
634 return;
635 }
636
637 ERR_FAIL_MSG("Invalid ID.");
638 }
639
640 ObjectID GodotPhysicsServer3D::body_get_object_instance_id(RID p_body) const {
641 GodotBody3D *body = body_owner.get_or_null(p_body);
642 ERR_FAIL_NULL_V(body, ObjectID());
643
644 return body->get_instance_id();
645 }
646
647 void GodotPhysicsServer3D::body_set_user_flags(RID p_body, uint32_t p_flags) {
648 GodotBody3D *body = body_owner.get_or_null(p_body);
649 ERR_FAIL_NULL(body);
650 }
651
652 uint32_t GodotPhysicsServer3D::body_get_user_flags(RID p_body) const {
653 GodotBody3D *body = body_owner.get_or_null(p_body);
654 ERR_FAIL_NULL_V(body, 0);
655
656 return 0;
657 }
658
659 void GodotPhysicsServer3D::body_set_param(RID p_body, BodyParameter p_param, const Variant &p_value) {
660 GodotBody3D *body = body_owner.get_or_null(p_body);
661 ERR_FAIL_NULL(body);
662
663 body->set_param(p_param, p_value);
664 }
665
666 Variant GodotPhysicsServer3D::body_get_param(RID p_body, BodyParameter p_param) const {
667 GodotBody3D *body = body_owner.get_or_null(p_body);
668 ERR_FAIL_NULL_V(body, 0);
669
670 return body->get_param(p_param);
671 }
672
673 void GodotPhysicsServer3D::body_reset_mass_properties(RID p_body) {
674 GodotBody3D *body = body_owner.get_or_null(p_body);
675 ERR_FAIL_NULL(body);
676
677 return body->reset_mass_properties();
678 }
679
680 void GodotPhysicsServer3D::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
681 GodotBody3D *body = body_owner.get_or_null(p_body);
682 ERR_FAIL_NULL(body);
683
684 body->set_state(p_state, p_variant);
685 }
686
687 Variant GodotPhysicsServer3D::body_get_state(RID p_body, BodyState p_state) const {
688 GodotBody3D *body = body_owner.get_or_null(p_body);
689 ERR_FAIL_NULL_V(body, Variant());
690
691 return body->get_state(p_state);
692 }
693
694 void GodotPhysicsServer3D::body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
695 GodotBody3D *body = body_owner.get_or_null(p_body);
696 ERR_FAIL_NULL(body);
697
698 _update_shapes();
699
700 body->apply_central_impulse(p_impulse);
701 body->wakeup();
702 }
703
704 void GodotPhysicsServer3D::body_apply_impulse(RID p_body, const Vector3 &p_impulse, const Vector3 &p_position) {
705 GodotBody3D *body = body_owner.get_or_null(p_body);
706 ERR_FAIL_NULL(body);
707
708 _update_shapes();
709
710 body->apply_impulse(p_impulse, p_position);
711 body->wakeup();
712 }
713
714 void GodotPhysicsServer3D::body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) {
715 GodotBody3D *body = body_owner.get_or_null(p_body);
716 ERR_FAIL_NULL(body);
717
718 _update_shapes();
719
720 body->apply_torque_impulse(p_impulse);
721 body->wakeup();
722 }
723
724 void GodotPhysicsServer3D::body_apply_central_force(RID p_body, const Vector3 &p_force) {
725 GodotBody3D *body = body_owner.get_or_null(p_body);
726 ERR_FAIL_NULL(body);
727
728 body->apply_central_force(p_force);
729 body->wakeup();
730 }
731
732 void GodotPhysicsServer3D::body_apply_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position) {
733 GodotBody3D *body = body_owner.get_or_null(p_body);
734 ERR_FAIL_NULL(body);
735
736 body->apply_force(p_force, p_position);
737 body->wakeup();
738 }
739
740 void GodotPhysicsServer3D::soft_body_apply_point_impulse(RID p_body, int p_point_index, const Vector3 &p_impulse) {
741 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
742 ERR_FAIL_NULL(soft_body);
743
744 soft_body->apply_node_impulse(p_point_index, p_impulse);
745 }
746
747 void GodotPhysicsServer3D::soft_body_apply_point_force(RID p_body, int p_point_index, const Vector3 &p_force) {
748 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
749 ERR_FAIL_NULL(soft_body);
750
751 soft_body->apply_node_force(p_point_index, p_force);
752 }
753
754 void GodotPhysicsServer3D::soft_body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
755 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
756 ERR_FAIL_NULL(soft_body);
757
758 soft_body->apply_central_impulse(p_impulse);
759 }
760
761 void GodotPhysicsServer3D::soft_body_apply_central_force(RID p_body, const Vector3 &p_force) {
762 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
763 ERR_FAIL_NULL(soft_body);
764
765 soft_body->apply_central_force(p_force);
766 }
767
768 void GodotPhysicsServer3D::body_apply_torque(RID p_body, const Vector3 &p_torque) {
769 GodotBody3D *body = body_owner.get_or_null(p_body);
770 ERR_FAIL_NULL(body);
771
772 body->apply_torque(p_torque);
773 body->wakeup();
774 }
775
776 void GodotPhysicsServer3D::body_add_constant_central_force(RID p_body, const Vector3 &p_force) {
777 GodotBody3D *body = body_owner.get_or_null(p_body);
778 ERR_FAIL_NULL(body);
779
780 body->add_constant_central_force(p_force);
781 body->wakeup();
782 }
783
784 void GodotPhysicsServer3D::body_add_constant_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position) {
785 GodotBody3D *body = body_owner.get_or_null(p_body);
786 ERR_FAIL_NULL(body);
787
788 body->add_constant_force(p_force, p_position);
789 body->wakeup();
790 }
791
792 void GodotPhysicsServer3D::body_add_constant_torque(RID p_body, const Vector3 &p_torque) {
793 GodotBody3D *body = body_owner.get_or_null(p_body);
794 ERR_FAIL_NULL(body);
795
796 body->add_constant_torque(p_torque);
797 body->wakeup();
798 }
799
800 void GodotPhysicsServer3D::body_set_constant_force(RID p_body, const Vector3 &p_force) {
801 GodotBody3D *body = body_owner.get_or_null(p_body);
802 ERR_FAIL_NULL(body);
803
804 body->set_constant_force(p_force);
805 if (!p_force.is_zero_approx()) {
806 body->wakeup();
807 }
808 }
809
810 Vector3 GodotPhysicsServer3D::body_get_constant_force(RID p_body) const {
811 GodotBody3D *body = body_owner.get_or_null(p_body);
812 ERR_FAIL_NULL_V(body, Vector3());
813 return body->get_constant_force();
814 }
815
816 void GodotPhysicsServer3D::body_set_constant_torque(RID p_body, const Vector3 &p_torque) {
817 GodotBody3D *body = body_owner.get_or_null(p_body);
818 ERR_FAIL_NULL(body);
819
820 body->set_constant_torque(p_torque);
821 if (!p_torque.is_zero_approx()) {
822 body->wakeup();
823 }
824 }
825
826 Vector3 GodotPhysicsServer3D::body_get_constant_torque(RID p_body) const {
827 GodotBody3D *body = body_owner.get_or_null(p_body);
828 ERR_FAIL_NULL_V(body, Vector3());
829
830 return body->get_constant_torque();
831 }
832
833 void GodotPhysicsServer3D::body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) {
834 GodotBody3D *body = body_owner.get_or_null(p_body);
835 ERR_FAIL_NULL(body);
836
837 _update_shapes();
838
839 Vector3 v = body->get_linear_velocity();
840 Vector3 axis = p_axis_velocity.normalized();
841 v -= axis * axis.dot(v);
842 v += p_axis_velocity;
843 body->set_linear_velocity(v);
844 body->wakeup();
845 }
846
847 void GodotPhysicsServer3D::body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) {
848 GodotBody3D *body = body_owner.get_or_null(p_body);
849 ERR_FAIL_NULL(body);
850
851 body->set_axis_lock(p_axis, p_lock);
852 body->wakeup();
853 }
854
855 bool GodotPhysicsServer3D::body_is_axis_locked(RID p_body, BodyAxis p_axis) const {
856 const GodotBody3D *body = body_owner.get_or_null(p_body);
857 ERR_FAIL_NULL_V(body, false);
858 return body->is_axis_locked(p_axis);
859 }
860
861 void GodotPhysicsServer3D::body_add_collision_exception(RID p_body, RID p_body_b) {
862 GodotBody3D *body = body_owner.get_or_null(p_body);
863 ERR_FAIL_NULL(body);
864
865 body->add_exception(p_body_b);
866 body->wakeup();
867 }
868
869 void GodotPhysicsServer3D::body_remove_collision_exception(RID p_body, RID p_body_b) {
870 GodotBody3D *body = body_owner.get_or_null(p_body);
871 ERR_FAIL_NULL(body);
872
873 body->remove_exception(p_body_b);
874 body->wakeup();
875 }
876
877 void GodotPhysicsServer3D::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
878 GodotBody3D *body = body_owner.get_or_null(p_body);
879 ERR_FAIL_NULL(body);
880
881 for (int i = 0; i < body->get_exceptions().size(); i++) {
882 p_exceptions->push_back(body->get_exceptions()[i]);
883 }
884 }
885
886 void GodotPhysicsServer3D::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
887 GodotBody3D *body = body_owner.get_or_null(p_body);
888 ERR_FAIL_NULL(body);
889 }
890
891 real_t GodotPhysicsServer3D::body_get_contacts_reported_depth_threshold(RID p_body) const {
892 GodotBody3D *body = body_owner.get_or_null(p_body);
893 ERR_FAIL_NULL_V(body, 0);
894 return 0;
895 }
896
897 void GodotPhysicsServer3D::body_set_omit_force_integration(RID p_body, bool p_omit) {
898 GodotBody3D *body = body_owner.get_or_null(p_body);
899 ERR_FAIL_NULL(body);
900
901 body->set_omit_force_integration(p_omit);
902 }
903
904 bool GodotPhysicsServer3D::body_is_omitting_force_integration(RID p_body) const {
905 GodotBody3D *body = body_owner.get_or_null(p_body);
906 ERR_FAIL_NULL_V(body, false);
907 return body->get_omit_force_integration();
908 }
909
910 void GodotPhysicsServer3D::body_set_max_contacts_reported(RID p_body, int p_contacts) {
911 GodotBody3D *body = body_owner.get_or_null(p_body);
912 ERR_FAIL_NULL(body);
913 body->set_max_contacts_reported(p_contacts);
914 }
915
916 int GodotPhysicsServer3D::body_get_max_contacts_reported(RID p_body) const {
917 GodotBody3D *body = body_owner.get_or_null(p_body);
918 ERR_FAIL_NULL_V(body, -1);
919 return body->get_max_contacts_reported();
920 }
921
922 void GodotPhysicsServer3D::body_set_state_sync_callback(RID p_body, const Callable &p_callable) {
923 GodotBody3D *body = body_owner.get_or_null(p_body);
924 ERR_FAIL_NULL(body);
925 body->set_state_sync_callback(p_callable);
926 }
927
928 void GodotPhysicsServer3D::body_set_force_integration_callback(RID p_body, const Callable &p_callable, const Variant &p_udata) {
929 GodotBody3D *body = body_owner.get_or_null(p_body);
930 ERR_FAIL_NULL(body);
931 body->set_force_integration_callback(p_callable, p_udata);
932 }
933
934 void GodotPhysicsServer3D::body_set_ray_pickable(RID p_body, bool p_enable) {
935 GodotBody3D *body = body_owner.get_or_null(p_body);
936 ERR_FAIL_NULL(body);
937 body->set_ray_pickable(p_enable);
938 }
939
940 bool GodotPhysicsServer3D::body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result) {
941 GodotBody3D *body = body_owner.get_or_null(p_body);
942 ERR_FAIL_NULL_V(body, false);
943 ERR_FAIL_NULL_V(body->get_space(), false);
944 ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
945
946 _update_shapes();
947
948 return body->get_space()->test_body_motion(body, p_parameters, r_result);
949 }
950
951 PhysicsDirectBodyState3D *GodotPhysicsServer3D::body_get_direct_state(RID p_body) {
952 ERR_FAIL_COND_V_MSG((using_threads && !doing_sync), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
953
954 if (!body_owner.owns(p_body)) {
955 return nullptr;
956 }
957
958 GodotBody3D *body = body_owner.get_or_null(p_body);
959 ERR_FAIL_NULL_V(body, nullptr);
960
961 if (!body->get_space()) {
962 return nullptr;
963 }
964
965 ERR_FAIL_COND_V_MSG(body->get_space()->is_locked(), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
966
967 return body->get_direct_state();
968 }
969
970 /* SOFT BODY */
971
972 RID GodotPhysicsServer3D::soft_body_create() {
973 GodotSoftBody3D *soft_body = memnew(GodotSoftBody3D);
974 RID rid = soft_body_owner.make_rid(soft_body);
975 soft_body->set_self(rid);
976 return rid;
977 }
978
979 void GodotPhysicsServer3D::soft_body_update_rendering_server(RID p_body, RequiredParam<PhysicsServer3DRenderingServerHandler> rp_rendering_server_handler) {
980 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
981 ERR_FAIL_NULL(soft_body);
982 EXTRACT_PARAM_OR_FAIL(p_rendering_server_handler, rp_rendering_server_handler);
983
984 soft_body->update_rendering_server(p_rendering_server_handler);
985 }
986
987 void GodotPhysicsServer3D::soft_body_set_space(RID p_body, RID p_space) {
988 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
989 ERR_FAIL_NULL(soft_body);
990
991 GodotSpace3D *space = nullptr;
992 if (p_space.is_valid()) {
993 space = space_owner.get_or_null(p_space);
994 ERR_FAIL_NULL(space);
995 }
996
997 if (soft_body->get_space() == space) {
998 return;
999 }
1000
1001 soft_body->set_space(space);
1002 }
1003
1004 RID GodotPhysicsServer3D::soft_body_get_space(RID p_body) const {
1005 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1006 ERR_FAIL_NULL_V(soft_body, RID());
1007
1008 GodotSpace3D *space = soft_body->get_space();
1009 if (!space) {
1010 return RID();
1011 }
1012 return space->get_self();
1013 }
1014
1015 void GodotPhysicsServer3D::soft_body_set_collision_layer(RID p_body, uint32_t p_layer) {
1016 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1017 ERR_FAIL_NULL(soft_body);
1018
1019 soft_body->set_collision_layer(p_layer);
1020 }
1021
1022 uint32_t GodotPhysicsServer3D::soft_body_get_collision_layer(RID p_body) const {
1023 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1024 ERR_FAIL_NULL_V(soft_body, 0);
1025
1026 return soft_body->get_collision_layer();
1027 }
1028
1029 void GodotPhysicsServer3D::soft_body_set_collision_mask(RID p_body, uint32_t p_mask) {
1030 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1031 ERR_FAIL_NULL(soft_body);
1032
1033 soft_body->set_collision_mask(p_mask);
1034 }
1035
1036 uint32_t GodotPhysicsServer3D::soft_body_get_collision_mask(RID p_body) const {
1037 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1038 ERR_FAIL_NULL_V(soft_body, 0);
1039
1040 return soft_body->get_collision_mask();
1041 }
1042
1043 void GodotPhysicsServer3D::soft_body_add_collision_exception(RID p_body, RID p_body_b) {
1044 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1045 ERR_FAIL_NULL(soft_body);
1046
1047 soft_body->add_exception(p_body_b);
1048 }
1049
1050 void GodotPhysicsServer3D::soft_body_remove_collision_exception(RID p_body, RID p_body_b) {
1051 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1052 ERR_FAIL_NULL(soft_body);
1053
1054 soft_body->remove_exception(p_body_b);
1055 }
1056
1057 void GodotPhysicsServer3D::soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
1058 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1059 ERR_FAIL_NULL(soft_body);
1060
1061 for (int i = 0; i < soft_body->get_exceptions().size(); i++) {
1062 p_exceptions->push_back(soft_body->get_exceptions()[i]);
1063 }
1064 }
1065
1066 void GodotPhysicsServer3D::soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
1067 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1068 ERR_FAIL_NULL(soft_body);
1069
1070 soft_body->set_state(p_state, p_variant);
1071 }
1072
1073 Variant GodotPhysicsServer3D::soft_body_get_state(RID p_body, BodyState p_state) const {
1074 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1075 ERR_FAIL_NULL_V(soft_body, Variant());
1076
1077 return soft_body->get_state(p_state);
1078 }
1079
1080 void GodotPhysicsServer3D::soft_body_set_transform(RID p_body, const Transform3D &p_transform) {
1081 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1082 ERR_FAIL_NULL(soft_body);
1083
1084 soft_body->set_state(BODY_STATE_TRANSFORM, p_transform);
1085 }
1086
1087 void GodotPhysicsServer3D::soft_body_set_ray_pickable(RID p_body, bool p_enable) {
1088 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1089 ERR_FAIL_NULL(soft_body);
1090
1091 soft_body->set_ray_pickable(p_enable);
1092 }
1093
1094 void GodotPhysicsServer3D::soft_body_set_simulation_precision(RID p_body, int p_simulation_precision) {
1095 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1096 ERR_FAIL_NULL(soft_body);
1097
1098 soft_body->set_iteration_count(p_simulation_precision);
1099 }
1100
1101 int GodotPhysicsServer3D::soft_body_get_simulation_precision(RID p_body) const {
1102 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1103 ERR_FAIL_NULL_V(soft_body, 0.f);
1104
1105 return soft_body->get_iteration_count();
1106 }
1107
1108 void GodotPhysicsServer3D::soft_body_set_total_mass(RID p_body, real_t p_total_mass) {
1109 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1110 ERR_FAIL_NULL(soft_body);
1111
1112 soft_body->set_total_mass(p_total_mass);
1113 }
1114
1115 real_t GodotPhysicsServer3D::soft_body_get_total_mass(RID p_body) const {
1116 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1117 ERR_FAIL_NULL_V(soft_body, 0.f);
1118
1119 return soft_body->get_total_mass();
1120 }
1121
1122 void GodotPhysicsServer3D::soft_body_set_linear_stiffness(RID p_body, real_t p_stiffness) {
1123 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1124 ERR_FAIL_NULL(soft_body);
1125
1126 soft_body->set_linear_stiffness(p_stiffness);
1127 }
1128
1129 real_t GodotPhysicsServer3D::soft_body_get_linear_stiffness(RID p_body) const {
1130 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1131 ERR_FAIL_NULL_V(soft_body, 0.f);
1132
1133 return soft_body->get_linear_stiffness();
1134 }
1135
1136 void GodotPhysicsServer3D::soft_body_set_shrinking_factor(RID p_body, real_t p_shrinking_factor) {
1137 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1138 ERR_FAIL_NULL(soft_body);
1139
1140 soft_body->set_shrinking_factor(p_shrinking_factor);
1141 }
1142
1143 real_t GodotPhysicsServer3D::soft_body_get_shrinking_factor(RID p_body) const {
1144 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1145 ERR_FAIL_NULL_V(soft_body, 0.f);
1146
1147 return soft_body->get_shrinking_factor();
1148 }
1149
1150 void GodotPhysicsServer3D::soft_body_set_pressure_coefficient(RID p_body, real_t p_pressure_coefficient) {
1151 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1152 ERR_FAIL_NULL(soft_body);
1153
1154 soft_body->set_pressure_coefficient(p_pressure_coefficient);
1155 }
1156
1157 real_t GodotPhysicsServer3D::soft_body_get_pressure_coefficient(RID p_body) const {
1158 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1159 ERR_FAIL_NULL_V(soft_body, 0.f);
1160
1161 return soft_body->get_pressure_coefficient();
1162 }
1163
1164 void GodotPhysicsServer3D::soft_body_set_damping_coefficient(RID p_body, real_t p_damping_coefficient) {
1165 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1166 ERR_FAIL_NULL(soft_body);
1167
1168 soft_body->set_damping_coefficient(p_damping_coefficient);
1169 }
1170
1171 real_t GodotPhysicsServer3D::soft_body_get_damping_coefficient(RID p_body) const {
1172 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1173 ERR_FAIL_NULL_V(soft_body, 0.f);
1174
1175 return soft_body->get_damping_coefficient();
1176 }
1177
1178 void GodotPhysicsServer3D::soft_body_set_drag_coefficient(RID p_body, real_t p_drag_coefficient) {
1179 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1180 ERR_FAIL_NULL(soft_body);
1181
1182 soft_body->set_drag_coefficient(p_drag_coefficient);
1183 }
1184
1185 real_t GodotPhysicsServer3D::soft_body_get_drag_coefficient(RID p_body) const {
1186 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1187 ERR_FAIL_NULL_V(soft_body, 0.f);
1188
1189 return soft_body->get_drag_coefficient();
1190 }
1191
1192 void GodotPhysicsServer3D::soft_body_set_mesh(RID p_body, RID p_mesh) {
1193 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1194 ERR_FAIL_NULL(soft_body);
1195
1196 soft_body->set_mesh(p_mesh);
1197 }
1198
1199 AABB GodotPhysicsServer3D::soft_body_get_bounds(RID p_body) const {
1200 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1201 ERR_FAIL_NULL_V(soft_body, AABB());
1202
1203 return soft_body->get_bounds();
1204 }
1205
1206 void GodotPhysicsServer3D::soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position) {
1207 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1208 ERR_FAIL_NULL(soft_body);
1209
1210 soft_body->set_vertex_position(p_point_index, p_global_position);
1211 }
1212
1213 Vector3 GodotPhysicsServer3D::soft_body_get_point_global_position(RID p_body, int p_point_index) const {
1214 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1215 ERR_FAIL_NULL_V(soft_body, Vector3());
1216
1217 return soft_body->get_vertex_position(p_point_index);
1218 }
1219
1220 void GodotPhysicsServer3D::soft_body_remove_all_pinned_points(RID p_body) {
1221 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1222 ERR_FAIL_NULL(soft_body);
1223
1224 soft_body->unpin_all_vertices();
1225 }
1226
1227 void GodotPhysicsServer3D::soft_body_pin_point(RID p_body, int p_point_index, bool p_pin) {
1228 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1229 ERR_FAIL_NULL(soft_body);
1230
1231 if (p_pin) {
1232 soft_body->pin_vertex(p_point_index);
1233 } else {
1234 soft_body->unpin_vertex(p_point_index);
1235 }
1236 }
1237
1238 bool GodotPhysicsServer3D::soft_body_is_point_pinned(RID p_body, int p_point_index) const {
1239 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1240 ERR_FAIL_NULL_V(soft_body, false);
1241
1242 return soft_body->is_vertex_pinned(p_point_index);
1243 }
1244
1245 /* JOINT API */
1246
1247 RID GodotPhysicsServer3D::joint_create() {
1248 GodotJoint3D *joint = memnew(GodotJoint3D);
1249 RID rid = joint_owner.make_rid(joint);
1250 joint->set_self(rid);
1251 return rid;
1252 }
1253
1254 void GodotPhysicsServer3D::joint_clear(RID p_joint) {
1255 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1256 ERR_FAIL_NULL(joint);
1257 if (joint->get_type() != JOINT_TYPE_MAX) {
1258 GodotJoint3D *empty_joint = memnew(GodotJoint3D);
1259 empty_joint->copy_settings_from(joint);
1260
1261 joint_owner.replace(p_joint, empty_joint);
1262 memdelete(joint);
1263 }
1264 }
1265
1266 void GodotPhysicsServer3D::joint_make_pin(RID p_joint, RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) {
1267 GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
1268 ERR_FAIL_NULL(body_A);
1269
1270 if (!p_body_B.is_valid()) {
1271 ERR_FAIL_NULL(body_A->get_space());
1272 p_body_B = body_A->get_space()->get_static_global_body();
1273 }
1274
1275 GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
1276 ERR_FAIL_NULL(body_B);
1277
1278 ERR_FAIL_COND(body_A == body_B);
1279
1280 GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
1281 ERR_FAIL_NULL(prev_joint);
1282
1283 GodotJoint3D *joint = memnew(GodotPinJoint3D(body_A, p_local_A, body_B, p_local_B));
1284
1285 joint->copy_settings_from(prev_joint);
1286 joint_owner.replace(p_joint, joint);
1287 memdelete(prev_joint);
1288 }
1289
1290 void GodotPhysicsServer3D::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
1291 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1292 ERR_FAIL_NULL(joint);
1293 ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
1294 GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
1295 pin_joint->set_param(p_param, p_value);
1296 }
1297
1298 real_t GodotPhysicsServer3D::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
1299 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1300 ERR_FAIL_NULL_V(joint, 0);
1301 ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, 0);
1302 GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
1303 return pin_joint->get_param(p_param);
1304 }
1305
1306 void GodotPhysicsServer3D::pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) {
1307 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1308 ERR_FAIL_NULL(joint);
1309 ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
1310 GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
1311 pin_joint->set_pos_a(p_A);
1312 }
1313
1314 Vector3 GodotPhysicsServer3D::pin_joint_get_local_a(RID p_joint) const {
1315 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1316 ERR_FAIL_NULL_V(joint, Vector3());
1317 ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
1318 GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
1319 return pin_joint->get_position_a();
1320 }
1321
1322 void GodotPhysicsServer3D::pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) {
1323 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1324 ERR_FAIL_NULL(joint);
1325 ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
1326 GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
1327 pin_joint->set_pos_b(p_B);
1328 }
1329
1330 Vector3 GodotPhysicsServer3D::pin_joint_get_local_b(RID p_joint) const {
1331 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1332 ERR_FAIL_NULL_V(joint, Vector3());
1333 ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
1334 GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
1335 return pin_joint->get_position_b();
1336 }
1337
1338 void GodotPhysicsServer3D::joint_make_hinge(RID p_joint, RID p_body_A, const Transform3D &p_frame_A, RID p_body_B, const Transform3D &p_frame_B) {
1339 GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
1340 ERR_FAIL_NULL(body_A);
1341
1342 if (!p_body_B.is_valid()) {
1343 ERR_FAIL_NULL(body_A->get_space());
1344 p_body_B = body_A->get_space()->get_static_global_body();
1345 }
1346
1347 GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
1348 ERR_FAIL_NULL(body_B);
1349
1350 ERR_FAIL_COND(body_A == body_B);
1351
1352 GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
1353 ERR_FAIL_NULL(prev_joint);
1354
1355 GodotJoint3D *joint = memnew(GodotHingeJoint3D(body_A, body_B, p_frame_A, p_frame_B));
1356
1357 joint->copy_settings_from(prev_joint);
1358 joint_owner.replace(p_joint, joint);
1359 memdelete(prev_joint);
1360 }
1361
1362 void GodotPhysicsServer3D::joint_make_hinge_simple(RID p_joint, RID p_body_A, const Vector3 &p_pivot_A, const Vector3 &p_axis_A, RID p_body_B, const Vector3 &p_pivot_B, const Vector3 &p_axis_B) {
1363 GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
1364 ERR_FAIL_NULL(body_A);
1365
1366 if (!p_body_B.is_valid()) {
1367 ERR_FAIL_NULL(body_A->get_space());
1368 p_body_B = body_A->get_space()->get_static_global_body();
1369 }
1370
1371 GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
1372 ERR_FAIL_NULL(body_B);
1373
1374 ERR_FAIL_COND(body_A == body_B);
1375
1376 GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
1377 ERR_FAIL_NULL(prev_joint);
1378
1379 GodotJoint3D *joint = memnew(GodotHingeJoint3D(body_A, body_B, p_pivot_A, p_pivot_B, p_axis_A, p_axis_B));
1380
1381 joint->copy_settings_from(prev_joint);
1382 joint_owner.replace(p_joint, joint);
1383 memdelete(prev_joint);
1384 }
1385
1386 void GodotPhysicsServer3D::hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) {
1387 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1388 ERR_FAIL_NULL(joint);
1389 ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
1390 GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
1391 hinge_joint->set_param(p_param, p_value);
1392 }
1393
1394 real_t GodotPhysicsServer3D::hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const {
1395 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1396 ERR_FAIL_NULL_V(joint, 0);
1397 ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, 0);
1398 GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
1399 return hinge_joint->get_param(p_param);
1400 }
1401
1402 void GodotPhysicsServer3D::hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_enabled) {
1403 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1404 ERR_FAIL_NULL(joint);
1405 ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
1406 GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
1407 hinge_joint->set_flag(p_flag, p_enabled);
1408 }
1409
1410 bool GodotPhysicsServer3D::hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const {
1411 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1412 ERR_FAIL_NULL_V(joint, false);
1413 ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, false);
1414 GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
1415 return hinge_joint->get_flag(p_flag);
1416 }
1417
1418 void GodotPhysicsServer3D::joint_set_solver_priority(RID p_joint, int p_priority) {
1419 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1420 ERR_FAIL_NULL(joint);
1421 joint->set_priority(p_priority);
1422 }
1423
1424 int GodotPhysicsServer3D::joint_get_solver_priority(RID p_joint) const {
1425 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1426 ERR_FAIL_NULL_V(joint, 0);
1427 return joint->get_priority();
1428 }
1429
1430 void GodotPhysicsServer3D::joint_disable_collisions_between_bodies(RID p_joint, bool p_disable) {
1431 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1432 ERR_FAIL_NULL(joint);
1433
1434 joint->disable_collisions_between_bodies(p_disable);
1435
1436 if (2 == joint->get_body_count()) {
1437 GodotBody3D *body_a = *joint->get_body_ptr();
1438 GodotBody3D *body_b = *(joint->get_body_ptr() + 1);
1439
1440 if (p_disable) {
1441 body_add_collision_exception(body_a->get_self(), body_b->get_self());
1442 body_add_collision_exception(body_b->get_self(), body_a->get_self());
1443 } else {
1444 body_remove_collision_exception(body_a->get_self(), body_b->get_self());
1445 body_remove_collision_exception(body_b->get_self(), body_a->get_self());
1446 }
1447 }
1448 }
1449
1450 bool GodotPhysicsServer3D::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
1451 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1452 ERR_FAIL_NULL_V(joint, true);
1453
1454 return joint->is_disabled_collisions_between_bodies();
1455 }
1456
1457 GodotPhysicsServer3D::JointType GodotPhysicsServer3D::joint_get_type(RID p_joint) const {
1458 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1459 ERR_FAIL_NULL_V(joint, JOINT_TYPE_PIN);
1460 return joint->get_type();
1461 }
1462
1463 void GodotPhysicsServer3D::joint_make_slider(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) {
1464 GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
1465 ERR_FAIL_NULL(body_A);
1466
1467 if (!p_body_B.is_valid()) {
1468 ERR_FAIL_NULL(body_A->get_space());
1469 p_body_B = body_A->get_space()->get_static_global_body();
1470 }
1471
1472 GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
1473 ERR_FAIL_NULL(body_B);
1474
1475 ERR_FAIL_COND(body_A == body_B);
1476
1477 GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
1478 ERR_FAIL_NULL(prev_joint);
1479
1480 GodotJoint3D *joint = memnew(GodotSliderJoint3D(body_A, body_B, p_local_frame_A, p_local_frame_B));
1481
1482 joint->copy_settings_from(prev_joint);
1483 joint_owner.replace(p_joint, joint);
1484 memdelete(prev_joint);
1485 }
1486
1487 void GodotPhysicsServer3D::slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) {
1488 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1489 ERR_FAIL_NULL(joint);
1490 ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_SLIDER);
1491 GodotSliderJoint3D *slider_joint = static_cast<GodotSliderJoint3D *>(joint);
1492 slider_joint->set_param(p_param, p_value);
1493 }
1494
1495 real_t GodotPhysicsServer3D::slider_joint_get_param(RID p_joint, SliderJointParam p_param) const {
1496 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1497 ERR_FAIL_NULL_V(joint, 0);
1498 ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0);
1499 GodotSliderJoint3D *slider_joint = static_cast<GodotSliderJoint3D *>(joint);
1500 return slider_joint->get_param(p_param);
1501 }
1502
1503 void GodotPhysicsServer3D::joint_make_cone_twist(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) {
1504 GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
1505 ERR_FAIL_NULL(body_A);
1506
1507 if (!p_body_B.is_valid()) {
1508 ERR_FAIL_NULL(body_A->get_space());
1509 p_body_B = body_A->get_space()->get_static_global_body();
1510 }
1511
1512 GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
1513 ERR_FAIL_NULL(body_B);
1514
1515 ERR_FAIL_COND(body_A == body_B);
1516
1517 GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
1518 ERR_FAIL_NULL(prev_joint);
1519
1520 GodotJoint3D *joint = memnew(GodotConeTwistJoint3D(body_A, body_B, p_local_frame_A, p_local_frame_B));
1521
1522 joint->copy_settings_from(prev_joint);
1523 joint_owner.replace(p_joint, joint);
1524 memdelete(prev_joint);
1525 }
1526
1527 void GodotPhysicsServer3D::cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) {
1528 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1529 ERR_FAIL_NULL(joint);
1530 ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_CONE_TWIST);
1531 GodotConeTwistJoint3D *cone_twist_joint = static_cast<GodotConeTwistJoint3D *>(joint);
1532 cone_twist_joint->set_param(p_param, p_value);
1533 }
1534
1535 real_t GodotPhysicsServer3D::cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const {
1536 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1537 ERR_FAIL_NULL_V(joint, 0);
1538 ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0);
1539 GodotConeTwistJoint3D *cone_twist_joint = static_cast<GodotConeTwistJoint3D *>(joint);
1540 return cone_twist_joint->get_param(p_param);
1541 }
1542
1543 void GodotPhysicsServer3D::joint_make_generic_6dof(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) {
1544 GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
1545 ERR_FAIL_NULL(body_A);
1546
1547 if (!p_body_B.is_valid()) {
1548 ERR_FAIL_NULL(body_A->get_space());
1549 p_body_B = body_A->get_space()->get_static_global_body();
1550 }
1551
1552 GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
1553 ERR_FAIL_NULL(body_B);
1554
1555 ERR_FAIL_COND(body_A == body_B);
1556
1557 GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
1558 ERR_FAIL_NULL(prev_joint);
1559
1560 GodotJoint3D *joint = memnew(GodotGeneric6DOFJoint3D(body_A, body_B, p_local_frame_A, p_local_frame_B, true));
1561
1562 joint->copy_settings_from(prev_joint);
1563 joint_owner.replace(p_joint, joint);
1564 memdelete(prev_joint);
1565 }
1566
1567 void GodotPhysicsServer3D::generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param, real_t p_value) {
1568 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1569 ERR_FAIL_NULL(joint);
1570 ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
1571 GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
1572 generic_6dof_joint->set_param(p_axis, p_param, p_value);
1573 }
1574
1575 real_t GodotPhysicsServer3D::generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param) const {
1576 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1577 ERR_FAIL_NULL_V(joint, 0);
1578 ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, 0);
1579 GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
1580 return generic_6dof_joint->get_param(p_axis, p_param);
1581 }
1582
1583 void GodotPhysicsServer3D::generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag, bool p_enable) {
1584 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1585 ERR_FAIL_NULL(joint);
1586 ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
1587 GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
1588 generic_6dof_joint->set_flag(p_axis, p_flag, p_enable);
1589 }
1590
1591 bool GodotPhysicsServer3D::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag) const {
1592 GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1593 ERR_FAIL_NULL_V(joint, false);
1594 ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, false);
1595 GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
1596 return generic_6dof_joint->get_flag(p_axis, p_flag);
1597 }
1598
1599 void GodotPhysicsServer3D::free_rid(RID p_rid) {
1600 _update_shapes(); //just in case
1601
1602 if (shape_owner.owns(p_rid)) {
1603 GodotShape3D *shape = shape_owner.get_or_null(p_rid);
1604
1605 while (shape->get_owners().size()) {
1606 GodotShapeOwner3D *so = shape->get_owners().begin()->key;
1607 so->remove_shape(shape);
1608 }
1609
1610 shape_owner.free(p_rid);
1611 memdelete(shape);
1612 } else if (body_owner.owns(p_rid)) {
1613 GodotBody3D *body = body_owner.get_or_null(p_rid);
1614
1615 body->set_space(nullptr);
1616
1617 while (body->get_shape_count()) {
1618 body->remove_shape(0);
1619 }
1620
1621 body_owner.free(p_rid);
1622 memdelete(body);
1623 } else if (soft_body_owner.owns(p_rid)) {
1624 GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_rid);
1625
1626 soft_body->set_space(nullptr);
1627
1628 soft_body_owner.free(p_rid);
1629 memdelete(soft_body);
1630 } else if (area_owner.owns(p_rid)) {
1631 GodotArea3D *area = area_owner.get_or_null(p_rid);
1632
1633 area->set_space(nullptr);
1634
1635 while (area->get_shape_count()) {
1636 area->remove_shape(0);
1637 }
1638
1639 area_owner.free(p_rid);
1640 memdelete(area);
1641 } else if (space_owner.owns(p_rid)) {
1642 GodotSpace3D *space = space_owner.get_or_null(p_rid);
1643
1644 while (space->get_objects().size()) {
1645 GodotCollisionObject3D *co = static_cast<GodotCollisionObject3D *>(*space->get_objects().begin());
1646 co->set_space(nullptr);
1647 }
1648
1649 active_spaces.erase(space);
1650 free_rid(space->get_default_area()->get_self());
1651 free_rid(space->get_static_global_body());
1652
1653 space_owner.free(p_rid);
1654 memdelete(space);
1655 } else if (joint_owner.owns(p_rid)) {
1656 GodotJoint3D *joint = joint_owner.get_or_null(p_rid);
1657
1658 joint_owner.free(p_rid);
1659 memdelete(joint);
1660
1661 } else {
1662 ERR_FAIL_MSG("Invalid ID.");
1663 }
1664 }
1665
1666 void GodotPhysicsServer3D::set_active(bool p_active) {
1667 active = p_active;
1668 }
1669
1670 void GodotPhysicsServer3D::init() {
1671 stepper = memnew(GodotStep3D);
1672 }
1673
1674 void GodotPhysicsServer3D::step(real_t p_step) {
1675 if (!active) {
1676 return;
1677 }
1678
1679 _update_shapes();
1680
1681 island_count = 0;
1682 active_objects = 0;
1683 collision_pairs = 0;
1684 for (GodotSpace3D *E : active_spaces) {
1685 stepper->step(E, p_step);
1686 island_count += E->get_island_count();
1687 active_objects += E->get_active_objects();
1688 collision_pairs += E->get_collision_pairs();
1689 }
1690 }
1691
1692 void GodotPhysicsServer3D::sync() {
1693 doing_sync = true;
1694 }
1695
1696 void GodotPhysicsServer3D::flush_queries() {
1697 if (!active) {
1698 return;
1699 }
1700
1701 flushing_queries = true;
1702
1703 uint64_t time_beg = OS::get_singleton()->get_ticks_usec();
1704
1705 for (GodotSpace3D *E : active_spaces) {
1706 GodotSpace3D *space = E;
1707 space->call_queries();
1708 }
1709
1710 flushing_queries = false;
1711
1712 if (EngineDebugger::is_profiling("servers")) {
1713 uint64_t total_time[GodotSpace3D::ELAPSED_TIME_MAX];
1714 static const char *time_name[GodotSpace3D::ELAPSED_TIME_MAX] = {
1715 "integrate_forces",
1716 "generate_islands",
1717 "setup_constraints",
1718 "solve_constraints",
1719 "integrate_velocities"
1720 };
1721
1722 for (int i = 0; i < GodotSpace3D::ELAPSED_TIME_MAX; i++) {
1723 total_time[i] = 0;
1724 }
1725
1726 for (const GodotSpace3D *E : active_spaces) {
1727 for (int i = 0; i < GodotSpace3D::ELAPSED_TIME_MAX; i++) {
1728 total_time[i] += E->get_elapsed_time(GodotSpace3D::ElapsedTime(i));
1729 }
1730 }
1731
1732 Array values;
1733 values.resize(GodotSpace3D::ELAPSED_TIME_MAX * 2);
1734 for (int i = 0; i < GodotSpace3D::ELAPSED_TIME_MAX; i++) {
1735 values[i * 2 + 0] = time_name[i];
1736 values[i * 2 + 1] = USEC_TO_SEC(total_time[i]);
1737 }
1738 values.push_back("flush_queries");
1739 values.push_back(USEC_TO_SEC(OS::get_singleton()->get_ticks_usec() - time_beg));
1740
1741 values.push_front("physics_3d");
1742 EngineDebugger::profiler_add_frame_data("servers", values);
1743 }
1744 }
1745
1746 void GodotPhysicsServer3D::end_sync() {
1747 doing_sync = false;
1748 }
1749
1750 void GodotPhysicsServer3D::finish() {
1751 memdelete(stepper);
1752 }
1753
1754 int GodotPhysicsServer3D::get_process_info(ProcessInfo p_info) {
1755 switch (p_info) {
1756 case INFO_ACTIVE_OBJECTS: {
1757 return active_objects;
1758 } break;
1759 case INFO_COLLISION_PAIRS: {
1760 return collision_pairs;
1761 } break;
1762 case INFO_ISLAND_COUNT: {
1763 return island_count;
1764 } break;
1765 }
1766
1767 return 0;
1768 }
1769
1770 void GodotPhysicsServer3D::_update_shapes() {
1771 while (pending_shape_update_list.first()) {
1772 pending_shape_update_list.first()->self()->_shape_changed();
1773 pending_shape_update_list.remove(pending_shape_update_list.first());
1774 }
1775 }
1776
1777 void GodotPhysicsServer3D::_shape_col_cbk(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata) {
1778 CollCbkData *cbk = static_cast<CollCbkData *>(p_userdata);
1779
1780 if (cbk->max == 0) {
1781 return;
1782 }
1783
1784 if (cbk->amount == cbk->max) {
1785 //find least deep
1786 real_t min_depth = 1e20;
1787 int min_depth_idx = 0;
1788 for (int i = 0; i < cbk->amount; i++) {
1789 real_t d = cbk->ptr[i * 2 + 0].distance_squared_to(cbk->ptr[i * 2 + 1]);
1790 if (d < min_depth) {
1791 min_depth = d;
1792 min_depth_idx = i;
1793 }
1794 }
1795
1796 real_t d = p_point_A.distance_squared_to(p_point_B);
1797 if (d < min_depth) {
1798 return;
1799 }
1800 cbk->ptr[min_depth_idx * 2 + 0] = p_point_A;
1801 cbk->ptr[min_depth_idx * 2 + 1] = p_point_B;
1802
1803 } else {
1804 cbk->ptr[cbk->amount * 2 + 0] = p_point_A;
1805 cbk->ptr[cbk->amount * 2 + 1] = p_point_B;
1806 cbk->amount++;
1807 }
1808 }
1809
1810 GodotPhysicsServer3D *GodotPhysicsServer3D::godot_singleton = nullptr;
1811 GodotPhysicsServer3D::GodotPhysicsServer3D(bool p_using_threads) {
1812 godot_singleton = this;
1813 GodotBroadPhase3D::create_func = GodotBroadPhase3DBVH::_create;
1814
1815 using_threads = p_using_threads;
1816 }