2
0

godot_body_2d.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*************************************************************************/
  2. /* godot_body_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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() && (calculate_inertia || calculate_center_of_mass)) {
  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_DYNAMIC: {
  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 = area * this->mass / total_area;
  60. // NOTE: we assume that the shape origin is also its center of mass.
  61. center_of_mass_local += mass * 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 = area * this->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, scale) + mass * 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_DYNAMIC_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_DYNAMIC) {
  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_DYNAMIC) {
  145. _mass_properties_changed();
  146. }
  147. } else {
  148. calculate_inertia = false;
  149. inertia = inertia_value;
  150. if (mode == PhysicsServer2D::BODY_MODE_DYNAMIC) {
  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. gravity_scale = p_value;
  162. } break;
  163. case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP_MODE: {
  164. int mode_value = p_value;
  165. linear_damp_mode = (PhysicsServer2D::BodyDampMode)mode_value;
  166. } break;
  167. case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP_MODE: {
  168. int mode_value = p_value;
  169. angular_damp_mode = (PhysicsServer2D::BodyDampMode)mode_value;
  170. } break;
  171. case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP: {
  172. linear_damp = p_value;
  173. } break;
  174. case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP: {
  175. angular_damp = p_value;
  176. } break;
  177. default: {
  178. }
  179. }
  180. }
  181. Variant GodotBody2D::get_param(PhysicsServer2D::BodyParameter p_param) const {
  182. switch (p_param) {
  183. case PhysicsServer2D::BODY_PARAM_BOUNCE: {
  184. return bounce;
  185. }
  186. case PhysicsServer2D::BODY_PARAM_FRICTION: {
  187. return friction;
  188. }
  189. case PhysicsServer2D::BODY_PARAM_MASS: {
  190. return mass;
  191. }
  192. case PhysicsServer2D::BODY_PARAM_INERTIA: {
  193. return inertia;
  194. }
  195. case PhysicsServer2D::BODY_PARAM_CENTER_OF_MASS: {
  196. return center_of_mass;
  197. }
  198. case PhysicsServer2D::BODY_PARAM_GRAVITY_SCALE: {
  199. return gravity_scale;
  200. }
  201. case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP_MODE: {
  202. return linear_damp_mode;
  203. }
  204. case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP_MODE: {
  205. return angular_damp_mode;
  206. }
  207. case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP: {
  208. return linear_damp;
  209. }
  210. case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP: {
  211. return angular_damp;
  212. }
  213. default: {
  214. }
  215. }
  216. return 0;
  217. }
  218. void GodotBody2D::set_mode(PhysicsServer2D::BodyMode p_mode) {
  219. PhysicsServer2D::BodyMode prev = mode;
  220. mode = p_mode;
  221. switch (p_mode) {
  222. //CLEAR UP EVERYTHING IN CASE IT NOT WORKS!
  223. case PhysicsServer2D::BODY_MODE_STATIC:
  224. case PhysicsServer2D::BODY_MODE_KINEMATIC: {
  225. _set_inv_transform(get_transform().affine_inverse());
  226. _inv_mass = 0;
  227. _inv_inertia = 0;
  228. _set_static(p_mode == PhysicsServer2D::BODY_MODE_STATIC);
  229. set_active(p_mode == PhysicsServer2D::BODY_MODE_KINEMATIC && contacts.size());
  230. linear_velocity = Vector2();
  231. angular_velocity = 0;
  232. if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC && prev != mode) {
  233. first_time_kinematic = true;
  234. }
  235. } break;
  236. case PhysicsServer2D::BODY_MODE_DYNAMIC: {
  237. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  238. if (!calculate_inertia) {
  239. _inv_inertia = 1.0 / inertia;
  240. }
  241. _mass_properties_changed();
  242. _set_static(false);
  243. set_active(true);
  244. } break;
  245. case PhysicsServer2D::BODY_MODE_DYNAMIC_LINEAR: {
  246. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  247. _inv_inertia = 0;
  248. angular_velocity = 0;
  249. _set_static(false);
  250. set_active(true);
  251. }
  252. }
  253. }
  254. PhysicsServer2D::BodyMode GodotBody2D::get_mode() const {
  255. return mode;
  256. }
  257. void GodotBody2D::_shapes_changed() {
  258. _mass_properties_changed();
  259. wakeup();
  260. wakeup_neighbours();
  261. }
  262. void GodotBody2D::set_state(PhysicsServer2D::BodyState p_state, const Variant &p_variant) {
  263. switch (p_state) {
  264. case PhysicsServer2D::BODY_STATE_TRANSFORM: {
  265. if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  266. new_transform = p_variant;
  267. //wakeup_neighbours();
  268. set_active(true);
  269. if (first_time_kinematic) {
  270. _set_transform(p_variant);
  271. _set_inv_transform(get_transform().affine_inverse());
  272. first_time_kinematic = false;
  273. }
  274. } else if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
  275. _set_transform(p_variant);
  276. _set_inv_transform(get_transform().affine_inverse());
  277. wakeup_neighbours();
  278. } else {
  279. Transform2D t = p_variant;
  280. t.orthonormalize();
  281. new_transform = get_transform(); //used as old to compute motion
  282. if (t == new_transform) {
  283. break;
  284. }
  285. _set_transform(t);
  286. _set_inv_transform(get_transform().inverse());
  287. _update_transform_dependent();
  288. }
  289. wakeup();
  290. } break;
  291. case PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY: {
  292. linear_velocity = p_variant;
  293. constant_linear_velocity = linear_velocity;
  294. wakeup();
  295. } break;
  296. case PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY: {
  297. angular_velocity = p_variant;
  298. constant_angular_velocity = angular_velocity;
  299. wakeup();
  300. } break;
  301. case PhysicsServer2D::BODY_STATE_SLEEPING: {
  302. if (mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  303. break;
  304. }
  305. bool do_sleep = p_variant;
  306. if (do_sleep) {
  307. linear_velocity = Vector2();
  308. //biased_linear_velocity=Vector3();
  309. angular_velocity = 0;
  310. //biased_angular_velocity=Vector3();
  311. set_active(false);
  312. } else {
  313. if (mode != PhysicsServer2D::BODY_MODE_STATIC) {
  314. set_active(true);
  315. }
  316. }
  317. } break;
  318. case PhysicsServer2D::BODY_STATE_CAN_SLEEP: {
  319. can_sleep = p_variant;
  320. if (mode >= PhysicsServer2D::BODY_MODE_DYNAMIC && !active && !can_sleep) {
  321. set_active(true);
  322. }
  323. } break;
  324. }
  325. }
  326. Variant GodotBody2D::get_state(PhysicsServer2D::BodyState p_state) const {
  327. switch (p_state) {
  328. case PhysicsServer2D::BODY_STATE_TRANSFORM: {
  329. return get_transform();
  330. }
  331. case PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY: {
  332. return linear_velocity;
  333. }
  334. case PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY: {
  335. return angular_velocity;
  336. }
  337. case PhysicsServer2D::BODY_STATE_SLEEPING: {
  338. return !is_active();
  339. }
  340. case PhysicsServer2D::BODY_STATE_CAN_SLEEP: {
  341. return can_sleep;
  342. }
  343. }
  344. return Variant();
  345. }
  346. void GodotBody2D::set_space(GodotSpace2D *p_space) {
  347. if (get_space()) {
  348. wakeup_neighbours();
  349. if (mass_properties_update_list.in_list()) {
  350. get_space()->body_remove_from_mass_properties_update_list(&mass_properties_update_list);
  351. }
  352. if (active_list.in_list()) {
  353. get_space()->body_remove_from_active_list(&active_list);
  354. }
  355. if (direct_state_query_list.in_list()) {
  356. get_space()->body_remove_from_state_query_list(&direct_state_query_list);
  357. }
  358. }
  359. _set_space(p_space);
  360. if (get_space()) {
  361. _mass_properties_changed();
  362. if (active) {
  363. get_space()->body_add_to_active_list(&active_list);
  364. }
  365. }
  366. }
  367. void GodotBody2D::_compute_area_gravity_and_damping(const GodotArea2D *p_area) {
  368. Vector2 area_gravity;
  369. p_area->compute_gravity(get_transform().get_origin(), area_gravity);
  370. gravity += area_gravity;
  371. total_linear_damp += p_area->get_linear_damp();
  372. total_angular_damp += p_area->get_angular_damp();
  373. }
  374. void GodotBody2D::_update_transform_dependent() {
  375. center_of_mass = get_transform().basis_xform(center_of_mass_local);
  376. }
  377. void GodotBody2D::integrate_forces(real_t p_step) {
  378. if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
  379. return;
  380. }
  381. int ac = areas.size();
  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. PhysicsServer2D::AreaSpaceOverrideMode mode = aa[i].area->get_space_override_mode();
  392. switch (mode) {
  393. case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE:
  394. case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
  395. _compute_area_gravity_and_damping(aa[i].area);
  396. stopped = mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
  397. } break;
  398. case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE:
  399. case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
  400. gravity = Vector2(0, 0);
  401. total_linear_damp = 0.0;
  402. total_angular_damp = 0.0;
  403. _compute_area_gravity_and_damping(aa[i].area);
  404. stopped = mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE;
  405. } break;
  406. default: {
  407. }
  408. }
  409. }
  410. }
  411. // Override linear damping with body's value.
  412. if (!stopped) {
  413. GodotArea2D *def_area = get_space()->get_default_area();
  414. ERR_FAIL_COND(!def_area);
  415. _compute_area_gravity_and_damping(def_area);
  416. }
  417. // Override linear damping with body's value.
  418. switch (linear_damp_mode) {
  419. case PhysicsServer2D::BODY_DAMP_MODE_COMBINE: {
  420. total_linear_damp += linear_damp;
  421. } break;
  422. case PhysicsServer2D::BODY_DAMP_MODE_REPLACE: {
  423. total_linear_damp = linear_damp;
  424. } break;
  425. }
  426. // Override angular damping with body's value.
  427. switch (angular_damp_mode) {
  428. case PhysicsServer2D::BODY_DAMP_MODE_COMBINE: {
  429. total_angular_damp += angular_damp;
  430. } break;
  431. case PhysicsServer2D::BODY_DAMP_MODE_REPLACE: {
  432. total_angular_damp = angular_damp;
  433. } break;
  434. }
  435. gravity *= gravity_scale;
  436. Vector2 motion;
  437. bool do_motion = false;
  438. if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  439. //compute motion, angular and etc. velocities from prev transform
  440. motion = new_transform.get_origin() - get_transform().get_origin();
  441. linear_velocity = constant_linear_velocity + motion / p_step;
  442. real_t rot = new_transform.get_rotation() - get_transform().get_rotation();
  443. angular_velocity = constant_angular_velocity + remainder(rot, 2.0 * Math_PI) / p_step;
  444. do_motion = true;
  445. } else {
  446. if (!omit_force_integration) {
  447. //overridden by direct state query
  448. Vector2 force = gravity * mass;
  449. force += applied_force;
  450. real_t torque = applied_torque;
  451. real_t damp = 1.0 - p_step * total_linear_damp;
  452. if (damp < 0) { // reached zero in the given time
  453. damp = 0;
  454. }
  455. real_t angular_damp = 1.0 - p_step * total_angular_damp;
  456. if (angular_damp < 0) { // reached zero in the given time
  457. angular_damp = 0;
  458. }
  459. linear_velocity *= damp;
  460. angular_velocity *= angular_damp;
  461. linear_velocity += _inv_mass * force * p_step;
  462. angular_velocity += _inv_inertia * torque * p_step;
  463. }
  464. if (continuous_cd_mode != PhysicsServer2D::CCD_MODE_DISABLED) {
  465. motion = linear_velocity * p_step;
  466. do_motion = true;
  467. }
  468. }
  469. biased_angular_velocity = 0;
  470. biased_linear_velocity = Vector2();
  471. if (do_motion) { //shapes temporarily extend for raycast
  472. _update_shapes_with_motion(motion);
  473. }
  474. contact_count = 0;
  475. }
  476. void GodotBody2D::integrate_velocities(real_t p_step) {
  477. if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
  478. return;
  479. }
  480. if (fi_callback_data || body_state_callback) {
  481. get_space()->body_add_to_state_query_list(&direct_state_query_list);
  482. }
  483. if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  484. _set_transform(new_transform, false);
  485. _set_inv_transform(new_transform.affine_inverse());
  486. if (contacts.size() == 0 && linear_velocity == Vector2() && angular_velocity == 0) {
  487. set_active(false); //stopped moving, deactivate
  488. }
  489. return;
  490. }
  491. real_t total_angular_velocity = angular_velocity + biased_angular_velocity;
  492. Vector2 total_linear_velocity = linear_velocity + biased_linear_velocity;
  493. real_t angle_delta = total_angular_velocity * p_step;
  494. real_t angle = get_transform().get_rotation() + angle_delta;
  495. Vector2 pos = get_transform().get_origin() + total_linear_velocity * p_step;
  496. if (center_of_mass.length_squared() > CMP_EPSILON2) {
  497. // Calculate displacement due to center of mass offset.
  498. pos += center_of_mass - center_of_mass.rotated(angle_delta);
  499. }
  500. _set_transform(Transform2D(angle, pos), continuous_cd_mode == PhysicsServer2D::CCD_MODE_DISABLED);
  501. _set_inv_transform(get_transform().inverse());
  502. if (continuous_cd_mode != PhysicsServer2D::CCD_MODE_DISABLED) {
  503. new_transform = get_transform();
  504. }
  505. _update_transform_dependent();
  506. }
  507. void GodotBody2D::wakeup_neighbours() {
  508. for (const Pair<GodotConstraint2D *, int> &E : constraint_list) {
  509. const GodotConstraint2D *c = E.first;
  510. GodotBody2D **n = c->get_body_ptr();
  511. int bc = c->get_body_count();
  512. for (int i = 0; i < bc; i++) {
  513. if (i == E.second) {
  514. continue;
  515. }
  516. GodotBody2D *b = n[i];
  517. if (b->mode < PhysicsServer2D::BODY_MODE_DYNAMIC) {
  518. continue;
  519. }
  520. if (!b->is_active()) {
  521. b->set_active(true);
  522. }
  523. }
  524. }
  525. }
  526. void GodotBody2D::call_queries() {
  527. if (fi_callback_data) {
  528. if (!fi_callback_data->callable.get_object()) {
  529. set_force_integration_callback(Callable());
  530. } else {
  531. Variant direct_state_variant = get_direct_state();
  532. const Variant *vp[2] = { &direct_state_variant, &fi_callback_data->udata };
  533. Callable::CallError ce;
  534. Variant rv;
  535. if (fi_callback_data->udata.get_type() != Variant::NIL) {
  536. fi_callback_data->callable.call(vp, 2, rv, ce);
  537. } else {
  538. fi_callback_data->callable.call(vp, 1, rv, ce);
  539. }
  540. }
  541. }
  542. if (body_state_callback) {
  543. (body_state_callback)(body_state_callback_instance, get_direct_state());
  544. }
  545. }
  546. bool GodotBody2D::sleep_test(real_t p_step) {
  547. if (mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  548. return true;
  549. } else if (!can_sleep) {
  550. return false;
  551. }
  552. 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()) {
  553. still_time += p_step;
  554. return still_time > get_space()->get_body_time_to_sleep();
  555. } else {
  556. still_time = 0; //maybe this should be set to 0 on set_active?
  557. return false;
  558. }
  559. }
  560. void GodotBody2D::set_state_sync_callback(void *p_instance, PhysicsServer2D::BodyStateCallback p_callback) {
  561. body_state_callback_instance = p_instance;
  562. body_state_callback = p_callback;
  563. }
  564. void GodotBody2D::set_force_integration_callback(const Callable &p_callable, const Variant &p_udata) {
  565. if (p_callable.get_object()) {
  566. if (!fi_callback_data) {
  567. fi_callback_data = memnew(ForceIntegrationCallbackData);
  568. }
  569. fi_callback_data->callable = p_callable;
  570. fi_callback_data->udata = p_udata;
  571. } else if (fi_callback_data) {
  572. memdelete(fi_callback_data);
  573. fi_callback_data = nullptr;
  574. }
  575. }
  576. GodotPhysicsDirectBodyState2D *GodotBody2D::get_direct_state() {
  577. if (!direct_state) {
  578. direct_state = memnew(GodotPhysicsDirectBodyState2D);
  579. direct_state->body = this;
  580. }
  581. return direct_state;
  582. }
  583. GodotBody2D::GodotBody2D() :
  584. GodotCollisionObject2D(TYPE_BODY),
  585. active_list(this),
  586. mass_properties_update_list(this),
  587. direct_state_query_list(this) {
  588. _set_static(false);
  589. }
  590. GodotBody2D::~GodotBody2D() {
  591. if (fi_callback_data) {
  592. memdelete(fi_callback_data);
  593. }
  594. if (direct_state) {
  595. memdelete(direct_state);
  596. }
  597. }