rigid_body_2d.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /**************************************************************************/
  2. /* rigid_body_2d.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_2d.h"
  31. void RigidBody2D::_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_scene);
  39. contact_monitor->locked = true;
  40. E->value.in_scene = 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 RigidBody2D::_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_scene);
  55. E->value.in_scene = 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 RigidBody2D::_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_scene = node && node->is_inside_tree();
  77. if (node) {
  78. node->connect(SceneStringName(tree_entered), callable_mp(this, &RigidBody2D::_body_enter_tree).bind(objid));
  79. node->connect(SceneStringName(tree_exiting), callable_mp(this, &RigidBody2D::_body_exit_tree).bind(objid));
  80. if (E->value.in_scene) {
  81. emit_signal(SceneStringName(body_entered), node);
  82. }
  83. }
  84. //E->value.rc++;
  85. }
  86. if (node) {
  87. E->value.shapes.insert(ShapePair(p_body_shape, p_local_shape));
  88. }
  89. if (E->value.in_scene) {
  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_scene = E->value.in_scene;
  98. if (E->value.shapes.is_empty()) {
  99. if (node) {
  100. node->disconnect(SceneStringName(tree_entered), callable_mp(this, &RigidBody2D::_body_enter_tree));
  101. node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &RigidBody2D::_body_exit_tree));
  102. if (in_scene) {
  103. emit_signal(SceneStringName(body_exited), node);
  104. }
  105. }
  106. contact_monitor->body_map.remove(E);
  107. }
  108. if (node && in_scene) {
  109. emit_signal(SceneStringName(body_shape_exited), p_body, node, p_body_shape, p_local_shape);
  110. }
  111. }
  112. }
  113. struct _RigidBody2DInOut {
  114. RID rid;
  115. ObjectID id;
  116. int shape = 0;
  117. int local_shape = 0;
  118. };
  119. void RigidBody2D::_sync_body_state(PhysicsDirectBodyState2D *p_state) {
  120. if (!freeze || freeze_mode != FREEZE_MODE_KINEMATIC) {
  121. set_block_transform_notify(true);
  122. set_global_transform(p_state->get_transform());
  123. set_block_transform_notify(false);
  124. }
  125. linear_velocity = p_state->get_linear_velocity();
  126. angular_velocity = p_state->get_angular_velocity();
  127. contact_count = p_state->get_contact_count();
  128. if (sleeping != p_state->is_sleeping()) {
  129. sleeping = p_state->is_sleeping();
  130. emit_signal(SceneStringName(sleeping_state_changed));
  131. }
  132. }
  133. void RigidBody2D::_body_state_changed(PhysicsDirectBodyState2D *p_state) {
  134. lock_callback();
  135. if (GDVIRTUAL_IS_OVERRIDDEN(_integrate_forces)) {
  136. _sync_body_state(p_state);
  137. Transform2D old_transform = get_global_transform();
  138. GDVIRTUAL_CALL(_integrate_forces, p_state);
  139. Transform2D new_transform = get_global_transform();
  140. if (new_transform != old_transform) {
  141. // Update the physics server with the new transform, to prevent it from being overwritten at the sync below.
  142. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_TRANSFORM, new_transform);
  143. }
  144. }
  145. _sync_body_state(p_state);
  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. _RigidBody2DInOut *toadd = (_RigidBody2DInOut *)alloca(p_state->get_contact_count() * sizeof(_RigidBody2DInOut));
  157. int toadd_count = 0; //state->get_contact_count();
  158. RigidBody2D_RemoveAction *toremove = (RigidBody2D_RemoveAction *)alloca(rc * sizeof(RigidBody2D_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 RigidBody2D::_apply_body_mode() {
  211. if (freeze) {
  212. switch (freeze_mode) {
  213. case FREEZE_MODE_STATIC: {
  214. set_body_mode(PhysicsServer2D::BODY_MODE_STATIC);
  215. } break;
  216. case FREEZE_MODE_KINEMATIC: {
  217. set_body_mode(PhysicsServer2D::BODY_MODE_KINEMATIC);
  218. } break;
  219. }
  220. } else if (lock_rotation) {
  221. set_body_mode(PhysicsServer2D::BODY_MODE_RIGID_LINEAR);
  222. } else {
  223. set_body_mode(PhysicsServer2D::BODY_MODE_RIGID);
  224. }
  225. }
  226. void RigidBody2D::set_lock_rotation_enabled(bool p_lock_rotation) {
  227. if (p_lock_rotation == lock_rotation) {
  228. return;
  229. }
  230. lock_rotation = p_lock_rotation;
  231. _apply_body_mode();
  232. }
  233. bool RigidBody2D::is_lock_rotation_enabled() const {
  234. return lock_rotation;
  235. }
  236. void RigidBody2D::set_freeze_enabled(bool p_freeze) {
  237. if (p_freeze == freeze) {
  238. return;
  239. }
  240. freeze = p_freeze;
  241. _apply_body_mode();
  242. }
  243. bool RigidBody2D::is_freeze_enabled() const {
  244. return freeze;
  245. }
  246. void RigidBody2D::set_freeze_mode(FreezeMode p_freeze_mode) {
  247. if (p_freeze_mode == freeze_mode) {
  248. return;
  249. }
  250. freeze_mode = p_freeze_mode;
  251. _apply_body_mode();
  252. }
  253. RigidBody2D::FreezeMode RigidBody2D::get_freeze_mode() const {
  254. return freeze_mode;
  255. }
  256. void RigidBody2D::set_mass(real_t p_mass) {
  257. ERR_FAIL_COND(p_mass <= 0);
  258. mass = p_mass;
  259. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_MASS, mass);
  260. }
  261. real_t RigidBody2D::get_mass() const {
  262. return mass;
  263. }
  264. void RigidBody2D::set_inertia(real_t p_inertia) {
  265. ERR_FAIL_COND(p_inertia < 0);
  266. inertia = p_inertia;
  267. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_INERTIA, inertia);
  268. }
  269. real_t RigidBody2D::get_inertia() const {
  270. return inertia;
  271. }
  272. void RigidBody2D::set_center_of_mass_mode(CenterOfMassMode p_mode) {
  273. if (center_of_mass_mode == p_mode) {
  274. return;
  275. }
  276. center_of_mass_mode = p_mode;
  277. switch (center_of_mass_mode) {
  278. case CENTER_OF_MASS_MODE_AUTO: {
  279. center_of_mass = Vector2();
  280. PhysicsServer2D::get_singleton()->body_reset_mass_properties(get_rid());
  281. if (inertia != 0.0) {
  282. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_INERTIA, inertia);
  283. }
  284. } break;
  285. case CENTER_OF_MASS_MODE_CUSTOM: {
  286. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_CENTER_OF_MASS, center_of_mass);
  287. } break;
  288. }
  289. notify_property_list_changed();
  290. }
  291. RigidBody2D::CenterOfMassMode RigidBody2D::get_center_of_mass_mode() const {
  292. return center_of_mass_mode;
  293. }
  294. void RigidBody2D::set_center_of_mass(const Vector2 &p_center_of_mass) {
  295. if (center_of_mass == p_center_of_mass) {
  296. return;
  297. }
  298. ERR_FAIL_COND(center_of_mass_mode != CENTER_OF_MASS_MODE_CUSTOM);
  299. center_of_mass = p_center_of_mass;
  300. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_CENTER_OF_MASS, center_of_mass);
  301. }
  302. const Vector2 &RigidBody2D::get_center_of_mass() const {
  303. return center_of_mass;
  304. }
  305. void RigidBody2D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
  306. if (physics_material_override.is_valid()) {
  307. physics_material_override->disconnect_changed(callable_mp(this, &RigidBody2D::_reload_physics_characteristics));
  308. }
  309. physics_material_override = p_physics_material_override;
  310. if (physics_material_override.is_valid()) {
  311. physics_material_override->connect_changed(callable_mp(this, &RigidBody2D::_reload_physics_characteristics));
  312. }
  313. _reload_physics_characteristics();
  314. }
  315. Ref<PhysicsMaterial> RigidBody2D::get_physics_material_override() const {
  316. return physics_material_override;
  317. }
  318. void RigidBody2D::set_gravity_scale(real_t p_gravity_scale) {
  319. gravity_scale = p_gravity_scale;
  320. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_GRAVITY_SCALE, gravity_scale);
  321. }
  322. real_t RigidBody2D::get_gravity_scale() const {
  323. return gravity_scale;
  324. }
  325. void RigidBody2D::set_linear_damp_mode(DampMode p_mode) {
  326. linear_damp_mode = p_mode;
  327. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_LINEAR_DAMP_MODE, linear_damp_mode);
  328. }
  329. RigidBody2D::DampMode RigidBody2D::get_linear_damp_mode() const {
  330. return linear_damp_mode;
  331. }
  332. void RigidBody2D::set_angular_damp_mode(DampMode p_mode) {
  333. angular_damp_mode = p_mode;
  334. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP_MODE, angular_damp_mode);
  335. }
  336. RigidBody2D::DampMode RigidBody2D::get_angular_damp_mode() const {
  337. return angular_damp_mode;
  338. }
  339. void RigidBody2D::set_linear_damp(real_t p_linear_damp) {
  340. ERR_FAIL_COND(p_linear_damp < -1);
  341. linear_damp = p_linear_damp;
  342. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_LINEAR_DAMP, linear_damp);
  343. }
  344. real_t RigidBody2D::get_linear_damp() const {
  345. return linear_damp;
  346. }
  347. void RigidBody2D::set_angular_damp(real_t p_angular_damp) {
  348. ERR_FAIL_COND(p_angular_damp < -1);
  349. angular_damp = p_angular_damp;
  350. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP, angular_damp);
  351. }
  352. real_t RigidBody2D::get_angular_damp() const {
  353. return angular_damp;
  354. }
  355. void RigidBody2D::set_axis_velocity(const Vector2 &p_axis) {
  356. Vector2 axis = p_axis.normalized();
  357. linear_velocity -= axis * axis.dot(linear_velocity);
  358. linear_velocity += p_axis;
  359. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
  360. }
  361. void RigidBody2D::set_linear_velocity(const Vector2 &p_velocity) {
  362. linear_velocity = p_velocity;
  363. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
  364. }
  365. Vector2 RigidBody2D::get_linear_velocity() const {
  366. return linear_velocity;
  367. }
  368. void RigidBody2D::set_angular_velocity(real_t p_velocity) {
  369. angular_velocity = p_velocity;
  370. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY, angular_velocity);
  371. }
  372. real_t RigidBody2D::get_angular_velocity() const {
  373. return angular_velocity;
  374. }
  375. void RigidBody2D::set_use_custom_integrator(bool p_enable) {
  376. if (custom_integrator == p_enable) {
  377. return;
  378. }
  379. custom_integrator = p_enable;
  380. PhysicsServer2D::get_singleton()->body_set_omit_force_integration(get_rid(), p_enable);
  381. }
  382. bool RigidBody2D::is_using_custom_integrator() {
  383. return custom_integrator;
  384. }
  385. void RigidBody2D::set_sleeping(bool p_sleeping) {
  386. sleeping = p_sleeping;
  387. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_SLEEPING, sleeping);
  388. }
  389. void RigidBody2D::set_can_sleep(bool p_active) {
  390. can_sleep = p_active;
  391. PhysicsServer2D::get_singleton()->body_set_state(get_rid(), PhysicsServer2D::BODY_STATE_CAN_SLEEP, p_active);
  392. }
  393. bool RigidBody2D::is_able_to_sleep() const {
  394. return can_sleep;
  395. }
  396. bool RigidBody2D::is_sleeping() const {
  397. return sleeping;
  398. }
  399. void RigidBody2D::set_max_contacts_reported(int p_amount) {
  400. max_contacts_reported = p_amount;
  401. PhysicsServer2D::get_singleton()->body_set_max_contacts_reported(get_rid(), p_amount);
  402. }
  403. int RigidBody2D::get_max_contacts_reported() const {
  404. return max_contacts_reported;
  405. }
  406. int RigidBody2D::get_contact_count() const {
  407. return contact_count;
  408. }
  409. void RigidBody2D::apply_central_impulse(const Vector2 &p_impulse) {
  410. PhysicsServer2D::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse);
  411. }
  412. void RigidBody2D::apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position) {
  413. PhysicsServer2D::get_singleton()->body_apply_impulse(get_rid(), p_impulse, p_position);
  414. }
  415. void RigidBody2D::apply_torque_impulse(real_t p_torque) {
  416. PhysicsServer2D::get_singleton()->body_apply_torque_impulse(get_rid(), p_torque);
  417. }
  418. void RigidBody2D::apply_central_force(const Vector2 &p_force) {
  419. PhysicsServer2D::get_singleton()->body_apply_central_force(get_rid(), p_force);
  420. }
  421. void RigidBody2D::apply_force(const Vector2 &p_force, const Vector2 &p_position) {
  422. PhysicsServer2D::get_singleton()->body_apply_force(get_rid(), p_force, p_position);
  423. }
  424. void RigidBody2D::apply_torque(real_t p_torque) {
  425. PhysicsServer2D::get_singleton()->body_apply_torque(get_rid(), p_torque);
  426. }
  427. void RigidBody2D::add_constant_central_force(const Vector2 &p_force) {
  428. PhysicsServer2D::get_singleton()->body_add_constant_central_force(get_rid(), p_force);
  429. }
  430. void RigidBody2D::add_constant_force(const Vector2 &p_force, const Vector2 &p_position) {
  431. PhysicsServer2D::get_singleton()->body_add_constant_force(get_rid(), p_force, p_position);
  432. }
  433. void RigidBody2D::add_constant_torque(const real_t p_torque) {
  434. PhysicsServer2D::get_singleton()->body_add_constant_torque(get_rid(), p_torque);
  435. }
  436. void RigidBody2D::set_constant_force(const Vector2 &p_force) {
  437. PhysicsServer2D::get_singleton()->body_set_constant_force(get_rid(), p_force);
  438. }
  439. Vector2 RigidBody2D::get_constant_force() const {
  440. return PhysicsServer2D::get_singleton()->body_get_constant_force(get_rid());
  441. }
  442. void RigidBody2D::set_constant_torque(real_t p_torque) {
  443. PhysicsServer2D::get_singleton()->body_set_constant_torque(get_rid(), p_torque);
  444. }
  445. real_t RigidBody2D::get_constant_torque() const {
  446. return PhysicsServer2D::get_singleton()->body_get_constant_torque(get_rid());
  447. }
  448. void RigidBody2D::set_continuous_collision_detection_mode(CCDMode p_mode) {
  449. ccd_mode = p_mode;
  450. PhysicsServer2D::get_singleton()->body_set_continuous_collision_detection_mode(get_rid(), PhysicsServer2D::CCDMode(p_mode));
  451. }
  452. RigidBody2D::CCDMode RigidBody2D::get_continuous_collision_detection_mode() const {
  453. return ccd_mode;
  454. }
  455. TypedArray<Node2D> RigidBody2D::get_colliding_bodies() const {
  456. ERR_FAIL_NULL_V(contact_monitor, TypedArray<Node2D>());
  457. TypedArray<Node2D> ret;
  458. ret.resize(contact_monitor->body_map.size());
  459. int idx = 0;
  460. for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
  461. Object *obj = ObjectDB::get_instance(E.key);
  462. if (!obj) {
  463. ret.resize(ret.size() - 1); //ops
  464. } else {
  465. ret[idx++] = obj;
  466. }
  467. }
  468. return ret;
  469. }
  470. void RigidBody2D::set_contact_monitor(bool p_enabled) {
  471. if (p_enabled == is_contact_monitor_enabled()) {
  472. return;
  473. }
  474. if (!p_enabled) {
  475. ERR_FAIL_COND_MSG(contact_monitor->locked, "Can't disable contact monitoring during in/out callback. Use call_deferred(\"set_contact_monitor\", false) instead.");
  476. for (const KeyValue<ObjectID, BodyState> &E : contact_monitor->body_map) {
  477. //clean up mess
  478. Object *obj = ObjectDB::get_instance(E.key);
  479. Node *node = Object::cast_to<Node>(obj);
  480. if (node) {
  481. node->disconnect(SceneStringName(tree_entered), callable_mp(this, &RigidBody2D::_body_enter_tree));
  482. node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &RigidBody2D::_body_exit_tree));
  483. }
  484. }
  485. memdelete(contact_monitor);
  486. contact_monitor = nullptr;
  487. } else {
  488. contact_monitor = memnew(ContactMonitor);
  489. contact_monitor->locked = false;
  490. }
  491. notify_property_list_changed();
  492. }
  493. bool RigidBody2D::is_contact_monitor_enabled() const {
  494. return contact_monitor != nullptr;
  495. }
  496. void RigidBody2D::_notification(int p_what) {
  497. #ifdef TOOLS_ENABLED
  498. switch (p_what) {
  499. case NOTIFICATION_ENTER_TREE: {
  500. if (Engine::get_singleton()->is_editor_hint()) {
  501. set_notify_local_transform(true); // Used for warnings and only in editor.
  502. }
  503. } break;
  504. case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
  505. update_configuration_warnings();
  506. } break;
  507. }
  508. #endif
  509. }
  510. PackedStringArray RigidBody2D::get_configuration_warnings() const {
  511. Transform2D t = get_transform();
  512. PackedStringArray warnings = PhysicsBody2D::get_configuration_warnings();
  513. if (ABS(t.columns[0].length() - 1.0) > 0.05 || ABS(t.columns[1].length() - 1.0) > 0.05) {
  514. warnings.push_back(RTR("Size changes to RigidBody2D will be overridden by the physics engine when running.\nChange the size in children collision shapes instead."));
  515. }
  516. return warnings;
  517. }
  518. void RigidBody2D::_bind_methods() {
  519. ClassDB::bind_method(D_METHOD("set_mass", "mass"), &RigidBody2D::set_mass);
  520. ClassDB::bind_method(D_METHOD("get_mass"), &RigidBody2D::get_mass);
  521. ClassDB::bind_method(D_METHOD("get_inertia"), &RigidBody2D::get_inertia);
  522. ClassDB::bind_method(D_METHOD("set_inertia", "inertia"), &RigidBody2D::set_inertia);
  523. ClassDB::bind_method(D_METHOD("set_center_of_mass_mode", "mode"), &RigidBody2D::set_center_of_mass_mode);
  524. ClassDB::bind_method(D_METHOD("get_center_of_mass_mode"), &RigidBody2D::get_center_of_mass_mode);
  525. ClassDB::bind_method(D_METHOD("set_center_of_mass", "center_of_mass"), &RigidBody2D::set_center_of_mass);
  526. ClassDB::bind_method(D_METHOD("get_center_of_mass"), &RigidBody2D::get_center_of_mass);
  527. ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &RigidBody2D::set_physics_material_override);
  528. ClassDB::bind_method(D_METHOD("get_physics_material_override"), &RigidBody2D::get_physics_material_override);
  529. ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &RigidBody2D::set_gravity_scale);
  530. ClassDB::bind_method(D_METHOD("get_gravity_scale"), &RigidBody2D::get_gravity_scale);
  531. ClassDB::bind_method(D_METHOD("set_linear_damp_mode", "linear_damp_mode"), &RigidBody2D::set_linear_damp_mode);
  532. ClassDB::bind_method(D_METHOD("get_linear_damp_mode"), &RigidBody2D::get_linear_damp_mode);
  533. ClassDB::bind_method(D_METHOD("set_angular_damp_mode", "angular_damp_mode"), &RigidBody2D::set_angular_damp_mode);
  534. ClassDB::bind_method(D_METHOD("get_angular_damp_mode"), &RigidBody2D::get_angular_damp_mode);
  535. ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &RigidBody2D::set_linear_damp);
  536. ClassDB::bind_method(D_METHOD("get_linear_damp"), &RigidBody2D::get_linear_damp);
  537. ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &RigidBody2D::set_angular_damp);
  538. ClassDB::bind_method(D_METHOD("get_angular_damp"), &RigidBody2D::get_angular_damp);
  539. ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &RigidBody2D::set_linear_velocity);
  540. ClassDB::bind_method(D_METHOD("get_linear_velocity"), &RigidBody2D::get_linear_velocity);
  541. ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &RigidBody2D::set_angular_velocity);
  542. ClassDB::bind_method(D_METHOD("get_angular_velocity"), &RigidBody2D::get_angular_velocity);
  543. ClassDB::bind_method(D_METHOD("set_max_contacts_reported", "amount"), &RigidBody2D::set_max_contacts_reported);
  544. ClassDB::bind_method(D_METHOD("get_max_contacts_reported"), &RigidBody2D::get_max_contacts_reported);
  545. ClassDB::bind_method(D_METHOD("get_contact_count"), &RigidBody2D::get_contact_count);
  546. ClassDB::bind_method(D_METHOD("set_use_custom_integrator", "enable"), &RigidBody2D::set_use_custom_integrator);
  547. ClassDB::bind_method(D_METHOD("is_using_custom_integrator"), &RigidBody2D::is_using_custom_integrator);
  548. ClassDB::bind_method(D_METHOD("set_contact_monitor", "enabled"), &RigidBody2D::set_contact_monitor);
  549. ClassDB::bind_method(D_METHOD("is_contact_monitor_enabled"), &RigidBody2D::is_contact_monitor_enabled);
  550. ClassDB::bind_method(D_METHOD("set_continuous_collision_detection_mode", "mode"), &RigidBody2D::set_continuous_collision_detection_mode);
  551. ClassDB::bind_method(D_METHOD("get_continuous_collision_detection_mode"), &RigidBody2D::get_continuous_collision_detection_mode);
  552. ClassDB::bind_method(D_METHOD("set_axis_velocity", "axis_velocity"), &RigidBody2D::set_axis_velocity);
  553. ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &RigidBody2D::apply_central_impulse, Vector2());
  554. ClassDB::bind_method(D_METHOD("apply_impulse", "impulse", "position"), &RigidBody2D::apply_impulse, Vector2());
  555. ClassDB::bind_method(D_METHOD("apply_torque_impulse", "torque"), &RigidBody2D::apply_torque_impulse);
  556. ClassDB::bind_method(D_METHOD("apply_central_force", "force"), &RigidBody2D::apply_central_force);
  557. ClassDB::bind_method(D_METHOD("apply_force", "force", "position"), &RigidBody2D::apply_force, Vector2());
  558. ClassDB::bind_method(D_METHOD("apply_torque", "torque"), &RigidBody2D::apply_torque);
  559. ClassDB::bind_method(D_METHOD("add_constant_central_force", "force"), &RigidBody2D::add_constant_central_force);
  560. ClassDB::bind_method(D_METHOD("add_constant_force", "force", "position"), &RigidBody2D::add_constant_force, Vector2());
  561. ClassDB::bind_method(D_METHOD("add_constant_torque", "torque"), &RigidBody2D::add_constant_torque);
  562. ClassDB::bind_method(D_METHOD("set_constant_force", "force"), &RigidBody2D::set_constant_force);
  563. ClassDB::bind_method(D_METHOD("get_constant_force"), &RigidBody2D::get_constant_force);
  564. ClassDB::bind_method(D_METHOD("set_constant_torque", "torque"), &RigidBody2D::set_constant_torque);
  565. ClassDB::bind_method(D_METHOD("get_constant_torque"), &RigidBody2D::get_constant_torque);
  566. ClassDB::bind_method(D_METHOD("set_sleeping", "sleeping"), &RigidBody2D::set_sleeping);
  567. ClassDB::bind_method(D_METHOD("is_sleeping"), &RigidBody2D::is_sleeping);
  568. ClassDB::bind_method(D_METHOD("set_can_sleep", "able_to_sleep"), &RigidBody2D::set_can_sleep);
  569. ClassDB::bind_method(D_METHOD("is_able_to_sleep"), &RigidBody2D::is_able_to_sleep);
  570. ClassDB::bind_method(D_METHOD("set_lock_rotation_enabled", "lock_rotation"), &RigidBody2D::set_lock_rotation_enabled);
  571. ClassDB::bind_method(D_METHOD("is_lock_rotation_enabled"), &RigidBody2D::is_lock_rotation_enabled);
  572. ClassDB::bind_method(D_METHOD("set_freeze_enabled", "freeze_mode"), &RigidBody2D::set_freeze_enabled);
  573. ClassDB::bind_method(D_METHOD("is_freeze_enabled"), &RigidBody2D::is_freeze_enabled);
  574. ClassDB::bind_method(D_METHOD("set_freeze_mode", "freeze_mode"), &RigidBody2D::set_freeze_mode);
  575. ClassDB::bind_method(D_METHOD("get_freeze_mode"), &RigidBody2D::get_freeze_mode);
  576. ClassDB::bind_method(D_METHOD("get_colliding_bodies"), &RigidBody2D::get_colliding_bodies);
  577. GDVIRTUAL_BIND(_integrate_forces, "state");
  578. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass", PROPERTY_HINT_RANGE, "0.001,1000,0.001,or_greater,exp,suffix:kg"), "set_mass", "get_mass");
  579. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
  580. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gravity_scale", PROPERTY_HINT_RANGE, "-8,8,0.001,or_less,or_greater"), "set_gravity_scale", "get_gravity_scale");
  581. ADD_GROUP("Mass Distribution", "");
  582. ADD_PROPERTY(PropertyInfo(Variant::INT, "center_of_mass_mode", PROPERTY_HINT_ENUM, "Auto,Custom"), "set_center_of_mass_mode", "get_center_of_mass_mode");
  583. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "center_of_mass", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_less,or_greater,suffix:px"), "set_center_of_mass", "get_center_of_mass");
  584. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inertia", PROPERTY_HINT_RANGE, U"0,1000,0.01,or_greater,exp,suffix:kg\u22C5px\u00B2"), "set_inertia", "get_inertia");
  585. ADD_GROUP("Deactivation", "");
  586. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleeping", "is_sleeping");
  587. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep"), "set_can_sleep", "is_able_to_sleep");
  588. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "lock_rotation"), "set_lock_rotation_enabled", "is_lock_rotation_enabled");
  589. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "freeze"), "set_freeze_enabled", "is_freeze_enabled");
  590. ADD_PROPERTY(PropertyInfo(Variant::INT, "freeze_mode", PROPERTY_HINT_ENUM, "Static,Kinematic"), "set_freeze_mode", "get_freeze_mode");
  591. ADD_GROUP("Solver", "");
  592. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_integrator"), "set_use_custom_integrator", "is_using_custom_integrator");
  593. ADD_PROPERTY(PropertyInfo(Variant::INT, "continuous_cd", PROPERTY_HINT_ENUM, "Disabled,Cast Ray,Cast Shape"), "set_continuous_collision_detection_mode", "get_continuous_collision_detection_mode");
  594. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "contact_monitor"), "set_contact_monitor", "is_contact_monitor_enabled");
  595. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_contacts_reported", PROPERTY_HINT_RANGE, "0,64,1,or_greater"), "set_max_contacts_reported", "get_max_contacts_reported");
  596. ADD_GROUP("Linear", "linear_");
  597. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "linear_velocity", PROPERTY_HINT_NONE, "suffix:px/s"), "set_linear_velocity", "get_linear_velocity");
  598. ADD_PROPERTY(PropertyInfo(Variant::INT, "linear_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_linear_damp_mode", "get_linear_damp_mode");
  599. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), "set_linear_damp", "get_linear_damp");
  600. ADD_GROUP("Angular", "angular_");
  601. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_velocity", PROPERTY_HINT_NONE, U"radians_as_degrees,suffix:\u00B0/s"), "set_angular_velocity", "get_angular_velocity");
  602. ADD_PROPERTY(PropertyInfo(Variant::INT, "angular_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_angular_damp_mode", "get_angular_damp_mode");
  603. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), "set_angular_damp", "get_angular_damp");
  604. ADD_GROUP("Constant Forces", "constant_");
  605. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "constant_force", PROPERTY_HINT_NONE, U"suffix:kg\u22C5px/s\u00B2"), "set_constant_force", "get_constant_force");
  606. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "constant_torque", PROPERTY_HINT_NONE, U"suffix:kg\u22C5px\u00B2/s\u00B2/rad"), "set_constant_torque", "get_constant_torque");
  607. 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")));
  608. 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")));
  609. ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  610. ADD_SIGNAL(MethodInfo("body_exited", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  611. ADD_SIGNAL(MethodInfo("sleeping_state_changed"));
  612. BIND_ENUM_CONSTANT(FREEZE_MODE_STATIC);
  613. BIND_ENUM_CONSTANT(FREEZE_MODE_KINEMATIC);
  614. BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_AUTO);
  615. BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_CUSTOM);
  616. BIND_ENUM_CONSTANT(DAMP_MODE_COMBINE);
  617. BIND_ENUM_CONSTANT(DAMP_MODE_REPLACE);
  618. BIND_ENUM_CONSTANT(CCD_MODE_DISABLED);
  619. BIND_ENUM_CONSTANT(CCD_MODE_CAST_RAY);
  620. BIND_ENUM_CONSTANT(CCD_MODE_CAST_SHAPE);
  621. }
  622. void RigidBody2D::_validate_property(PropertyInfo &p_property) const {
  623. if (center_of_mass_mode != CENTER_OF_MASS_MODE_CUSTOM && p_property.name == "center_of_mass") {
  624. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  625. }
  626. if (!contact_monitor && p_property.name == "max_contacts_reported") {
  627. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  628. }
  629. }
  630. RigidBody2D::RigidBody2D() :
  631. PhysicsBody2D(PhysicsServer2D::BODY_MODE_RIGID) {
  632. PhysicsServer2D::get_singleton()->body_set_state_sync_callback(get_rid(), callable_mp(this, &RigidBody2D::_body_state_changed));
  633. }
  634. RigidBody2D::~RigidBody2D() {
  635. if (contact_monitor) {
  636. memdelete(contact_monitor);
  637. }
  638. }
  639. void RigidBody2D::_reload_physics_characteristics() {
  640. if (physics_material_override.is_null()) {
  641. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_BOUNCE, 0);
  642. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_FRICTION, 1);
  643. } else {
  644. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
  645. PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
  646. }
  647. }