1/**************************************************************************/
2/* collision_object_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 "collision_object_3d.h"
32
33#include "scene/resources/shape_3d.h"
34#include "scene/scene_string_names.h"
35
36void CollisionObject3D::_notification(int p_what) {
37 switch (p_what) {
38 case NOTIFICATION_ENTER_TREE: {
39 if (_are_collision_shapes_visible()) {
40 debug_shape_old_transform = get_global_transform();
41 for (const KeyValue<uint32_t, ShapeData> &E : shapes) {
42 debug_shapes_to_update.insert(E.key);
43 }
44 _update_debug_shapes();
45 }
46#ifdef TOOLS_ENABLED
47 if (Engine::get_singleton()->is_editor_hint()) {
48 set_notify_local_transform(true); // Used for warnings and only in editor.
49 }
50#endif
51 } break;
52
53 case NOTIFICATION_EXIT_TREE: {
54 if (debug_shapes_count > 0) {
55 _clear_debug_shapes();
56 }
57 } break;
58
59 case NOTIFICATION_ENTER_WORLD: {
60 if (area) {
61 PhysicsServer3D::get_singleton()->area_set_transform(rid, get_global_transform());
62 } else {
63 PhysicsServer3D::get_singleton()->body_set_state(rid, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
64 }
65
66 bool disabled = !is_enabled();
67
68 if (disabled && (disable_mode != DISABLE_MODE_REMOVE)) {
69 _apply_disabled();
70 }
71
72 if (!disabled || (disable_mode != DISABLE_MODE_REMOVE)) {
73 Ref<World3D> world_ref = get_world_3d();
74 ERR_FAIL_COND(!world_ref.is_valid());
75 RID space = world_ref->get_space();
76 if (area) {
77 PhysicsServer3D::get_singleton()->area_set_space(rid, space);
78 } else {
79 PhysicsServer3D::get_singleton()->body_set_space(rid, space);
80 }
81 }
82
83 _update_pickable();
84 } break;
85
86 case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
87 update_configuration_warnings();
88 } break;
89
90 case NOTIFICATION_TRANSFORM_CHANGED: {
91 if (only_update_transform_changes) {
92 return;
93 }
94
95 if (area) {
96 PhysicsServer3D::get_singleton()->area_set_transform(rid, get_global_transform());
97 } else {
98 PhysicsServer3D::get_singleton()->body_set_state(rid, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
99 }
100
101 _on_transform_changed();
102 } break;
103
104 case NOTIFICATION_VISIBILITY_CHANGED: {
105 _update_pickable();
106 } break;
107
108 case NOTIFICATION_EXIT_WORLD: {
109 bool disabled = !is_enabled();
110
111 if (!disabled || (disable_mode != DISABLE_MODE_REMOVE)) {
112 if (callback_lock > 0) {
113 ERR_PRINT("Removing a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Remove with call_deferred() instead.");
114 } else {
115 if (area) {
116 PhysicsServer3D::get_singleton()->area_set_space(rid, RID());
117 } else {
118 PhysicsServer3D::get_singleton()->body_set_space(rid, RID());
119 }
120 }
121 }
122
123 if (disabled && (disable_mode != DISABLE_MODE_REMOVE)) {
124 _apply_enabled();
125 }
126 } break;
127
128 case NOTIFICATION_DISABLED: {
129 _apply_disabled();
130 } break;
131
132 case NOTIFICATION_ENABLED: {
133 _apply_enabled();
134 } break;
135 }
136}
137
138void CollisionObject3D::set_collision_layer(uint32_t p_layer) {
139 collision_layer = p_layer;
140 if (area) {
141 PhysicsServer3D::get_singleton()->area_set_collision_layer(get_rid(), p_layer);
142 } else {
143 PhysicsServer3D::get_singleton()->body_set_collision_layer(get_rid(), p_layer);
144 }
145}
146
147uint32_t CollisionObject3D::get_collision_layer() const {
148 return collision_layer;
149}
150
151void CollisionObject3D::set_collision_mask(uint32_t p_mask) {
152 collision_mask = p_mask;
153 if (area) {
154 PhysicsServer3D::get_singleton()->area_set_collision_mask(get_rid(), p_mask);
155 } else {
156 PhysicsServer3D::get_singleton()->body_set_collision_mask(get_rid(), p_mask);
157 }
158}
159
160uint32_t CollisionObject3D::get_collision_mask() const {
161 return collision_mask;
162}
163
164void CollisionObject3D::set_collision_layer_value(int p_layer_number, bool p_value) {
165 ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
166 ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
167 uint32_t collision_layer_new = get_collision_layer();
168 if (p_value) {
169 collision_layer_new |= 1 << (p_layer_number - 1);
170 } else {
171 collision_layer_new &= ~(1 << (p_layer_number - 1));
172 }
173 set_collision_layer(collision_layer_new);
174}
175
176bool CollisionObject3D::get_collision_layer_value(int p_layer_number) const {
177 ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
178 ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
179 return get_collision_layer() & (1 << (p_layer_number - 1));
180}
181
182void CollisionObject3D::set_collision_mask_value(int p_layer_number, bool p_value) {
183 ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
184 ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
185 uint32_t mask = get_collision_mask();
186 if (p_value) {
187 mask |= 1 << (p_layer_number - 1);
188 } else {
189 mask &= ~(1 << (p_layer_number - 1));
190 }
191 set_collision_mask(mask);
192}
193
194bool CollisionObject3D::get_collision_mask_value(int p_layer_number) const {
195 ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
196 ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
197 return get_collision_mask() & (1 << (p_layer_number - 1));
198}
199
200void CollisionObject3D::set_collision_priority(real_t p_priority) {
201 collision_priority = p_priority;
202 if (!area) {
203 PhysicsServer3D::get_singleton()->body_set_collision_priority(get_rid(), p_priority);
204 }
205}
206
207real_t CollisionObject3D::get_collision_priority() const {
208 return collision_priority;
209}
210
211void CollisionObject3D::set_disable_mode(DisableMode p_mode) {
212 if (disable_mode == p_mode) {
213 return;
214 }
215
216 bool disabled = is_inside_tree() && !is_enabled();
217
218 if (disabled) {
219 // Cancel previous disable mode.
220 _apply_enabled();
221 }
222
223 disable_mode = p_mode;
224
225 if (disabled) {
226 // Apply new disable mode.
227 _apply_disabled();
228 }
229}
230
231CollisionObject3D::DisableMode CollisionObject3D::get_disable_mode() const {
232 return disable_mode;
233}
234
235void CollisionObject3D::_apply_disabled() {
236 switch (disable_mode) {
237 case DISABLE_MODE_REMOVE: {
238 if (is_inside_tree()) {
239 if (callback_lock > 0) {
240 ERR_PRINT("Disabling a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Disable with call_deferred() instead.");
241 } else {
242 if (area) {
243 PhysicsServer3D::get_singleton()->area_set_space(rid, RID());
244 } else {
245 PhysicsServer3D::get_singleton()->body_set_space(rid, RID());
246 }
247 }
248 }
249 } break;
250
251 case DISABLE_MODE_MAKE_STATIC: {
252 if (!area && (body_mode != PhysicsServer3D::BODY_MODE_STATIC)) {
253 PhysicsServer3D::get_singleton()->body_set_mode(rid, PhysicsServer3D::BODY_MODE_STATIC);
254 }
255 } break;
256
257 case DISABLE_MODE_KEEP_ACTIVE: {
258 // Nothing to do.
259 } break;
260 }
261}
262
263void CollisionObject3D::_apply_enabled() {
264 switch (disable_mode) {
265 case DISABLE_MODE_REMOVE: {
266 if (is_inside_tree()) {
267 RID space = get_world_3d()->get_space();
268 if (area) {
269 PhysicsServer3D::get_singleton()->area_set_space(rid, space);
270 } else {
271 PhysicsServer3D::get_singleton()->body_set_space(rid, space);
272 }
273 }
274 } break;
275
276 case DISABLE_MODE_MAKE_STATIC: {
277 if (!area && (body_mode != PhysicsServer3D::BODY_MODE_STATIC)) {
278 PhysicsServer3D::get_singleton()->body_set_mode(rid, body_mode);
279 }
280 } break;
281
282 case DISABLE_MODE_KEEP_ACTIVE: {
283 // Nothing to do.
284 } break;
285 }
286}
287
288void CollisionObject3D::_input_event_call(Camera3D *p_camera, const Ref<InputEvent> &p_input_event, const Vector3 &p_pos, const Vector3 &p_normal, int p_shape) {
289 GDVIRTUAL_CALL(_input_event, p_camera, p_input_event, p_pos, p_normal, p_shape);
290 emit_signal(SceneStringNames::get_singleton()->input_event, p_camera, p_input_event, p_pos, p_normal, p_shape);
291}
292
293void CollisionObject3D::_mouse_enter() {
294 if (get_script_instance()) {
295 get_script_instance()->call(SceneStringNames::get_singleton()->_mouse_enter);
296 }
297 emit_signal(SceneStringNames::get_singleton()->mouse_entered);
298}
299
300void CollisionObject3D::_mouse_exit() {
301 if (get_script_instance()) {
302 get_script_instance()->call(SceneStringNames::get_singleton()->_mouse_exit);
303 }
304 emit_signal(SceneStringNames::get_singleton()->mouse_exited);
305}
306
307void CollisionObject3D::set_body_mode(PhysicsServer3D::BodyMode p_mode) {
308 ERR_FAIL_COND(area);
309
310 if (body_mode == p_mode) {
311 return;
312 }
313
314 body_mode = p_mode;
315
316 if (is_inside_tree() && !is_enabled() && (disable_mode == DISABLE_MODE_MAKE_STATIC)) {
317 return;
318 }
319
320 PhysicsServer3D::get_singleton()->body_set_mode(rid, p_mode);
321}
322
323void CollisionObject3D::set_only_update_transform_changes(bool p_enable) {
324 only_update_transform_changes = p_enable;
325}
326
327bool CollisionObject3D::is_only_update_transform_changes_enabled() const {
328 return only_update_transform_changes;
329}
330
331void CollisionObject3D::_update_pickable() {
332 if (!is_inside_tree()) {
333 return;
334 }
335
336 bool pickable = ray_pickable && is_visible_in_tree();
337 if (area) {
338 PhysicsServer3D::get_singleton()->area_set_ray_pickable(rid, pickable);
339 } else {
340 PhysicsServer3D::get_singleton()->body_set_ray_pickable(rid, pickable);
341 }
342}
343
344bool CollisionObject3D::_are_collision_shapes_visible() {
345 return is_inside_tree() && get_tree()->is_debugging_collisions_hint() && !Engine::get_singleton()->is_editor_hint();
346}
347
348void CollisionObject3D::_update_shape_data(uint32_t p_owner) {
349 if (_are_collision_shapes_visible()) {
350 if (debug_shapes_to_update.is_empty()) {
351 callable_mp(this, &CollisionObject3D::_update_debug_shapes).call_deferred();
352 }
353 debug_shapes_to_update.insert(p_owner);
354 }
355}
356
357void CollisionObject3D::_shape_changed(const Ref<Shape3D> &p_shape) {
358 for (KeyValue<uint32_t, ShapeData> &E : shapes) {
359 ShapeData &shapedata = E.value;
360 ShapeData::ShapeBase *shape_bases = shapedata.shapes.ptrw();
361 for (int i = 0; i < shapedata.shapes.size(); i++) {
362 ShapeData::ShapeBase &s = shape_bases[i];
363 if (s.shape == p_shape && s.debug_shape.is_valid()) {
364 Ref<Mesh> mesh = s.shape->get_debug_mesh();
365 RS::get_singleton()->instance_set_base(s.debug_shape, mesh->get_rid());
366 }
367 }
368 }
369}
370
371void CollisionObject3D::_update_debug_shapes() {
372 ERR_FAIL_NULL(RenderingServer::get_singleton());
373
374 if (!is_inside_tree()) {
375 debug_shapes_to_update.clear();
376 return;
377 }
378
379 for (const uint32_t &shapedata_idx : debug_shapes_to_update) {
380 if (shapes.has(shapedata_idx)) {
381 ShapeData &shapedata = shapes[shapedata_idx];
382 ShapeData::ShapeBase *shape_bases = shapedata.shapes.ptrw();
383 for (int i = 0; i < shapedata.shapes.size(); i++) {
384 ShapeData::ShapeBase &s = shape_bases[i];
385 if (s.shape.is_null() || shapedata.disabled) {
386 if (s.debug_shape.is_valid()) {
387 RS::get_singleton()->free(s.debug_shape);
388 s.debug_shape = RID();
389 --debug_shapes_count;
390 }
391 continue;
392 }
393
394 if (s.debug_shape.is_null()) {
395 s.debug_shape = RS::get_singleton()->instance_create();
396 RS::get_singleton()->instance_set_scenario(s.debug_shape, get_world_3d()->get_scenario());
397 s.shape->connect_changed(callable_mp(this, &CollisionObject3D::_shape_changed).bind(s.shape), CONNECT_DEFERRED);
398 ++debug_shapes_count;
399 }
400
401 Ref<Mesh> mesh = s.shape->get_debug_mesh();
402 RS::get_singleton()->instance_set_base(s.debug_shape, mesh->get_rid());
403 RS::get_singleton()->instance_set_transform(s.debug_shape, get_global_transform() * shapedata.xform);
404 }
405 }
406 }
407 debug_shapes_to_update.clear();
408}
409
410void CollisionObject3D::_clear_debug_shapes() {
411 ERR_FAIL_NULL(RenderingServer::get_singleton());
412
413 for (KeyValue<uint32_t, ShapeData> &E : shapes) {
414 ShapeData &shapedata = E.value;
415 ShapeData::ShapeBase *shape_bases = shapedata.shapes.ptrw();
416 for (int i = 0; i < shapedata.shapes.size(); i++) {
417 ShapeData::ShapeBase &s = shape_bases[i];
418 if (s.debug_shape.is_valid()) {
419 RS::get_singleton()->free(s.debug_shape);
420 s.debug_shape = RID();
421 if (s.shape.is_valid()) {
422 s.shape->disconnect_changed(callable_mp(this, &CollisionObject3D::_update_shape_data));
423 }
424 }
425 }
426 }
427 debug_shapes_count = 0;
428}
429
430void CollisionObject3D::_on_transform_changed() {
431 if (debug_shapes_count > 0 && !debug_shape_old_transform.is_equal_approx(get_global_transform())) {
432 debug_shape_old_transform = get_global_transform();
433 for (KeyValue<uint32_t, ShapeData> &E : shapes) {
434 ShapeData &shapedata = E.value;
435 if (shapedata.disabled) {
436 continue; // If disabled then there are no debug shapes to update.
437 }
438 const ShapeData::ShapeBase *shape_bases = shapedata.shapes.ptr();
439 for (int i = 0; i < shapedata.shapes.size(); i++) {
440 RS::get_singleton()->instance_set_transform(shape_bases[i].debug_shape, debug_shape_old_transform * shapedata.xform);
441 }
442 }
443 }
444}
445
446void CollisionObject3D::set_ray_pickable(bool p_ray_pickable) {
447 ray_pickable = p_ray_pickable;
448 _update_pickable();
449}
450
451bool CollisionObject3D::is_ray_pickable() const {
452 return ray_pickable;
453}
454
455void CollisionObject3D::_bind_methods() {
456 ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &CollisionObject3D::set_collision_layer);
457 ClassDB::bind_method(D_METHOD("get_collision_layer"), &CollisionObject3D::get_collision_layer);
458 ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &CollisionObject3D::set_collision_mask);
459 ClassDB::bind_method(D_METHOD("get_collision_mask"), &CollisionObject3D::get_collision_mask);
460 ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &CollisionObject3D::set_collision_layer_value);
461 ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &CollisionObject3D::get_collision_layer_value);
462 ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &CollisionObject3D::set_collision_mask_value);
463 ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &CollisionObject3D::get_collision_mask_value);
464 ClassDB::bind_method(D_METHOD("set_collision_priority", "priority"), &CollisionObject3D::set_collision_priority);
465 ClassDB::bind_method(D_METHOD("get_collision_priority"), &CollisionObject3D::get_collision_priority);
466 ClassDB::bind_method(D_METHOD("set_disable_mode", "mode"), &CollisionObject3D::set_disable_mode);
467 ClassDB::bind_method(D_METHOD("get_disable_mode"), &CollisionObject3D::get_disable_mode);
468 ClassDB::bind_method(D_METHOD("set_ray_pickable", "ray_pickable"), &CollisionObject3D::set_ray_pickable);
469 ClassDB::bind_method(D_METHOD("is_ray_pickable"), &CollisionObject3D::is_ray_pickable);
470 ClassDB::bind_method(D_METHOD("set_capture_input_on_drag", "enable"), &CollisionObject3D::set_capture_input_on_drag);
471 ClassDB::bind_method(D_METHOD("get_capture_input_on_drag"), &CollisionObject3D::get_capture_input_on_drag);
472 ClassDB::bind_method(D_METHOD("get_rid"), &CollisionObject3D::get_rid);
473 ClassDB::bind_method(D_METHOD("create_shape_owner", "owner"), &CollisionObject3D::create_shape_owner);
474 ClassDB::bind_method(D_METHOD("remove_shape_owner", "owner_id"), &CollisionObject3D::remove_shape_owner);
475 ClassDB::bind_method(D_METHOD("get_shape_owners"), &CollisionObject3D::_get_shape_owners);
476 ClassDB::bind_method(D_METHOD("shape_owner_set_transform", "owner_id", "transform"), &CollisionObject3D::shape_owner_set_transform);
477 ClassDB::bind_method(D_METHOD("shape_owner_get_transform", "owner_id"), &CollisionObject3D::shape_owner_get_transform);
478 ClassDB::bind_method(D_METHOD("shape_owner_get_owner", "owner_id"), &CollisionObject3D::shape_owner_get_owner);
479 ClassDB::bind_method(D_METHOD("shape_owner_set_disabled", "owner_id", "disabled"), &CollisionObject3D::shape_owner_set_disabled);
480 ClassDB::bind_method(D_METHOD("is_shape_owner_disabled", "owner_id"), &CollisionObject3D::is_shape_owner_disabled);
481 ClassDB::bind_method(D_METHOD("shape_owner_add_shape", "owner_id", "shape"), &CollisionObject3D::shape_owner_add_shape);
482 ClassDB::bind_method(D_METHOD("shape_owner_get_shape_count", "owner_id"), &CollisionObject3D::shape_owner_get_shape_count);
483 ClassDB::bind_method(D_METHOD("shape_owner_get_shape", "owner_id", "shape_id"), &CollisionObject3D::shape_owner_get_shape);
484 ClassDB::bind_method(D_METHOD("shape_owner_get_shape_index", "owner_id", "shape_id"), &CollisionObject3D::shape_owner_get_shape_index);
485 ClassDB::bind_method(D_METHOD("shape_owner_remove_shape", "owner_id", "shape_id"), &CollisionObject3D::shape_owner_remove_shape);
486 ClassDB::bind_method(D_METHOD("shape_owner_clear_shapes", "owner_id"), &CollisionObject3D::shape_owner_clear_shapes);
487 ClassDB::bind_method(D_METHOD("shape_find_owner", "shape_index"), &CollisionObject3D::shape_find_owner);
488
489 GDVIRTUAL_BIND(_input_event, "camera", "event", "position", "normal", "shape_idx");
490 GDVIRTUAL_BIND(_mouse_enter);
491 GDVIRTUAL_BIND(_mouse_exit);
492
493 ADD_SIGNAL(MethodInfo("input_event", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"), PropertyInfo(Variant::VECTOR3, "position"), PropertyInfo(Variant::VECTOR3, "normal"), PropertyInfo(Variant::INT, "shape_idx")));
494 ADD_SIGNAL(MethodInfo("mouse_entered"));
495 ADD_SIGNAL(MethodInfo("mouse_exited"));
496
497 ADD_PROPERTY(PropertyInfo(Variant::INT, "disable_mode", PROPERTY_HINT_ENUM, "Remove,Make Static,Keep Active"), "set_disable_mode", "get_disable_mode");
498
499 ADD_GROUP("Collision", "collision_");
500 ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
501 ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
502 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_priority"), "set_collision_priority", "get_collision_priority");
503
504 ADD_GROUP("Input", "input_");
505 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_ray_pickable"), "set_ray_pickable", "is_ray_pickable");
506 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "input_capture_on_drag"), "set_capture_input_on_drag", "get_capture_input_on_drag");
507
508 BIND_ENUM_CONSTANT(DISABLE_MODE_REMOVE);
509 BIND_ENUM_CONSTANT(DISABLE_MODE_MAKE_STATIC);
510 BIND_ENUM_CONSTANT(DISABLE_MODE_KEEP_ACTIVE);
511}
512
513uint32_t CollisionObject3D::create_shape_owner(Object *p_owner) {
514 ShapeData sd;
515 uint32_t id;
516
517 if (shapes.size() == 0) {
518 id = 0;
519 } else {
520 id = shapes.back()->key() + 1;
521 }
522
523 sd.owner_id = p_owner ? p_owner->get_instance_id() : ObjectID();
524
525 shapes[id] = sd;
526
527 return id;
528}
529
530void CollisionObject3D::remove_shape_owner(uint32_t owner) {
531 ERR_FAIL_COND(!shapes.has(owner));
532
533 shape_owner_clear_shapes(owner);
534
535 shapes.erase(owner);
536}
537
538void CollisionObject3D::shape_owner_set_disabled(uint32_t p_owner, bool p_disabled) {
539 ERR_FAIL_COND(!shapes.has(p_owner));
540
541 ShapeData &sd = shapes[p_owner];
542 if (sd.disabled == p_disabled) {
543 return;
544 }
545 sd.disabled = p_disabled;
546
547 for (int i = 0; i < sd.shapes.size(); i++) {
548 if (area) {
549 PhysicsServer3D::get_singleton()->area_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
550 } else {
551 PhysicsServer3D::get_singleton()->body_set_shape_disabled(rid, sd.shapes[i].index, p_disabled);
552 }
553 }
554 _update_shape_data(p_owner);
555}
556
557bool CollisionObject3D::is_shape_owner_disabled(uint32_t p_owner) const {
558 ERR_FAIL_COND_V(!shapes.has(p_owner), false);
559
560 return shapes[p_owner].disabled;
561}
562
563void CollisionObject3D::get_shape_owners(List<uint32_t> *r_owners) {
564 for (const KeyValue<uint32_t, ShapeData> &E : shapes) {
565 r_owners->push_back(E.key);
566 }
567}
568
569PackedInt32Array CollisionObject3D::_get_shape_owners() {
570 PackedInt32Array ret;
571 for (const KeyValue<uint32_t, ShapeData> &E : shapes) {
572 ret.push_back(E.key);
573 }
574
575 return ret;
576}
577
578void CollisionObject3D::shape_owner_set_transform(uint32_t p_owner, const Transform3D &p_transform) {
579 ERR_FAIL_COND(!shapes.has(p_owner));
580
581 ShapeData &sd = shapes[p_owner];
582 sd.xform = p_transform;
583 for (int i = 0; i < sd.shapes.size(); i++) {
584 if (area) {
585 PhysicsServer3D::get_singleton()->area_set_shape_transform(rid, sd.shapes[i].index, p_transform);
586 } else {
587 PhysicsServer3D::get_singleton()->body_set_shape_transform(rid, sd.shapes[i].index, p_transform);
588 }
589 }
590
591 _update_shape_data(p_owner);
592}
593Transform3D CollisionObject3D::shape_owner_get_transform(uint32_t p_owner) const {
594 ERR_FAIL_COND_V(!shapes.has(p_owner), Transform3D());
595
596 return shapes[p_owner].xform;
597}
598
599Object *CollisionObject3D::shape_owner_get_owner(uint32_t p_owner) const {
600 ERR_FAIL_COND_V(!shapes.has(p_owner), nullptr);
601
602 return ObjectDB::get_instance(shapes[p_owner].owner_id);
603}
604
605void CollisionObject3D::shape_owner_add_shape(uint32_t p_owner, const Ref<Shape3D> &p_shape) {
606 ERR_FAIL_COND(!shapes.has(p_owner));
607 ERR_FAIL_COND(p_shape.is_null());
608
609 ShapeData &sd = shapes[p_owner];
610 ShapeData::ShapeBase s;
611 s.index = total_subshapes;
612 s.shape = p_shape;
613
614 if (area) {
615 PhysicsServer3D::get_singleton()->area_add_shape(rid, p_shape->get_rid(), sd.xform, sd.disabled);
616 } else {
617 PhysicsServer3D::get_singleton()->body_add_shape(rid, p_shape->get_rid(), sd.xform, sd.disabled);
618 }
619 sd.shapes.push_back(s);
620
621 total_subshapes++;
622
623 _update_shape_data(p_owner);
624}
625
626int CollisionObject3D::shape_owner_get_shape_count(uint32_t p_owner) const {
627 ERR_FAIL_COND_V(!shapes.has(p_owner), 0);
628
629 return shapes[p_owner].shapes.size();
630}
631
632Ref<Shape3D> CollisionObject3D::shape_owner_get_shape(uint32_t p_owner, int p_shape) const {
633 ERR_FAIL_COND_V(!shapes.has(p_owner), Ref<Shape3D>());
634 ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), Ref<Shape3D>());
635
636 return shapes[p_owner].shapes[p_shape].shape;
637}
638
639int CollisionObject3D::shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const {
640 ERR_FAIL_COND_V(!shapes.has(p_owner), -1);
641 ERR_FAIL_INDEX_V(p_shape, shapes[p_owner].shapes.size(), -1);
642
643 return shapes[p_owner].shapes[p_shape].index;
644}
645
646void CollisionObject3D::shape_owner_remove_shape(uint32_t p_owner, int p_shape) {
647 ERR_FAIL_NULL(RenderingServer::get_singleton());
648 ERR_FAIL_COND(!shapes.has(p_owner));
649 ERR_FAIL_INDEX(p_shape, shapes[p_owner].shapes.size());
650
651 ShapeData::ShapeBase &s = shapes[p_owner].shapes.write[p_shape];
652 int index_to_remove = s.index;
653
654 if (area) {
655 PhysicsServer3D::get_singleton()->area_remove_shape(rid, index_to_remove);
656 } else {
657 PhysicsServer3D::get_singleton()->body_remove_shape(rid, index_to_remove);
658 }
659
660 if (s.debug_shape.is_valid()) {
661 RS::get_singleton()->free(s.debug_shape);
662 if (s.shape.is_valid()) {
663 s.shape->disconnect_changed(callable_mp(this, &CollisionObject3D::_shape_changed));
664 }
665 --debug_shapes_count;
666 }
667
668 shapes[p_owner].shapes.remove_at(p_shape);
669
670 for (KeyValue<uint32_t, ShapeData> &E : shapes) {
671 for (int i = 0; i < E.value.shapes.size(); i++) {
672 if (E.value.shapes[i].index > index_to_remove) {
673 E.value.shapes.write[i].index -= 1;
674 }
675 }
676 }
677
678 total_subshapes--;
679}
680
681void CollisionObject3D::shape_owner_clear_shapes(uint32_t p_owner) {
682 ERR_FAIL_COND(!shapes.has(p_owner));
683
684 while (shape_owner_get_shape_count(p_owner) > 0) {
685 shape_owner_remove_shape(p_owner, 0);
686 }
687}
688
689uint32_t CollisionObject3D::shape_find_owner(int p_shape_index) const {
690 ERR_FAIL_INDEX_V(p_shape_index, total_subshapes, UINT32_MAX);
691
692 for (const KeyValue<uint32_t, ShapeData> &E : shapes) {
693 for (int i = 0; i < E.value.shapes.size(); i++) {
694 if (E.value.shapes[i].index == p_shape_index) {
695 return E.key;
696 }
697 }
698 }
699
700 //in theory it should be unreachable
701 ERR_FAIL_V_MSG(UINT32_MAX, "Can't find owner for shape index " + itos(p_shape_index) + ".");
702}
703
704CollisionObject3D::CollisionObject3D(RID p_rid, bool p_area) {
705 rid = p_rid;
706 area = p_area;
707 set_notify_transform(true);
708
709 if (p_area) {
710 PhysicsServer3D::get_singleton()->area_attach_object_instance_id(rid, get_instance_id());
711 } else {
712 PhysicsServer3D::get_singleton()->body_attach_object_instance_id(rid, get_instance_id());
713 PhysicsServer3D::get_singleton()->body_set_mode(rid, body_mode);
714 }
715}
716
717void CollisionObject3D::set_capture_input_on_drag(bool p_capture) {
718 capture_input_on_drag = p_capture;
719}
720
721bool CollisionObject3D::get_capture_input_on_drag() const {
722 return capture_input_on_drag;
723}
724
725PackedStringArray CollisionObject3D::get_configuration_warnings() const {
726 PackedStringArray warnings = Node::get_configuration_warnings();
727
728 if (shapes.is_empty()) {
729 warnings.push_back(RTR("This node has no shape, so it can't collide or interact with other objects.\nConsider adding a CollisionShape3D or CollisionPolygon3D as a child to define its shape."));
730 }
731
732 Vector3 scale = get_transform().get_basis().get_scale();
733 if (!(Math::is_zero_approx(scale.x - scale.y) && Math::is_zero_approx(scale.y - scale.z))) {
734 warnings.push_back(RTR("With a non-uniform scale this node will probably not function as expected.\nPlease make its scale uniform (i.e. the same on all axes), and change the size in children collision shapes instead."));
735 }
736
737 return warnings;
738}
739
740CollisionObject3D::CollisionObject3D() {
741 set_notify_transform(true);
742 //owner=
743
744 //set_transform_notify(true);
745}
746
747CollisionObject3D::~CollisionObject3D() {
748 ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
749 PhysicsServer3D::get_singleton()->free(rid);
750}
751