Debian Codegraph @

scene/3d/physics/rigid_body_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* rigid_body_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 "rigid_body_3d.h"
32
33 void RigidBody3D::_body_enter_tree(ObjectID p_id) {
34 Object *obj = ObjectDB::get_instance(p_id);
35 Node *node = Object::cast_to<Node>(obj);
36 ERR_FAIL_NULL(node);
37 ERR_FAIL_NULL(contact_monitor);
38 HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(p_id);
39 ERR_FAIL_COND(!E);
40 ERR_FAIL_COND(E->value.in_tree);
41
42 E->value.in_tree = true;
43
44 contact_monitor->locked = true;
45
46 emit_signal(SceneStringName(body_entered), node);
47
48 for (int i = 0; i < E->value.shapes.size(); i++) {
49 emit_signal(SceneStringName(body_shape_entered), E->value.rid, node, E->value.shapes[i].body_shape, E->value.shapes[i].local_shape);
50 }
51
52 contact_monitor->locked = false;
53 }
54
55 void RigidBody3D::_body_exit_tree(ObjectID p_id) {
56 Object *obj = ObjectDB::get_instance(p_id);
57 Node *node = Object::cast_to<Node>(obj);
58 ERR_FAIL_NULL(node);
59 ERR_FAIL_NULL(contact_monitor);
60 HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(p_id);
61 ERR_FAIL_COND(!E);
62 ERR_FAIL_COND(!E->value.in_tree);
63 E->value.in_tree = false;
64
65 contact_monitor->locked = true;
66
67 emit_signal(SceneStringName(body_exited), node);
68
69 for (int i = 0; i < E->value.shapes.size(); i++) {
70 emit_signal(SceneStringName(body_shape_exited), E->value.rid, node, E->value.shapes[i].body_shape, E->value.shapes[i].local_shape);
71 }
72
73 contact_monitor->locked = false;
74 }
75
76 void RigidBody3D::_body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_local_shape) {
77 bool body_in = p_status == 1;
78 ObjectID objid = p_instance;
79
80 Object *obj = ObjectDB::get_instance(objid);
81 Node *node = Object::cast_to<Node>(obj);
82
83 ERR_FAIL_NULL(contact_monitor);
84 HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(objid);
85
86 ERR_FAIL_COND(!body_in && !E);
87
88 if (body_in) {
89 if (!E) {
90 E = contact_monitor->body_map.insert(objid, BodyState());
91 E->value.rid = p_body;
92 //E->value.rc=0;
93 E->value.in_tree = node && node->is_inside_tree();
94 if (node) {
95 node->connect(SceneStringName(tree_entered), callable_mp(this, &RigidBody3D::_body_enter_tree).bind(objid));
96 node->connect(SceneStringName(tree_exiting), callable_mp(this, &RigidBody3D::_body_exit_tree).bind(objid));
97 if (E->value.in_tree) {
98 emit_signal(SceneStringName(body_entered), node);
99 }
100 }
101 }
102 //E->value.rc++;
103 if (node) {
104 E->value.shapes.insert(ShapePair(p_body_shape, p_local_shape));
105 }
106
107 if (E->value.in_tree) {
108 emit_signal(SceneStringName(body_shape_entered), p_body, node, p_body_shape, p_local_shape);
109 }
110
111 } else {
112 //E->value.rc--;
113
114 if (node) {
115 E->value.shapes.erase(ShapePair(p_body_shape, p_local_shape));
116 }
117
118 bool in_tree = E->value.in_tree;
119
120 if (E->value.shapes.is_empty()) {
121 if (node) {
122 node->disconnect(SceneStringName(tree_entered), callable_mp(this, &RigidBody3D::_body_enter_tree));
123 node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &RigidBody3D::_body_exit_tree));
124 if (in_tree) {
125 emit_signal(SceneStringName(body_exited), node);
126 }
127 }
128
129 contact_monitor->body_map.remove(E);
130 }
131 if (node && in_tree) {
132 emit_signal(SceneStringName(body_shape_exited), p_body, obj, p_body_shape, p_local_shape);
133 }
134 }
135 }
136
137 struct _RigidBodyInOut {
138 RID rid;
139 ObjectID id;
140 int shape = 0;
141 int local_shape = 0;
142 };
143
144 void RigidBody3D::_sync_body_state(PhysicsDirectBodyState3D *p_state) {
145 set_ignore_transform_notification(true);
146 set_global_transform(p_state->get_transform());
147 set_ignore_transform_notification(false);
148
149 linear_velocity = p_state->get_linear_velocity();
150 angular_velocity = p_state->get_angular_velocity();
151
152 inverse_inertia_tensor = p_state->get_inverse_inertia_tensor();
153
154 contact_count = p_state->get_contact_count();
155
156 if (sleeping != p_state->is_sleeping()) {
157 sleeping = p_state->is_sleeping();
158 emit_signal(SceneStringName(sleeping_state_changed));
159 }
160 }
161
162 void RigidBody3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
163 lock_callback();
164
165 if (GDVIRTUAL_IS_OVERRIDDEN(_integrate_forces)) {
166 _sync_body_state(p_state);
167
168 Transform3D old_transform = get_global_transform();
169 GDVIRTUAL_CALL(_integrate_forces, p_state);
170 Transform3D new_transform = get_global_transform();
171
172 if (new_transform != old_transform) {
173 // Update the physics server with the new transform, to prevent it from being overwritten at the sync below.
174 PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_TRANSFORM, new_transform);
175 }
176 }
177
178 _sync_body_state(p_state);
179 _on_transform_changed();
180
181 if (contact_monitor) {
182 contact_monitor->locked = true;
183
184 //untag all
185 int rc = 0;
186 for (KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
187 for (int i = 0; i < E.value.shapes.size(); i++) {
188 E.value.shapes[i].tagged = false;
189 rc++;
190 }
191 }
192
193 _RigidBodyInOut *toadd = (_RigidBodyInOut *)alloca(p_state->get_contact_count() * sizeof(_RigidBodyInOut));
194 int toadd_count = 0;
195 RigidBody3D_RemoveAction *toremove = (RigidBody3D_RemoveAction *)alloca(rc * sizeof(RigidBody3D_RemoveAction));
196 int toremove_count = 0;
197
198 //put the ones to add
199
200 for (int i = 0; i < p_state->get_contact_count(); i++) {
201 RID col_rid = p_state->get_contact_collider(i);
202 ObjectID col_obj = p_state->get_contact_collider_id(i);
203 int local_shape = p_state->get_contact_local_shape(i);
204 int col_shape = p_state->get_contact_collider_shape(i);
205
206 HashMap<ObjectID, BodyState>::Iterator E = contact_monitor->body_map.find(col_obj);
207 if (!E) {
208 toadd[toadd_count].rid = col_rid;
209 toadd[toadd_count].local_shape = local_shape;
210 toadd[toadd_count].id = col_obj;
211 toadd[toadd_count].shape = col_shape;
212 toadd_count++;
213 continue;
214 }
215
216 ShapePair sp(col_shape, local_shape);
217 int idx = E->value.shapes.find(sp);
218 if (idx == -1) {
219 toadd[toadd_count].rid = col_rid;
220 toadd[toadd_count].local_shape = local_shape;
221 toadd[toadd_count].id = col_obj;
222 toadd[toadd_count].shape = col_shape;
223 toadd_count++;
224 continue;
225 }
226
227 E->value.shapes[idx].tagged = true;
228 }
229
230 //put the ones to remove
231
232 for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
233 for (int i = 0; i < E.value.shapes.size(); i++) {
234 if (!E.value.shapes[i].tagged) {
235 toremove[toremove_count].rid = E.value.rid;
236 toremove[toremove_count].body_id = E.key;
237 toremove[toremove_count].pair = E.value.shapes[i];
238 toremove_count++;
239 }
240 }
241 }
242
243 //process removals
244
245 for (int i = 0; i < toremove_count; i++) {
246 _body_inout(0, toremove[i].rid, toremove[i].body_id, toremove[i].pair.body_shape, toremove[i].pair.local_shape);
247 }
248
249 //process additions
250
251 for (int i = 0; i < toadd_count; i++) {
252 _body_inout(1, toadd[i].rid, toadd[i].id, toadd[i].shape, toadd[i].local_shape);
253 }
254
255 contact_monitor->locked = false;
256 }
257
258 unlock_callback();
259 }
260
261 void RigidBody3D::_notification(int p_what) {
262 #ifdef TOOLS_ENABLED
263 switch (p_what) {
264 case NOTIFICATION_ENTER_TREE: {
265 if (Engine::get_singleton()->is_editor_hint()) {
266 set_notify_local_transform(true); // Used for warnings and only in editor.
267 }
268 } break;
269
270 case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
271 update_configuration_warnings();
272 } break;
273 }
274 #endif
275 }
276
277 void RigidBody3D::_apply_body_mode() {
278 if (freeze) {
279 switch (freeze_mode) {
280 case FREEZE_MODE_STATIC: {
281 set_body_mode(PhysicsServer3D::BODY_MODE_STATIC);
282 } break;
283 case FREEZE_MODE_KINEMATIC: {
284 set_body_mode(PhysicsServer3D::BODY_MODE_KINEMATIC);
285 } break;
286 }
287 } else if (lock_rotation) {
288 set_body_mode(PhysicsServer3D::BODY_MODE_RIGID_LINEAR);
289 } else {
290 set_body_mode(PhysicsServer3D::BODY_MODE_RIGID);
291 }
292 }
293
294 void RigidBody3D::set_lock_rotation_enabled(bool p_lock_rotation) {
295 if (p_lock_rotation == lock_rotation) {
296 return;
297 }
298
299 lock_rotation = p_lock_rotation;
300 _apply_body_mode();
301 }
302
303 bool RigidBody3D::is_lock_rotation_enabled() const {
304 return lock_rotation;
305 }
306
307 void RigidBody3D::set_freeze_enabled(bool p_freeze) {
308 if (p_freeze == freeze) {
309 return;
310 }
311
312 freeze = p_freeze;
313 _apply_body_mode();
314 }
315
316 bool RigidBody3D::is_freeze_enabled() const {
317 return freeze;
318 }
319
320 void RigidBody3D::set_freeze_mode(FreezeMode p_freeze_mode) {
321 if (p_freeze_mode == freeze_mode) {
322 return;
323 }
324
325 freeze_mode = p_freeze_mode;
326 _apply_body_mode();
327 }
328
329 RigidBody3D::FreezeMode RigidBody3D::get_freeze_mode() const {
330 return freeze_mode;
331 }
332
333 void RigidBody3D::set_mass(real_t p_mass) {
334 ERR_FAIL_COND(p_mass <= 0);
335 mass = p_mass;
336 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_MASS, mass);
337 }
338
339 real_t RigidBody3D::get_mass() const {
340 return mass;
341 }
342
343 void RigidBody3D::set_inertia(const Vector3 &p_inertia) {
344 ERR_FAIL_COND(p_inertia.x < 0);
345 ERR_FAIL_COND(p_inertia.y < 0);
346 ERR_FAIL_COND(p_inertia.z < 0);
347
348 inertia = p_inertia;
349 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_INERTIA, inertia);
350 }
351
352 const Vector3 &RigidBody3D::get_inertia() const {
353 return inertia;
354 }
355
356 void RigidBody3D::set_center_of_mass_mode(CenterOfMassMode p_mode) {
357 if (center_of_mass_mode == p_mode) {
358 return;
359 }
360
361 center_of_mass_mode = p_mode;
362
363 switch (center_of_mass_mode) {
364 case CENTER_OF_MASS_MODE_AUTO: {
365 center_of_mass = Vector3();
366 PhysicsServer3D::get_singleton()->body_reset_mass_properties(get_rid());
367 if (inertia != Vector3()) {
368 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_INERTIA, inertia);
369 }
370 } break;
371
372 case CENTER_OF_MASS_MODE_CUSTOM: {
373 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_CENTER_OF_MASS, center_of_mass);
374 } break;
375 }
376
377 notify_property_list_changed();
378 }
379
380 RigidBody3D::CenterOfMassMode RigidBody3D::get_center_of_mass_mode() const {
381 return center_of_mass_mode;
382 }
383
384 void RigidBody3D::set_center_of_mass(const Vector3 &p_center_of_mass) {
385 if (center_of_mass == p_center_of_mass) {
386 return;
387 }
388
389 ERR_FAIL_COND(center_of_mass_mode != CENTER_OF_MASS_MODE_CUSTOM);
390 center_of_mass = p_center_of_mass;
391
392 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_CENTER_OF_MASS, center_of_mass);
393 }
394
395 const Vector3 &RigidBody3D::get_center_of_mass() const {
396 return center_of_mass;
397 }
398
399 void RigidBody3D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
400 if (physics_material_override.is_valid()) {
401 physics_material_override->disconnect_changed(callable_mp(this, &RigidBody3D::_reload_physics_characteristics));
402 }
403
404 physics_material_override = p_physics_material_override;
405
406 if (physics_material_override.is_valid()) {
407 physics_material_override->connect_changed(callable_mp(this, &RigidBody3D::_reload_physics_characteristics));
408 }
409 _reload_physics_characteristics();
410 }
411
412 Ref<PhysicsMaterial> RigidBody3D::get_physics_material_override() const {
413 return physics_material_override;
414 }
415
416 void RigidBody3D::set_gravity_scale(real_t p_gravity_scale) {
417 gravity_scale = p_gravity_scale;
418 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE, gravity_scale);
419 }
420
421 real_t RigidBody3D::get_gravity_scale() const {
422 return gravity_scale;
423 }
424
425 void RigidBody3D::set_linear_damp_mode(DampMode p_mode) {
426 linear_damp_mode = p_mode;
427 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP_MODE, linear_damp_mode);
428 }
429
430 RigidBody3D::DampMode RigidBody3D::get_linear_damp_mode() const {
431 return linear_damp_mode;
432 }
433
434 void RigidBody3D::set_angular_damp_mode(DampMode p_mode) {
435 angular_damp_mode = p_mode;
436 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP_MODE, angular_damp_mode);
437 }
438
439 RigidBody3D::DampMode RigidBody3D::get_angular_damp_mode() const {
440 return angular_damp_mode;
441 }
442
443 void RigidBody3D::set_linear_damp(real_t p_linear_damp) {
444 ERR_FAIL_COND(p_linear_damp < 0.0);
445 linear_damp = p_linear_damp;
446 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_LINEAR_DAMP, linear_damp);
447 }
448
449 real_t RigidBody3D::get_linear_damp() const {
450 return linear_damp;
451 }
452
453 void RigidBody3D::set_angular_damp(real_t p_angular_damp) {
454 ERR_FAIL_COND(p_angular_damp < 0.0);
455 angular_damp = p_angular_damp;
456 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP, angular_damp);
457 }
458
459 real_t RigidBody3D::get_angular_damp() const {
460 return angular_damp;
461 }
462
463 void RigidBody3D::set_axis_velocity(const Vector3 &p_axis) {
464 Vector3 axis = p_axis.normalized();
465 linear_velocity -= axis * axis.dot(linear_velocity);
466 linear_velocity += p_axis;
467 PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
468 }
469
470 void RigidBody3D::set_linear_velocity(const Vector3 &p_velocity) {
471 linear_velocity = p_velocity;
472 PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
473 }
474
475 Vector3 RigidBody3D::get_linear_velocity() const {
476 return linear_velocity;
477 }
478
479 void RigidBody3D::set_angular_velocity(const Vector3 &p_velocity) {
480 angular_velocity = p_velocity;
481 PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY, angular_velocity);
482 }
483
484 Vector3 RigidBody3D::get_angular_velocity() const {
485 return angular_velocity;
486 }
487
488 Basis RigidBody3D::get_inverse_inertia_tensor() const {
489 return inverse_inertia_tensor;
490 }
491
492 void RigidBody3D::set_use_custom_integrator(bool p_enable) {
493 if (custom_integrator == p_enable) {
494 return;
495 }
496
497 custom_integrator = p_enable;
498 PhysicsServer3D::get_singleton()->body_set_omit_force_integration(get_rid(), p_enable);
499 }
500
501 bool RigidBody3D::is_using_custom_integrator() {
502 return custom_integrator;
503 }
504
505 void RigidBody3D::set_sleeping(bool p_sleeping) {
506 sleeping = p_sleeping;
507 PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_SLEEPING, sleeping);
508 }
509
510 void RigidBody3D::set_can_sleep(bool p_active) {
511 can_sleep = p_active;
512 PhysicsServer3D::get_singleton()->body_set_state(get_rid(), PhysicsServer3D::BODY_STATE_CAN_SLEEP, p_active);
513 }
514
515 bool RigidBody3D::is_able_to_sleep() const {
516 return can_sleep;
517 }
518
519 bool RigidBody3D::is_sleeping() const {
520 return sleeping;
521 }
522
523 void RigidBody3D::set_max_contacts_reported(int p_amount) {
524 ERR_FAIL_INDEX_MSG(p_amount, MAX_CONTACTS_REPORTED_3D_MAX, "Max contacts reported allocates memory (about 80 bytes each), and therefore must not be set too high.");
525 max_contacts_reported = p_amount;
526 PhysicsServer3D::get_singleton()->body_set_max_contacts_reported(get_rid(), p_amount);
527 }
528
529 int RigidBody3D::get_max_contacts_reported() const {
530 return max_contacts_reported;
531 }
532
533 int RigidBody3D::get_contact_count() const {
534 return contact_count;
535 }
536
537 void RigidBody3D::apply_central_impulse(const Vector3 &p_impulse) {
538 PhysicsServer3D::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse);
539 }
540
541 void RigidBody3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) {
542 PhysicsServer3D *singleton = PhysicsServer3D::get_singleton();
543 singleton->body_apply_impulse(get_rid(), p_impulse, p_position);
544 }
545
546 void RigidBody3D::apply_torque_impulse(const Vector3 &p_impulse) {
547 PhysicsServer3D::get_singleton()->body_apply_torque_impulse(get_rid(), p_impulse);
548 }
549
550 void RigidBody3D::apply_central_force(const Vector3 &p_force) {
551 PhysicsServer3D::get_singleton()->body_apply_central_force(get_rid(), p_force);
552 }
553
554 void RigidBody3D::apply_force(const Vector3 &p_force, const Vector3 &p_position) {
555 PhysicsServer3D *singleton = PhysicsServer3D::get_singleton();
556 singleton->body_apply_force(get_rid(), p_force, p_position);
557 }
558
559 void RigidBody3D::apply_torque(const Vector3 &p_torque) {
560 PhysicsServer3D::get_singleton()->body_apply_torque(get_rid(), p_torque);
561 }
562
563 void RigidBody3D::add_constant_central_force(const Vector3 &p_force) {
564 PhysicsServer3D::get_singleton()->body_add_constant_central_force(get_rid(), p_force);
565 }
566
567 void RigidBody3D::add_constant_force(const Vector3 &p_force, const Vector3 &p_position) {
568 PhysicsServer3D *singleton = PhysicsServer3D::get_singleton();
569 singleton->body_add_constant_force(get_rid(), p_force, p_position);
570 }
571
572 void RigidBody3D::add_constant_torque(const Vector3 &p_torque) {
573 PhysicsServer3D::get_singleton()->body_add_constant_torque(get_rid(), p_torque);
574 }
575
576 void RigidBody3D::set_constant_force(const Vector3 &p_force) {
577 PhysicsServer3D::get_singleton()->body_set_constant_force(get_rid(), p_force);
578 }
579
580 Vector3 RigidBody3D::get_constant_force() const {
581 return PhysicsServer3D::get_singleton()->body_get_constant_force(get_rid());
582 }
583
584 void RigidBody3D::set_constant_torque(const Vector3 &p_torque) {
585 PhysicsServer3D::get_singleton()->body_set_constant_torque(get_rid(), p_torque);
586 }
587
588 Vector3 RigidBody3D::get_constant_torque() const {
589 return PhysicsServer3D::get_singleton()->body_get_constant_torque(get_rid());
590 }
591
592 void RigidBody3D::set_use_continuous_collision_detection(bool p_enable) {
593 ccd = p_enable;
594 PhysicsServer3D::get_singleton()->body_set_enable_continuous_collision_detection(get_rid(), p_enable);
595 }
596
597 bool RigidBody3D::is_using_continuous_collision_detection() const {
598 return ccd;
599 }
600
601 void RigidBody3D::set_contact_monitor(bool p_enabled) {
602 if (p_enabled == is_contact_monitor_enabled()) {
603 return;
604 }
605
606 if (!p_enabled) {
607 ERR_FAIL_COND_MSG(contact_monitor->locked, "Can't disable contact monitoring during in/out callback. Use call_deferred(\"set_contact_monitor\", false) instead.");
608
609 for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
610 //clean up mess
611 Object *obj = ObjectDB::get_instance(E.key);
612 Node *node = Object::cast_to<Node>(obj);
613
614 if (node) {
615 node->disconnect(SceneStringName(tree_entered), callable_mp(this, &RigidBody3D::_body_enter_tree));
616 node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &RigidBody3D::_body_exit_tree));
617 }
618 }
619
620 memdelete(contact_monitor);
621 contact_monitor = nullptr;
622 } else {
623 contact_monitor = memnew(ContactMonitor);
624 contact_monitor->locked = false;
625 }
626
627 notify_property_list_changed();
628 }
629
630 bool RigidBody3D::is_contact_monitor_enabled() const {
631 return contact_monitor != nullptr;
632 }
633
634 TypedArray<Node3D> RigidBody3D::get_colliding_bodies() const {
635 ERR_FAIL_NULL_V(contact_monitor, TypedArray<Node3D>());
636
637 TypedArray<Node3D> ret;
638 ret.resize(contact_monitor->body_map.size());
639 int idx = 0;
640 for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
641 Object *obj = ObjectDB::get_instance(E.key);
642 if (!obj) {
643 ret.resize(ret.size() - 1); //ops
644 } else {
645 ret[idx++] = obj;
646 }
647 }
648
649 return ret;
650 }
651
652 void RigidBody3D::_reload_physics_characteristics() {
653 if (physics_material_override.is_null()) {
654 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, 0);
655 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, 1);
656 } else {
657 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
658 PhysicsServer3D::get_singleton()->body_set_param(get_rid(), PhysicsServer3D::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
659 }
660 }
661
662 PackedStringArray RigidBody3D::get_configuration_warnings() const {
663 PackedStringArray warnings = PhysicsBody3D::get_configuration_warnings();
664
665 Vector3 scale = get_transform().get_basis().get_scale();
666 if (Math::abs(scale.x - 1.0) > 0.05 || Math::abs(scale.y - 1.0) > 0.05 || Math::abs(scale.z - 1.0) > 0.05) {
667 warnings.push_back(RTR("Scale changes to RigidBody3D will be overridden by the physics engine when running.\nPlease change the size in children collision shapes instead."));
668 }
669
670 return warnings;
671 }
672
673 void RigidBody3D::_bind_methods() {
674 ClassDB::bind_method(D_METHOD("set_mass", "mass"), &RigidBody3D::set_mass);
675 ClassDB::bind_method(D_METHOD("get_mass"), &RigidBody3D::get_mass);
676
677 ClassDB::bind_method(D_METHOD("set_inertia", "inertia"), &RigidBody3D::set_inertia);
678 ClassDB::bind_method(D_METHOD("get_inertia"), &RigidBody3D::get_inertia);
679
680 ClassDB::bind_method(D_METHOD("set_center_of_mass_mode", "mode"), &RigidBody3D::set_center_of_mass_mode);
681 ClassDB::bind_method(D_METHOD("get_center_of_mass_mode"), &RigidBody3D::get_center_of_mass_mode);
682
683 ClassDB::bind_method(D_METHOD("set_center_of_mass", "center_of_mass"), &RigidBody3D::set_center_of_mass);
684 ClassDB::bind_method(D_METHOD("get_center_of_mass"), &RigidBody3D::get_center_of_mass);
685
686 ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &RigidBody3D::set_physics_material_override);
687 ClassDB::bind_method(D_METHOD("get_physics_material_override"), &RigidBody3D::get_physics_material_override);
688
689 ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &RigidBody3D::set_linear_velocity);
690 ClassDB::bind_method(D_METHOD("get_linear_velocity"), &RigidBody3D::get_linear_velocity);
691
692 ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &RigidBody3D::set_angular_velocity);
693 ClassDB::bind_method(D_METHOD("get_angular_velocity"), &RigidBody3D::get_angular_velocity);
694
695 ClassDB::bind_method(D_METHOD("get_inverse_inertia_tensor"), &RigidBody3D::get_inverse_inertia_tensor);
696
697 ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &RigidBody3D::set_gravity_scale);
698 ClassDB::bind_method(D_METHOD("get_gravity_scale"), &RigidBody3D::get_gravity_scale);
699
700 ClassDB::bind_method(D_METHOD("set_linear_damp_mode", "linear_damp_mode"), &RigidBody3D::set_linear_damp_mode);
701 ClassDB::bind_method(D_METHOD("get_linear_damp_mode"), &RigidBody3D::get_linear_damp_mode);
702
703 ClassDB::bind_method(D_METHOD("set_angular_damp_mode", "angular_damp_mode"), &RigidBody3D::set_angular_damp_mode);
704 ClassDB::bind_method(D_METHOD("get_angular_damp_mode"), &RigidBody3D::get_angular_damp_mode);
705
706 ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &RigidBody3D::set_linear_damp);
707 ClassDB::bind_method(D_METHOD("get_linear_damp"), &RigidBody3D::get_linear_damp);
708
709 ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &RigidBody3D::set_angular_damp);
710 ClassDB::bind_method(D_METHOD("get_angular_damp"), &RigidBody3D::get_angular_damp);
711
712 ClassDB::bind_method(D_METHOD("set_max_contacts_reported", "amount"), &RigidBody3D::set_max_contacts_reported);
713 ClassDB::bind_method(D_METHOD("get_max_contacts_reported"), &RigidBody3D::get_max_contacts_reported);
714 ClassDB::bind_method(D_METHOD("get_contact_count"), &RigidBody3D::get_contact_count);
715
716 ClassDB::bind_method(D_METHOD("set_use_custom_integrator", "enable"), &RigidBody3D::set_use_custom_integrator);
717 ClassDB::bind_method(D_METHOD("is_using_custom_integrator"), &RigidBody3D::is_using_custom_integrator);
718
719 ClassDB::bind_method(D_METHOD("set_contact_monitor", "enabled"), &RigidBody3D::set_contact_monitor);
720 ClassDB::bind_method(D_METHOD("is_contact_monitor_enabled"), &RigidBody3D::is_contact_monitor_enabled);
721
722 ClassDB::bind_method(D_METHOD("set_use_continuous_collision_detection", "enable"), &RigidBody3D::set_use_continuous_collision_detection);
723 ClassDB::bind_method(D_METHOD("is_using_continuous_collision_detection"), &RigidBody3D::is_using_continuous_collision_detection);
724
725 ClassDB::bind_method(D_METHOD("set_axis_velocity", "axis_velocity"), &RigidBody3D::set_axis_velocity);
726
727 ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &RigidBody3D::apply_central_impulse);
728 ClassDB::bind_method(D_METHOD("apply_impulse", "impulse", "position"), &RigidBody3D::apply_impulse, Vector3());
729 ClassDB::bind_method(D_METHOD("apply_torque_impulse", "impulse"), &RigidBody3D::apply_torque_impulse);
730
731 ClassDB::bind_method(D_METHOD("apply_central_force", "force"), &RigidBody3D::apply_central_force);
732 ClassDB::bind_method(D_METHOD("apply_force", "force", "position"), &RigidBody3D::apply_force, Vector3());
733 ClassDB::bind_method(D_METHOD("apply_torque", "torque"), &RigidBody3D::apply_torque);
734
735 ClassDB::bind_method(D_METHOD("add_constant_central_force", "force"), &RigidBody3D::add_constant_central_force);
736 ClassDB::bind_method(D_METHOD("add_constant_force", "force", "position"), &RigidBody3D::add_constant_force, Vector3());
737 ClassDB::bind_method(D_METHOD("add_constant_torque", "torque"), &RigidBody3D::add_constant_torque);
738
739 ClassDB::bind_method(D_METHOD("set_constant_force", "force"), &RigidBody3D::set_constant_force);
740 ClassDB::bind_method(D_METHOD("get_constant_force"), &RigidBody3D::get_constant_force);
741
742 ClassDB::bind_method(D_METHOD("set_constant_torque", "torque"), &RigidBody3D::set_constant_torque);
743 ClassDB::bind_method(D_METHOD("get_constant_torque"), &RigidBody3D::get_constant_torque);
744
745 ClassDB::bind_method(D_METHOD("set_sleeping", "sleeping"), &RigidBody3D::set_sleeping);
746 ClassDB::bind_method(D_METHOD("is_sleeping"), &RigidBody3D::is_sleeping);
747
748 ClassDB::bind_method(D_METHOD("set_can_sleep", "able_to_sleep"), &RigidBody3D::set_can_sleep);
749 ClassDB::bind_method(D_METHOD("is_able_to_sleep"), &RigidBody3D::is_able_to_sleep);
750
751 ClassDB::bind_method(D_METHOD("set_lock_rotation_enabled", "lock_rotation"), &RigidBody3D::set_lock_rotation_enabled);
752 ClassDB::bind_method(D_METHOD("is_lock_rotation_enabled"), &RigidBody3D::is_lock_rotation_enabled);
753
754 ClassDB::bind_method(D_METHOD("set_freeze_enabled", "freeze_mode"), &RigidBody3D::set_freeze_enabled);
755 ClassDB::bind_method(D_METHOD("is_freeze_enabled"), &RigidBody3D::is_freeze_enabled);
756
757 ClassDB::bind_method(D_METHOD("set_freeze_mode", "freeze_mode"), &RigidBody3D::set_freeze_mode);
758 ClassDB::bind_method(D_METHOD("get_freeze_mode"), &RigidBody3D::get_freeze_mode);
759
760 ClassDB::bind_method(D_METHOD("get_colliding_bodies"), &RigidBody3D::get_colliding_bodies);
761
762 GDVIRTUAL_BIND(_integrate_forces, "state");
763
764 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass", PROPERTY_HINT_RANGE, "0.001,1000,0.001,or_greater,exp,suffix:kg"), "set_mass", "get_mass");
765 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
766 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gravity_scale", PROPERTY_HINT_RANGE, "-8,8,0.001,or_less,or_greater"), "set_gravity_scale", "get_gravity_scale");
767 ADD_GROUP("Mass Distribution", "");
768 ADD_PROPERTY(PropertyInfo(Variant::INT, "center_of_mass_mode", PROPERTY_HINT_ENUM, "Auto,Custom"), "set_center_of_mass_mode", "get_center_of_mass_mode");
769 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "center_of_mass", PROPERTY_HINT_RANGE, "-10,10,0.01,or_less,or_greater,suffix:m"), "set_center_of_mass", "get_center_of_mass");
770 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "inertia", PROPERTY_HINT_RANGE, U"0,1000,0.01,or_greater,exp,suffix:kg\u22C5m\u00B2"), "set_inertia", "get_inertia");
771 ADD_GROUP("Deactivation", "");
772 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleeping", "is_sleeping");
773 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep"), "set_can_sleep", "is_able_to_sleep");
774 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "lock_rotation"), "set_lock_rotation_enabled", "is_lock_rotation_enabled");
775 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "freeze"), "set_freeze_enabled", "is_freeze_enabled");
776 ADD_PROPERTY(PropertyInfo(Variant::INT, "freeze_mode", PROPERTY_HINT_ENUM, "Static,Kinematic"), "set_freeze_mode", "get_freeze_mode");
777 ADD_GROUP("Solver", "");
778 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_integrator"), "set_use_custom_integrator", "is_using_custom_integrator");
779 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "continuous_cd"), "set_use_continuous_collision_detection", "is_using_continuous_collision_detection");
780 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "contact_monitor"), "set_contact_monitor", "is_contact_monitor_enabled");
781 ADD_PROPERTY(PropertyInfo(Variant::INT, "max_contacts_reported", PROPERTY_HINT_RANGE, "0,64,1,or_greater"), "set_max_contacts_reported", "get_max_contacts_reported");
782 ADD_GROUP("Linear", "linear_");
783 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity", PROPERTY_HINT_NONE, "suffix:m/s"), "set_linear_velocity", "get_linear_velocity");
784 ADD_PROPERTY(PropertyInfo(Variant::INT, "linear_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_linear_damp_mode", "get_linear_damp_mode");
785 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_linear_damp", "get_linear_damp");
786 ADD_GROUP("Angular", "angular_");
787 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity", PROPERTY_HINT_NONE, U"radians_as_degrees,suffix:\u00B0/s"), "set_angular_velocity", "get_angular_velocity");
788 ADD_PROPERTY(PropertyInfo(Variant::INT, "angular_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_angular_damp_mode", "get_angular_damp_mode");
789 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_angular_damp", "get_angular_damp");
790 ADD_GROUP("Constant Forces", "constant_");
791 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_force", PROPERTY_HINT_NONE, U"suffix:kg\u22C5m/s\u00B2 (N)"), "set_constant_force", "get_constant_force");
792 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant_torque", PROPERTY_HINT_NONE, U"suffix:kg\u22C5m\u00B2/s\u00B2/rad"), "set_constant_torque", "get_constant_torque");
793
794 ADD_SIGNAL(MethodInfo("body_shape_entered", PropertyInfo(Variant::RID, "body_rid"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::INT, "body_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
795 ADD_SIGNAL(MethodInfo("body_shape_exited", PropertyInfo(Variant::RID, "body_rid"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::INT, "body_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
796 ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
797 ADD_SIGNAL(MethodInfo("body_exited", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
798 ADD_SIGNAL(MethodInfo("sleeping_state_changed"));
799
800 BIND_ENUM_CONSTANT(FREEZE_MODE_STATIC);
801 BIND_ENUM_CONSTANT(FREEZE_MODE_KINEMATIC);
802
803 BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_AUTO);
804 BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_CUSTOM);
805
806 BIND_ENUM_CONSTANT(DAMP_MODE_COMBINE);
807 BIND_ENUM_CONSTANT(DAMP_MODE_REPLACE);
808 }
809
810 void RigidBody3D::_validate_property(PropertyInfo &p_property) const {
811 if (!Engine::get_singleton()->is_editor_hint()) {
812 return;
813 }
814 if (center_of_mass_mode != CENTER_OF_MASS_MODE_CUSTOM && p_property.name == "center_of_mass") {
815 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
816 }
817
818 if (!contact_monitor && p_property.name == "max_contacts_reported") {
819 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
820 }
821 }
822
823 RigidBody3D::RigidBody3D() :
824 PhysicsBody3D(PhysicsServer3D::BODY_MODE_RIGID) {
825 PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), callable_mp(this, &RigidBody3D::_body_state_changed));
826 }
827
828 RigidBody3D::~RigidBody3D() {
829 if (contact_monitor) {
830 memdelete(contact_monitor);
831 }
832 }