body_2d_sw.cpp 19 KB

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