godot_body_2d.cpp 22 KB

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