godot_body_2d.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /**************************************************************************/
  2. /* godot_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 "godot_body_2d.h"
  31. #include "godot_area_2d.h"
  32. #include "godot_body_direct_state_2d.h"
  33. #include "godot_constraint_2d.h"
  34. #include "godot_space_2d.h"
  35. void GodotBody2D::_mass_properties_changed() {
  36. if (get_space() && !mass_properties_update_list.in_list()) {
  37. get_space()->body_add_to_mass_properties_update_list(&mass_properties_update_list);
  38. }
  39. }
  40. void GodotBody2D::update_mass_properties() {
  41. //update shapes and motions
  42. switch (mode) {
  43. case PhysicsServer2D::BODY_MODE_RIGID: {
  44. real_t total_area = 0;
  45. for (int i = 0; i < get_shape_count(); i++) {
  46. if (is_shape_disabled(i)) {
  47. continue;
  48. }
  49. total_area += get_shape_aabb(i).get_area();
  50. }
  51. if (calculate_center_of_mass) {
  52. // We have to recompute the center of mass.
  53. center_of_mass_local = Vector2();
  54. if (total_area != 0.0) {
  55. for (int i = 0; i < get_shape_count(); i++) {
  56. if (is_shape_disabled(i)) {
  57. continue;
  58. }
  59. real_t area = get_shape_aabb(i).get_area();
  60. real_t mass_new = area * mass / total_area;
  61. // NOTE: we assume that the shape origin is also its center of mass.
  62. center_of_mass_local += mass_new * get_shape_transform(i).get_origin();
  63. }
  64. center_of_mass_local /= mass;
  65. }
  66. }
  67. if (calculate_inertia) {
  68. inertia = 0;
  69. for (int i = 0; i < get_shape_count(); i++) {
  70. if (is_shape_disabled(i)) {
  71. continue;
  72. }
  73. const GodotShape2D *shape = get_shape(i);
  74. real_t area = get_shape_aabb(i).get_area();
  75. if (area == 0.0) {
  76. continue;
  77. }
  78. real_t mass_new = area * mass / total_area;
  79. Transform2D mtx = get_shape_transform(i);
  80. Vector2 scale = mtx.get_scale();
  81. Vector2 shape_origin = mtx.get_origin() - center_of_mass_local;
  82. inertia += shape->get_moment_of_inertia(mass_new, scale) + mass_new * shape_origin.length_squared();
  83. }
  84. }
  85. _inv_inertia = inertia > 0.0 ? (1.0 / inertia) : 0.0;
  86. if (mass) {
  87. _inv_mass = 1.0 / mass;
  88. } else {
  89. _inv_mass = 0;
  90. }
  91. } break;
  92. case PhysicsServer2D::BODY_MODE_KINEMATIC:
  93. case PhysicsServer2D::BODY_MODE_STATIC: {
  94. _inv_inertia = 0;
  95. _inv_mass = 0;
  96. } break;
  97. case PhysicsServer2D::BODY_MODE_RIGID_LINEAR: {
  98. _inv_inertia = 0;
  99. _inv_mass = 1.0 / mass;
  100. } break;
  101. }
  102. _update_transform_dependent();
  103. }
  104. void GodotBody2D::reset_mass_properties() {
  105. calculate_inertia = true;
  106. calculate_center_of_mass = true;
  107. _mass_properties_changed();
  108. }
  109. void GodotBody2D::set_active(bool p_active) {
  110. if (active == p_active) {
  111. return;
  112. }
  113. active = p_active;
  114. if (active) {
  115. if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
  116. // Static bodies can't be active.
  117. active = false;
  118. } else if (get_space()) {
  119. get_space()->body_add_to_active_list(&active_list);
  120. }
  121. } else if (get_space()) {
  122. get_space()->body_remove_from_active_list(&active_list);
  123. }
  124. }
  125. void GodotBody2D::set_param(PhysicsServer2D::BodyParameter p_param, const Variant &p_value) {
  126. switch (p_param) {
  127. case PhysicsServer2D::BODY_PARAM_BOUNCE: {
  128. bounce = p_value;
  129. } break;
  130. case PhysicsServer2D::BODY_PARAM_FRICTION: {
  131. friction = p_value;
  132. } break;
  133. case PhysicsServer2D::BODY_PARAM_MASS: {
  134. real_t mass_value = p_value;
  135. ERR_FAIL_COND(mass_value <= 0);
  136. mass = mass_value;
  137. if (mode >= PhysicsServer2D::BODY_MODE_RIGID) {
  138. _mass_properties_changed();
  139. }
  140. } break;
  141. case PhysicsServer2D::BODY_PARAM_INERTIA: {
  142. real_t inertia_value = p_value;
  143. if (inertia_value <= 0.0) {
  144. calculate_inertia = true;
  145. if (mode == PhysicsServer2D::BODY_MODE_RIGID) {
  146. _mass_properties_changed();
  147. }
  148. } else {
  149. calculate_inertia = false;
  150. inertia = inertia_value;
  151. if (mode == PhysicsServer2D::BODY_MODE_RIGID) {
  152. _inv_inertia = 1.0 / inertia;
  153. }
  154. }
  155. } break;
  156. case PhysicsServer2D::BODY_PARAM_CENTER_OF_MASS: {
  157. calculate_center_of_mass = false;
  158. center_of_mass_local = p_value;
  159. _update_transform_dependent();
  160. } break;
  161. case PhysicsServer2D::BODY_PARAM_GRAVITY_SCALE: {
  162. if (Math::is_zero_approx(gravity_scale)) {
  163. wakeup();
  164. }
  165. gravity_scale = p_value;
  166. } break;
  167. case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP_MODE: {
  168. int mode_value = p_value;
  169. linear_damp_mode = (PhysicsServer2D::BodyDampMode)mode_value;
  170. } break;
  171. case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP_MODE: {
  172. int mode_value = p_value;
  173. angular_damp_mode = (PhysicsServer2D::BodyDampMode)mode_value;
  174. } break;
  175. case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP: {
  176. linear_damp = p_value;
  177. } break;
  178. case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP: {
  179. angular_damp = p_value;
  180. } break;
  181. default: {
  182. }
  183. }
  184. }
  185. Variant GodotBody2D::get_param(PhysicsServer2D::BodyParameter p_param) const {
  186. switch (p_param) {
  187. case PhysicsServer2D::BODY_PARAM_BOUNCE: {
  188. return bounce;
  189. }
  190. case PhysicsServer2D::BODY_PARAM_FRICTION: {
  191. return friction;
  192. }
  193. case PhysicsServer2D::BODY_PARAM_MASS: {
  194. return mass;
  195. }
  196. case PhysicsServer2D::BODY_PARAM_INERTIA: {
  197. return inertia;
  198. }
  199. case PhysicsServer2D::BODY_PARAM_CENTER_OF_MASS: {
  200. return center_of_mass_local;
  201. }
  202. case PhysicsServer2D::BODY_PARAM_GRAVITY_SCALE: {
  203. return gravity_scale;
  204. }
  205. case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP_MODE: {
  206. return linear_damp_mode;
  207. }
  208. case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP_MODE: {
  209. return angular_damp_mode;
  210. }
  211. case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP: {
  212. return linear_damp;
  213. }
  214. case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP: {
  215. return angular_damp;
  216. }
  217. default: {
  218. }
  219. }
  220. return 0;
  221. }
  222. void GodotBody2D::set_mode(PhysicsServer2D::BodyMode p_mode) {
  223. PhysicsServer2D::BodyMode prev = mode;
  224. mode = p_mode;
  225. switch (p_mode) {
  226. //CLEAR UP EVERYTHING IN CASE IT NOT WORKS!
  227. case PhysicsServer2D::BODY_MODE_STATIC:
  228. case PhysicsServer2D::BODY_MODE_KINEMATIC: {
  229. _set_inv_transform(get_transform().affine_inverse());
  230. _inv_mass = 0;
  231. _inv_inertia = 0;
  232. _set_static(p_mode == PhysicsServer2D::BODY_MODE_STATIC);
  233. set_active(p_mode == PhysicsServer2D::BODY_MODE_KINEMATIC && contacts.size());
  234. linear_velocity = Vector2();
  235. angular_velocity = 0;
  236. if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC && prev != mode) {
  237. first_time_kinematic = true;
  238. }
  239. } break;
  240. case PhysicsServer2D::BODY_MODE_RIGID: {
  241. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  242. if (!calculate_inertia) {
  243. _inv_inertia = 1.0 / inertia;
  244. }
  245. _mass_properties_changed();
  246. _set_static(false);
  247. set_active(true);
  248. } break;
  249. case PhysicsServer2D::BODY_MODE_RIGID_LINEAR: {
  250. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  251. _inv_inertia = 0;
  252. angular_velocity = 0;
  253. _set_static(false);
  254. set_active(true);
  255. }
  256. }
  257. }
  258. PhysicsServer2D::BodyMode GodotBody2D::get_mode() const {
  259. return mode;
  260. }
  261. void GodotBody2D::_shapes_changed() {
  262. _mass_properties_changed();
  263. wakeup();
  264. wakeup_neighbours();
  265. }
  266. void GodotBody2D::set_state(PhysicsServer2D::BodyState p_state, const Variant &p_variant) {
  267. switch (p_state) {
  268. case PhysicsServer2D::BODY_STATE_TRANSFORM: {
  269. if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  270. new_transform = p_variant;
  271. //wakeup_neighbours();
  272. set_active(true);
  273. if (first_time_kinematic) {
  274. _set_transform(p_variant);
  275. _set_inv_transform(get_transform().affine_inverse());
  276. first_time_kinematic = false;
  277. }
  278. } else if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
  279. _set_transform(p_variant);
  280. _set_inv_transform(get_transform().affine_inverse());
  281. wakeup_neighbours();
  282. } else {
  283. Transform2D t = p_variant;
  284. t.orthonormalize();
  285. new_transform = get_transform(); //used as old to compute motion
  286. if (t == new_transform) {
  287. break;
  288. }
  289. _set_transform(t);
  290. _set_inv_transform(get_transform().inverse());
  291. _update_transform_dependent();
  292. }
  293. wakeup();
  294. } break;
  295. case PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY: {
  296. linear_velocity = p_variant;
  297. constant_linear_velocity = linear_velocity;
  298. wakeup();
  299. } break;
  300. case PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY: {
  301. angular_velocity = p_variant;
  302. constant_angular_velocity = angular_velocity;
  303. wakeup();
  304. } break;
  305. case PhysicsServer2D::BODY_STATE_SLEEPING: {
  306. if (mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  307. break;
  308. }
  309. bool do_sleep = p_variant;
  310. if (do_sleep) {
  311. linear_velocity = Vector2();
  312. //biased_linear_velocity=Vector3();
  313. angular_velocity = 0;
  314. //biased_angular_velocity=Vector3();
  315. set_active(false);
  316. } else {
  317. if (mode != PhysicsServer2D::BODY_MODE_STATIC) {
  318. set_active(true);
  319. }
  320. }
  321. } break;
  322. case PhysicsServer2D::BODY_STATE_CAN_SLEEP: {
  323. can_sleep = p_variant;
  324. if (mode >= PhysicsServer2D::BODY_MODE_RIGID && !active && !can_sleep) {
  325. set_active(true);
  326. }
  327. } break;
  328. }
  329. }
  330. Variant GodotBody2D::get_state(PhysicsServer2D::BodyState p_state) const {
  331. switch (p_state) {
  332. case PhysicsServer2D::BODY_STATE_TRANSFORM: {
  333. return get_transform();
  334. }
  335. case PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY: {
  336. return linear_velocity;
  337. }
  338. case PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY: {
  339. return angular_velocity;
  340. }
  341. case PhysicsServer2D::BODY_STATE_SLEEPING: {
  342. return !is_active();
  343. }
  344. case PhysicsServer2D::BODY_STATE_CAN_SLEEP: {
  345. return can_sleep;
  346. }
  347. }
  348. return Variant();
  349. }
  350. void GodotBody2D::set_space(GodotSpace2D *p_space) {
  351. if (get_space()) {
  352. wakeup_neighbours();
  353. if (mass_properties_update_list.in_list()) {
  354. get_space()->body_remove_from_mass_properties_update_list(&mass_properties_update_list);
  355. }
  356. if (active_list.in_list()) {
  357. get_space()->body_remove_from_active_list(&active_list);
  358. }
  359. if (direct_state_query_list.in_list()) {
  360. get_space()->body_remove_from_state_query_list(&direct_state_query_list);
  361. }
  362. }
  363. _set_space(p_space);
  364. if (get_space()) {
  365. _mass_properties_changed();
  366. if (active && !active_list.in_list()) {
  367. get_space()->body_add_to_active_list(&active_list);
  368. }
  369. }
  370. }
  371. void GodotBody2D::_update_transform_dependent() {
  372. center_of_mass = get_transform().basis_xform(center_of_mass_local);
  373. }
  374. void GodotBody2D::integrate_forces(real_t p_step) {
  375. if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
  376. return;
  377. }
  378. ERR_FAIL_NULL(get_space());
  379. int ac = areas.size();
  380. bool gravity_done = false;
  381. bool linear_damp_done = false;
  382. bool angular_damp_done = false;
  383. bool stopped = false;
  384. gravity = Vector2(0, 0);
  385. total_linear_damp = 0.0;
  386. total_angular_damp = 0.0;
  387. // Combine gravity and damping from overlapping areas in priority order.
  388. if (ac) {
  389. areas.sort();
  390. const AreaCMP *aa = &areas[0];
  391. for (int i = ac - 1; i >= 0 && !stopped; i--) {
  392. if (!gravity_done) {
  393. PhysicsServer2D::AreaSpaceOverrideMode area_gravity_mode = (PhysicsServer2D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE);
  394. if (area_gravity_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
  395. Vector2 area_gravity;
  396. aa[i].area->compute_gravity(get_transform().get_origin(), area_gravity);
  397. switch (area_gravity_mode) {
  398. case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE:
  399. case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
  400. gravity += area_gravity;
  401. gravity_done = area_gravity_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
  402. } break;
  403. case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE:
  404. case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
  405. gravity = area_gravity;
  406. gravity_done = area_gravity_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE;
  407. } break;
  408. default: {
  409. }
  410. }
  411. }
  412. }
  413. if (!linear_damp_done) {
  414. PhysicsServer2D::AreaSpaceOverrideMode area_linear_damp_mode = (PhysicsServer2D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE);
  415. if (area_linear_damp_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
  416. real_t area_linear_damp = aa[i].area->get_linear_damp();
  417. switch (area_linear_damp_mode) {
  418. case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE:
  419. case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
  420. total_linear_damp += area_linear_damp;
  421. linear_damp_done = area_linear_damp_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
  422. } break;
  423. case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE:
  424. case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
  425. total_linear_damp = area_linear_damp;
  426. linear_damp_done = area_linear_damp_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE;
  427. } break;
  428. default: {
  429. }
  430. }
  431. }
  432. }
  433. if (!angular_damp_done) {
  434. PhysicsServer2D::AreaSpaceOverrideMode area_angular_damp_mode = (PhysicsServer2D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE);
  435. if (area_angular_damp_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
  436. real_t area_angular_damp = aa[i].area->get_angular_damp();
  437. switch (area_angular_damp_mode) {
  438. case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE:
  439. case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
  440. total_angular_damp += area_angular_damp;
  441. angular_damp_done = area_angular_damp_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
  442. } break;
  443. case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE:
  444. case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
  445. total_angular_damp = area_angular_damp;
  446. angular_damp_done = area_angular_damp_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE;
  447. } break;
  448. default: {
  449. }
  450. }
  451. }
  452. }
  453. stopped = gravity_done && linear_damp_done && angular_damp_done;
  454. }
  455. }
  456. // Add default gravity and damping from space area.
  457. if (!stopped) {
  458. GodotArea2D *default_area = get_space()->get_default_area();
  459. ERR_FAIL_NULL(default_area);
  460. if (!gravity_done) {
  461. Vector2 default_gravity;
  462. default_area->compute_gravity(get_transform().get_origin(), default_gravity);
  463. gravity += default_gravity;
  464. }
  465. if (!linear_damp_done) {
  466. total_linear_damp += default_area->get_linear_damp();
  467. }
  468. if (!angular_damp_done) {
  469. total_angular_damp += default_area->get_angular_damp();
  470. }
  471. }
  472. // Override linear damping with body's value.
  473. switch (linear_damp_mode) {
  474. case PhysicsServer2D::BODY_DAMP_MODE_COMBINE: {
  475. total_linear_damp += linear_damp;
  476. } break;
  477. case PhysicsServer2D::BODY_DAMP_MODE_REPLACE: {
  478. total_linear_damp = linear_damp;
  479. } break;
  480. }
  481. // Override angular damping with body's value.
  482. switch (angular_damp_mode) {
  483. case PhysicsServer2D::BODY_DAMP_MODE_COMBINE: {
  484. total_angular_damp += angular_damp;
  485. } break;
  486. case PhysicsServer2D::BODY_DAMP_MODE_REPLACE: {
  487. total_angular_damp = angular_damp;
  488. } break;
  489. }
  490. gravity *= gravity_scale;
  491. prev_linear_velocity = linear_velocity;
  492. prev_angular_velocity = angular_velocity;
  493. Vector2 motion;
  494. bool do_motion = false;
  495. if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  496. //compute motion, angular and etc. velocities from prev transform
  497. motion = new_transform.get_origin() - get_transform().get_origin();
  498. linear_velocity = constant_linear_velocity + motion / p_step;
  499. real_t rot = new_transform.get_rotation() - get_transform().get_rotation();
  500. angular_velocity = constant_angular_velocity + remainder(rot, 2.0 * Math_PI) / p_step;
  501. do_motion = true;
  502. } else {
  503. if (!omit_force_integration) {
  504. //overridden by direct state query
  505. Vector2 force = gravity * mass + applied_force + constant_force;
  506. real_t torque = applied_torque + constant_torque;
  507. real_t damp = 1.0 - p_step * total_linear_damp;
  508. if (damp < 0) { // reached zero in the given time
  509. damp = 0;
  510. }
  511. real_t angular_damp_new = 1.0 - p_step * total_angular_damp;
  512. if (angular_damp_new < 0) { // reached zero in the given time
  513. angular_damp_new = 0;
  514. }
  515. linear_velocity *= damp;
  516. angular_velocity *= angular_damp_new;
  517. linear_velocity += _inv_mass * force * p_step;
  518. angular_velocity += _inv_inertia * torque * p_step;
  519. }
  520. if (continuous_cd_mode != PhysicsServer2D::CCD_MODE_DISABLED) {
  521. motion = linear_velocity * p_step;
  522. do_motion = true;
  523. }
  524. }
  525. applied_force = Vector2();
  526. applied_torque = 0.0;
  527. biased_angular_velocity = 0.0;
  528. biased_linear_velocity = Vector2();
  529. if (do_motion) { //shapes temporarily extend for raycast
  530. _update_shapes_with_motion(motion);
  531. }
  532. contact_count = 0;
  533. }
  534. void GodotBody2D::integrate_velocities(real_t p_step) {
  535. if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
  536. return;
  537. }
  538. ERR_FAIL_NULL(get_space());
  539. if (fi_callback_data || body_state_callback.is_valid()) {
  540. get_space()->body_add_to_state_query_list(&direct_state_query_list);
  541. }
  542. if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  543. _set_transform(new_transform, false);
  544. _set_inv_transform(new_transform.affine_inverse());
  545. if (contacts.size() == 0 && linear_velocity == Vector2() && angular_velocity == 0) {
  546. set_active(false); //stopped moving, deactivate
  547. }
  548. return;
  549. }
  550. real_t total_angular_velocity = angular_velocity + biased_angular_velocity;
  551. Vector2 total_linear_velocity = linear_velocity + biased_linear_velocity;
  552. real_t angle_delta = total_angular_velocity * p_step;
  553. real_t angle = get_transform().get_rotation() + angle_delta;
  554. Vector2 pos = get_transform().get_origin() + total_linear_velocity * p_step;
  555. if (center_of_mass.length_squared() > CMP_EPSILON2) {
  556. // Calculate displacement due to center of mass offset.
  557. pos += center_of_mass - center_of_mass.rotated(angle_delta);
  558. }
  559. _set_transform(Transform2D(angle, pos), continuous_cd_mode == PhysicsServer2D::CCD_MODE_DISABLED);
  560. _set_inv_transform(get_transform().inverse());
  561. if (continuous_cd_mode != PhysicsServer2D::CCD_MODE_DISABLED) {
  562. new_transform = get_transform();
  563. }
  564. _update_transform_dependent();
  565. }
  566. void GodotBody2D::wakeup_neighbours() {
  567. for (const Pair<GodotConstraint2D *, int> &E : constraint_list) {
  568. const GodotConstraint2D *c = E.first;
  569. GodotBody2D **n = c->get_body_ptr();
  570. int bc = c->get_body_count();
  571. for (int i = 0; i < bc; i++) {
  572. if (i == E.second) {
  573. continue;
  574. }
  575. GodotBody2D *b = n[i];
  576. if (b->mode < PhysicsServer2D::BODY_MODE_RIGID) {
  577. continue;
  578. }
  579. if (!b->is_active()) {
  580. b->set_active(true);
  581. }
  582. }
  583. }
  584. }
  585. void GodotBody2D::call_queries() {
  586. Variant direct_state_variant = get_direct_state();
  587. if (fi_callback_data) {
  588. if (!fi_callback_data->callable.is_valid()) {
  589. set_force_integration_callback(Callable());
  590. } else {
  591. const Variant *vp[2] = { &direct_state_variant, &fi_callback_data->udata };
  592. Callable::CallError ce;
  593. Variant rv;
  594. if (fi_callback_data->udata.get_type() != Variant::NIL) {
  595. fi_callback_data->callable.callp(vp, 2, rv, ce);
  596. } else {
  597. fi_callback_data->callable.callp(vp, 1, rv, ce);
  598. }
  599. }
  600. }
  601. if (body_state_callback.is_valid()) {
  602. body_state_callback.call(direct_state_variant);
  603. }
  604. }
  605. bool GodotBody2D::sleep_test(real_t p_step) {
  606. if (mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  607. return true;
  608. } else if (!can_sleep) {
  609. return false;
  610. }
  611. ERR_FAIL_NULL_V(get_space(), true);
  612. if (Math::abs(angular_velocity) < get_space()->get_body_angular_velocity_sleep_threshold() && Math::abs(linear_velocity.length_squared()) < get_space()->get_body_linear_velocity_sleep_threshold() * get_space()->get_body_linear_velocity_sleep_threshold()) {
  613. still_time += p_step;
  614. return still_time > get_space()->get_body_time_to_sleep();
  615. } else {
  616. still_time = 0; //maybe this should be set to 0 on set_active?
  617. return false;
  618. }
  619. }
  620. void GodotBody2D::set_state_sync_callback(const Callable &p_callable) {
  621. body_state_callback = p_callable;
  622. }
  623. void GodotBody2D::set_force_integration_callback(const Callable &p_callable, const Variant &p_udata) {
  624. if (p_callable.is_valid()) {
  625. if (!fi_callback_data) {
  626. fi_callback_data = memnew(ForceIntegrationCallbackData);
  627. }
  628. fi_callback_data->callable = p_callable;
  629. fi_callback_data->udata = p_udata;
  630. } else if (fi_callback_data) {
  631. memdelete(fi_callback_data);
  632. fi_callback_data = nullptr;
  633. }
  634. }
  635. GodotPhysicsDirectBodyState2D *GodotBody2D::get_direct_state() {
  636. if (!direct_state) {
  637. direct_state = memnew(GodotPhysicsDirectBodyState2D);
  638. direct_state->body = this;
  639. }
  640. return direct_state;
  641. }
  642. GodotBody2D::GodotBody2D() :
  643. GodotCollisionObject2D(TYPE_BODY),
  644. active_list(this),
  645. mass_properties_update_list(this),
  646. direct_state_query_list(this) {
  647. _set_static(false);
  648. }
  649. GodotBody2D::~GodotBody2D() {
  650. if (fi_callback_data) {
  651. memdelete(fi_callback_data);
  652. }
  653. if (direct_state) {
  654. memdelete(direct_state);
  655. }
  656. }