body_2d_sw.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*************************************************************************/
  2. /* body_2d_sw.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 "body_2d_sw.h"
  31. #include "area_2d_sw.h"
  32. #include "physics_server_2d_sw.h"
  33. #include "space_2d_sw.h"
  34. void Body2DSW::_update_inertia() {
  35. if (!user_inertia && get_space() && !inertia_update_list.in_list()) {
  36. get_space()->body_add_to_inertia_update_list(&inertia_update_list);
  37. }
  38. }
  39. void Body2DSW::update_inertias() {
  40. //update shapes and motions
  41. switch (mode) {
  42. case PhysicsServer2D::BODY_MODE_RIGID: {
  43. if (user_inertia) {
  44. _inv_inertia = inertia > 0 ? (1.0 / inertia) : 0;
  45. break;
  46. }
  47. //update tensor for allshapes, not the best way but should be somehow OK. (inspired from bullet)
  48. real_t total_area = 0;
  49. for (int i = 0; i < get_shape_count(); i++) {
  50. total_area += get_shape_aabb(i).get_area();
  51. }
  52. inertia = 0;
  53. for (int i = 0; i < get_shape_count(); i++) {
  54. if (is_shape_disabled(i)) {
  55. continue;
  56. }
  57. const Shape2DSW *shape = get_shape(i);
  58. real_t area = get_shape_aabb(i).get_area();
  59. real_t mass = area * this->mass / total_area;
  60. Transform2D mtx = get_shape_transform(i);
  61. Vector2 scale = mtx.get_scale();
  62. inertia += shape->get_moment_of_inertia(mass, scale) + mass * mtx.get_origin().length_squared();
  63. }
  64. _inv_inertia = inertia > 0 ? (1.0 / inertia) : 0;
  65. if (mass) {
  66. _inv_mass = 1.0 / mass;
  67. } else {
  68. _inv_mass = 0;
  69. }
  70. } break;
  71. case PhysicsServer2D::BODY_MODE_KINEMATIC:
  72. case PhysicsServer2D::BODY_MODE_STATIC: {
  73. _inv_inertia = 0;
  74. _inv_mass = 0;
  75. } break;
  76. case PhysicsServer2D::BODY_MODE_CHARACTER: {
  77. _inv_inertia = 0;
  78. _inv_mass = 1.0 / mass;
  79. } break;
  80. }
  81. //_update_inertia_tensor();
  82. //_update_shapes();
  83. }
  84. void Body2DSW::set_active(bool p_active) {
  85. if (active == p_active) {
  86. return;
  87. }
  88. active = p_active;
  89. if (!p_active) {
  90. if (get_space()) {
  91. get_space()->body_remove_from_active_list(&active_list);
  92. }
  93. } else {
  94. if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
  95. return; //static bodies can't become active
  96. }
  97. if (get_space()) {
  98. get_space()->body_add_to_active_list(&active_list);
  99. }
  100. //still_time=0;
  101. }
  102. /*
  103. if (!space)
  104. return;
  105. for(int i=0;i<get_shape_count();i++) {
  106. Shape &s=shapes[i];
  107. if (s.bpid>0) {
  108. get_space()->get_broadphase()->set_active(s.bpid,active);
  109. }
  110. }
  111. */
  112. }
  113. void Body2DSW::set_param(PhysicsServer2D::BodyParameter p_param, real_t p_value) {
  114. switch (p_param) {
  115. case PhysicsServer2D::BODY_PARAM_BOUNCE: {
  116. bounce = p_value;
  117. } break;
  118. case PhysicsServer2D::BODY_PARAM_FRICTION: {
  119. friction = p_value;
  120. } break;
  121. case PhysicsServer2D::BODY_PARAM_MASS: {
  122. ERR_FAIL_COND(p_value <= 0);
  123. mass = p_value;
  124. _update_inertia();
  125. } break;
  126. case PhysicsServer2D::BODY_PARAM_INERTIA: {
  127. if (p_value <= 0) {
  128. user_inertia = false;
  129. _update_inertia();
  130. } else {
  131. user_inertia = true;
  132. inertia = p_value;
  133. _inv_inertia = 1.0 / p_value;
  134. }
  135. } break;
  136. case PhysicsServer2D::BODY_PARAM_GRAVITY_SCALE: {
  137. gravity_scale = p_value;
  138. } break;
  139. case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP: {
  140. linear_damp = p_value;
  141. } break;
  142. case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP: {
  143. angular_damp = p_value;
  144. } break;
  145. default: {
  146. }
  147. }
  148. }
  149. real_t Body2DSW::get_param(PhysicsServer2D::BodyParameter p_param) const {
  150. switch (p_param) {
  151. case PhysicsServer2D::BODY_PARAM_BOUNCE: {
  152. return bounce;
  153. }
  154. case PhysicsServer2D::BODY_PARAM_FRICTION: {
  155. return friction;
  156. }
  157. case PhysicsServer2D::BODY_PARAM_MASS: {
  158. return mass;
  159. }
  160. case PhysicsServer2D::BODY_PARAM_INERTIA: {
  161. return inertia;
  162. }
  163. case PhysicsServer2D::BODY_PARAM_GRAVITY_SCALE: {
  164. return gravity_scale;
  165. }
  166. case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP: {
  167. return linear_damp;
  168. }
  169. case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP: {
  170. return angular_damp;
  171. }
  172. default: {
  173. }
  174. }
  175. return 0;
  176. }
  177. void Body2DSW::set_mode(PhysicsServer2D::BodyMode p_mode) {
  178. PhysicsServer2D::BodyMode prev = mode;
  179. mode = p_mode;
  180. switch (p_mode) {
  181. //CLEAR UP EVERYTHING IN CASE IT NOT WORKS!
  182. case PhysicsServer2D::BODY_MODE_STATIC:
  183. case PhysicsServer2D::BODY_MODE_KINEMATIC: {
  184. _set_inv_transform(get_transform().affine_inverse());
  185. _inv_mass = 0;
  186. _inv_inertia = 0;
  187. _set_static(p_mode == PhysicsServer2D::BODY_MODE_STATIC);
  188. set_active(p_mode == PhysicsServer2D::BODY_MODE_KINEMATIC && contacts.size());
  189. linear_velocity = Vector2();
  190. angular_velocity = 0;
  191. if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC && prev != mode) {
  192. first_time_kinematic = true;
  193. }
  194. } break;
  195. case PhysicsServer2D::BODY_MODE_RIGID: {
  196. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  197. _inv_inertia = inertia > 0 ? (1.0 / inertia) : 0;
  198. _set_static(false);
  199. set_active(true);
  200. } break;
  201. case PhysicsServer2D::BODY_MODE_CHARACTER: {
  202. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  203. _inv_inertia = 0;
  204. _set_static(false);
  205. set_active(true);
  206. angular_velocity = 0;
  207. } break;
  208. }
  209. if (p_mode == PhysicsServer2D::BODY_MODE_RIGID && _inv_inertia == 0) {
  210. _update_inertia();
  211. }
  212. /*
  213. if (get_space())
  214. _update_queries();
  215. */
  216. }
  217. PhysicsServer2D::BodyMode Body2DSW::get_mode() const {
  218. return mode;
  219. }
  220. void Body2DSW::_shapes_changed() {
  221. _update_inertia();
  222. wakeup_neighbours();
  223. }
  224. void Body2DSW::set_state(PhysicsServer2D::BodyState p_state, const Variant &p_variant) {
  225. switch (p_state) {
  226. case PhysicsServer2D::BODY_STATE_TRANSFORM: {
  227. if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  228. new_transform = p_variant;
  229. //wakeup_neighbours();
  230. set_active(true);
  231. if (first_time_kinematic) {
  232. _set_transform(p_variant);
  233. _set_inv_transform(get_transform().affine_inverse());
  234. first_time_kinematic = false;
  235. }
  236. } else if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
  237. _set_transform(p_variant);
  238. _set_inv_transform(get_transform().affine_inverse());
  239. wakeup_neighbours();
  240. } else {
  241. Transform2D t = p_variant;
  242. t.orthonormalize();
  243. new_transform = get_transform(); //used as old to compute motion
  244. if (t == new_transform) {
  245. break;
  246. }
  247. _set_transform(t);
  248. _set_inv_transform(get_transform().inverse());
  249. }
  250. wakeup();
  251. } break;
  252. case PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY: {
  253. /*
  254. if (mode==PhysicsServer2D::BODY_MODE_STATIC)
  255. break;
  256. */
  257. linear_velocity = p_variant;
  258. wakeup();
  259. } break;
  260. case PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY: {
  261. /*
  262. if (mode!=PhysicsServer2D::BODY_MODE_RIGID)
  263. break;
  264. */
  265. angular_velocity = p_variant;
  266. wakeup();
  267. } break;
  268. case PhysicsServer2D::BODY_STATE_SLEEPING: {
  269. //?
  270. if (mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  271. break;
  272. }
  273. bool do_sleep = p_variant;
  274. if (do_sleep) {
  275. linear_velocity = Vector2();
  276. //biased_linear_velocity=Vector3();
  277. angular_velocity = 0;
  278. //biased_angular_velocity=Vector3();
  279. set_active(false);
  280. } else {
  281. if (mode != PhysicsServer2D::BODY_MODE_STATIC) {
  282. set_active(true);
  283. }
  284. }
  285. } break;
  286. case PhysicsServer2D::BODY_STATE_CAN_SLEEP: {
  287. can_sleep = p_variant;
  288. if (mode == PhysicsServer2D::BODY_MODE_RIGID && !active && !can_sleep) {
  289. set_active(true);
  290. }
  291. } break;
  292. }
  293. }
  294. Variant Body2DSW::get_state(PhysicsServer2D::BodyState p_state) const {
  295. switch (p_state) {
  296. case PhysicsServer2D::BODY_STATE_TRANSFORM: {
  297. return get_transform();
  298. }
  299. case PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY: {
  300. return linear_velocity;
  301. }
  302. case PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY: {
  303. return angular_velocity;
  304. }
  305. case PhysicsServer2D::BODY_STATE_SLEEPING: {
  306. return !is_active();
  307. }
  308. case PhysicsServer2D::BODY_STATE_CAN_SLEEP: {
  309. return can_sleep;
  310. }
  311. }
  312. return Variant();
  313. }
  314. void Body2DSW::set_space(Space2DSW *p_space) {
  315. if (get_space()) {
  316. wakeup_neighbours();
  317. if (inertia_update_list.in_list()) {
  318. get_space()->body_remove_from_inertia_update_list(&inertia_update_list);
  319. }
  320. if (active_list.in_list()) {
  321. get_space()->body_remove_from_active_list(&active_list);
  322. }
  323. if (direct_state_query_list.in_list()) {
  324. get_space()->body_remove_from_state_query_list(&direct_state_query_list);
  325. }
  326. }
  327. _set_space(p_space);
  328. if (get_space()) {
  329. _update_inertia();
  330. if (active) {
  331. get_space()->body_add_to_active_list(&active_list);
  332. }
  333. /*
  334. _update_queries();
  335. if (is_active()) {
  336. active=false;
  337. set_active(true);
  338. }
  339. */
  340. }
  341. first_integration = false;
  342. }
  343. void Body2DSW::_compute_area_gravity_and_dampenings(const Area2DSW *p_area) {
  344. if (p_area->is_gravity_point()) {
  345. if (p_area->get_gravity_distance_scale() > 0) {
  346. Vector2 v = p_area->get_transform().xform(p_area->get_gravity_vector()) - get_transform().get_origin();
  347. gravity += v.normalized() * (p_area->get_gravity() / Math::pow(v.length() * p_area->get_gravity_distance_scale() + 1, 2));
  348. } else {
  349. gravity += (p_area->get_transform().xform(p_area->get_gravity_vector()) - get_transform().get_origin()).normalized() * p_area->get_gravity();
  350. }
  351. } else {
  352. gravity += p_area->get_gravity_vector() * p_area->get_gravity();
  353. }
  354. area_linear_damp += p_area->get_linear_damp();
  355. area_angular_damp += p_area->get_angular_damp();
  356. }
  357. void Body2DSW::integrate_forces(real_t p_step) {
  358. if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
  359. return;
  360. }
  361. Area2DSW *def_area = get_space()->get_default_area();
  362. // Area2DSW *damp_area = def_area;
  363. ERR_FAIL_COND(!def_area);
  364. int ac = areas.size();
  365. bool stopped = false;
  366. gravity = Vector2(0, 0);
  367. area_angular_damp = 0;
  368. area_linear_damp = 0;
  369. if (ac) {
  370. areas.sort();
  371. const AreaCMP *aa = &areas[0];
  372. // damp_area = aa[ac-1].area;
  373. for (int i = ac - 1; i >= 0 && !stopped; i--) {
  374. PhysicsServer2D::AreaSpaceOverrideMode mode = aa[i].area->get_space_override_mode();
  375. switch (mode) {
  376. case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE:
  377. case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
  378. _compute_area_gravity_and_dampenings(aa[i].area);
  379. stopped = mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
  380. } break;
  381. case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE:
  382. case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
  383. gravity = Vector2(0, 0);
  384. area_angular_damp = 0;
  385. area_linear_damp = 0;
  386. _compute_area_gravity_and_dampenings(aa[i].area);
  387. stopped = mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE;
  388. } break;
  389. default: {
  390. }
  391. }
  392. }
  393. }
  394. if (!stopped) {
  395. _compute_area_gravity_and_dampenings(def_area);
  396. }
  397. gravity *= gravity_scale;
  398. // If less than 0, override dampenings with that of the Body2D
  399. if (angular_damp >= 0) {
  400. area_angular_damp = angular_damp;
  401. }
  402. /*
  403. else
  404. area_angular_damp=damp_area->get_angular_damp();
  405. */
  406. if (linear_damp >= 0) {
  407. area_linear_damp = linear_damp;
  408. }
  409. /*
  410. else
  411. area_linear_damp=damp_area->get_linear_damp();
  412. */
  413. Vector2 motion;
  414. bool do_motion = false;
  415. if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  416. //compute motion, angular and etc. velocities from prev transform
  417. motion = new_transform.get_origin() - get_transform().get_origin();
  418. linear_velocity = motion / p_step;
  419. real_t rot = new_transform.get_rotation() - get_transform().get_rotation();
  420. angular_velocity = remainder(rot, 2.0 * Math_PI) / p_step;
  421. do_motion = true;
  422. /*
  423. for(int i=0;i<get_shape_count();i++) {
  424. set_shape_kinematic_advance(i,Vector2());
  425. set_shape_kinematic_retreat(i,0);
  426. }
  427. */
  428. } else {
  429. if (!omit_force_integration && !first_integration) {
  430. //overridden by direct state query
  431. Vector2 force = gravity * mass;
  432. force += applied_force;
  433. real_t torque = applied_torque;
  434. real_t damp = 1.0 - p_step * area_linear_damp;
  435. if (damp < 0) { // reached zero in the given time
  436. damp = 0;
  437. }
  438. real_t angular_damp = 1.0 - p_step * area_angular_damp;
  439. if (angular_damp < 0) { // reached zero in the given time
  440. angular_damp = 0;
  441. }
  442. linear_velocity *= damp;
  443. angular_velocity *= angular_damp;
  444. linear_velocity += _inv_mass * force * p_step;
  445. angular_velocity += _inv_inertia * torque * p_step;
  446. }
  447. if (continuous_cd_mode != PhysicsServer2D::CCD_MODE_DISABLED) {
  448. motion = linear_velocity * p_step;
  449. do_motion = true;
  450. }
  451. }
  452. //motion=linear_velocity*p_step;
  453. first_integration = false;
  454. biased_angular_velocity = 0;
  455. biased_linear_velocity = Vector2();
  456. if (do_motion) { //shapes temporarily extend for raycast
  457. _update_shapes_with_motion(motion);
  458. }
  459. // damp_area=nullptr; // clear the area, so it is set in the next frame
  460. def_area = nullptr; // clear the area, so it is set in the next frame
  461. contact_count = 0;
  462. }
  463. void Body2DSW::integrate_velocities(real_t p_step) {
  464. if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
  465. return;
  466. }
  467. if (fi_callback) {
  468. get_space()->body_add_to_state_query_list(&direct_state_query_list);
  469. }
  470. if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  471. _set_transform(new_transform, false);
  472. _set_inv_transform(new_transform.affine_inverse());
  473. if (contacts.size() == 0 && linear_velocity == Vector2() && angular_velocity == 0) {
  474. set_active(false); //stopped moving, deactivate
  475. }
  476. return;
  477. }
  478. real_t total_angular_velocity = angular_velocity + biased_angular_velocity;
  479. Vector2 total_linear_velocity = linear_velocity + biased_linear_velocity;
  480. real_t angle = get_transform().get_rotation() + total_angular_velocity * p_step;
  481. Vector2 pos = get_transform().get_origin() + total_linear_velocity * p_step;
  482. _set_transform(Transform2D(angle, pos), continuous_cd_mode == PhysicsServer2D::CCD_MODE_DISABLED);
  483. _set_inv_transform(get_transform().inverse());
  484. if (continuous_cd_mode != PhysicsServer2D::CCD_MODE_DISABLED) {
  485. new_transform = get_transform();
  486. }
  487. //_update_inertia_tensor();
  488. }
  489. void Body2DSW::wakeup_neighbours() {
  490. for (List<Pair<Constraint2DSW *, int>>::Element *E = constraint_list.front(); E; E = E->next()) {
  491. const Constraint2DSW *c = E->get().first;
  492. Body2DSW **n = c->get_body_ptr();
  493. int bc = c->get_body_count();
  494. for (int i = 0; i < bc; i++) {
  495. if (i == E->get().second) {
  496. continue;
  497. }
  498. Body2DSW *b = n[i];
  499. if (b->mode != PhysicsServer2D::BODY_MODE_RIGID) {
  500. continue;
  501. }
  502. if (!b->is_active()) {
  503. b->set_active(true);
  504. }
  505. }
  506. }
  507. }
  508. void Body2DSW::call_queries() {
  509. if (fi_callback) {
  510. PhysicsDirectBodyState2DSW *dbs = PhysicsDirectBodyState2DSW::singleton;
  511. dbs->body = this;
  512. Variant v = dbs;
  513. const Variant *vp[2] = { &v, &fi_callback->callback_udata };
  514. Object *obj = ObjectDB::get_instance(fi_callback->id);
  515. if (!obj) {
  516. set_force_integration_callback(ObjectID(), StringName());
  517. } else {
  518. Callable::CallError ce;
  519. if (fi_callback->callback_udata.get_type() != Variant::NIL) {
  520. obj->call(fi_callback->method, vp, 2, ce);
  521. } else {
  522. obj->call(fi_callback->method, vp, 1, ce);
  523. }
  524. }
  525. }
  526. }
  527. bool Body2DSW::sleep_test(real_t p_step) {
  528. if (mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
  529. return true; //
  530. } else if (mode == PhysicsServer2D::BODY_MODE_CHARACTER) {
  531. return !active; // characters and kinematic bodies don't sleep unless asked to sleep
  532. } else if (!can_sleep) {
  533. return false;
  534. }
  535. 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()) {
  536. still_time += p_step;
  537. return still_time > get_space()->get_body_time_to_sleep();
  538. } else {
  539. still_time = 0; //maybe this should be set to 0 on set_active?
  540. return false;
  541. }
  542. }
  543. void Body2DSW::set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata) {
  544. if (fi_callback) {
  545. memdelete(fi_callback);
  546. fi_callback = nullptr;
  547. }
  548. if (p_id.is_valid()) {
  549. fi_callback = memnew(ForceIntegrationCallback);
  550. fi_callback->id = p_id;
  551. fi_callback->method = p_method;
  552. fi_callback->callback_udata = p_udata;
  553. }
  554. }
  555. Body2DSW::Body2DSW() :
  556. CollisionObject2DSW(TYPE_BODY),
  557. active_list(this),
  558. inertia_update_list(this),
  559. direct_state_query_list(this) {
  560. mode = PhysicsServer2D::BODY_MODE_RIGID;
  561. active = true;
  562. angular_velocity = 0;
  563. biased_angular_velocity = 0;
  564. mass = 1;
  565. inertia = 0;
  566. user_inertia = false;
  567. _inv_inertia = 0;
  568. _inv_mass = 1;
  569. bounce = 0;
  570. friction = 1;
  571. omit_force_integration = false;
  572. applied_torque = 0;
  573. island_step = 0;
  574. island_next = nullptr;
  575. island_list_next = nullptr;
  576. _set_static(false);
  577. first_time_kinematic = false;
  578. linear_damp = -1;
  579. angular_damp = -1;
  580. area_angular_damp = 0;
  581. area_linear_damp = 0;
  582. contact_count = 0;
  583. gravity_scale = 1.0;
  584. first_integration = false;
  585. still_time = 0;
  586. continuous_cd_mode = PhysicsServer2D::CCD_MODE_DISABLED;
  587. can_sleep = true;
  588. fi_callback = nullptr;
  589. }
  590. Body2DSW::~Body2DSW() {
  591. if (fi_callback) {
  592. memdelete(fi_callback);
  593. }
  594. }
  595. PhysicsDirectBodyState2DSW *PhysicsDirectBodyState2DSW::singleton = nullptr;
  596. PhysicsDirectSpaceState2D *PhysicsDirectBodyState2DSW::get_space_state() {
  597. return body->get_space()->get_direct_state();
  598. }
  599. Variant PhysicsDirectBodyState2DSW::get_contact_collider_shape_metadata(int p_contact_idx) const {
  600. ERR_FAIL_INDEX_V(p_contact_idx, body->contact_count, Variant());
  601. if (!PhysicsServer2DSW::singletonsw->body_owner.owns(body->contacts[p_contact_idx].collider)) {
  602. return Variant();
  603. }
  604. Body2DSW *other = PhysicsServer2DSW::singletonsw->body_owner.getornull(body->contacts[p_contact_idx].collider);
  605. int sidx = body->contacts[p_contact_idx].collider_shape;
  606. if (sidx < 0 || sidx >= other->get_shape_count()) {
  607. return Variant();
  608. }
  609. return other->get_shape_metadata(sidx);
  610. }