body_2d_sw.cpp 19 KB

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