rigid_body_3d.cpp 32 KB

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