body_sw.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*************************************************************************/
  2. /* body_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_sw.h"
  31. #include "area_sw.h"
  32. #include "space_sw.h"
  33. void BodySW::_update_inertia() {
  34. if (get_space() && !inertia_update_list.in_list()) {
  35. get_space()->body_add_to_inertia_update_list(&inertia_update_list);
  36. }
  37. }
  38. void BodySW::_update_transform_dependant() {
  39. center_of_mass = get_transform().basis.xform(center_of_mass_local);
  40. principal_inertia_axes = get_transform().basis * principal_inertia_axes_local;
  41. // update inertia tensor
  42. Basis tb = principal_inertia_axes;
  43. Basis tbt = tb.transposed();
  44. Basis diag;
  45. diag.scale(_inv_inertia);
  46. _inv_inertia_tensor = tb * diag * tbt;
  47. }
  48. void BodySW::update_inertias() {
  49. // Update shapes and motions.
  50. switch (mode) {
  51. case PhysicsServer::BODY_MODE_RIGID: {
  52. // Update tensor for all shapes, not the best way but should be somehow OK. (inspired from bullet)
  53. real_t total_area = 0;
  54. for (int i = 0; i < get_shape_count(); i++) {
  55. if (is_shape_disabled(i)) {
  56. continue;
  57. }
  58. total_area += get_shape_area(i);
  59. }
  60. // We have to recompute the center of mass.
  61. center_of_mass_local.zero();
  62. if (total_area != 0.0) {
  63. for (int i = 0; i < get_shape_count(); i++) {
  64. if (is_shape_disabled(i)) {
  65. continue;
  66. }
  67. real_t area = get_shape_area(i);
  68. real_t mass = area * this->mass / total_area;
  69. // NOTE: we assume that the shape origin is also its center of mass.
  70. center_of_mass_local += mass * get_shape_transform(i).origin;
  71. }
  72. center_of_mass_local /= mass;
  73. }
  74. // Recompute the inertia tensor.
  75. Basis inertia_tensor;
  76. inertia_tensor.set_zero();
  77. bool inertia_set = false;
  78. for (int i = 0; i < get_shape_count(); i++) {
  79. if (is_shape_disabled(i)) {
  80. continue;
  81. }
  82. real_t area = get_shape_area(i);
  83. if (area == 0.0) {
  84. continue;
  85. }
  86. inertia_set = true;
  87. const ShapeSW *shape = get_shape(i);
  88. real_t mass = area * this->mass / total_area;
  89. Basis shape_inertia_tensor = shape->get_moment_of_inertia(mass).to_diagonal_matrix();
  90. Transform shape_transform = get_shape_transform(i);
  91. Basis shape_basis = shape_transform.basis.orthonormalized();
  92. // NOTE: we don't take the scale of collision shapes into account when computing the inertia tensor!
  93. shape_inertia_tensor = shape_basis * shape_inertia_tensor * shape_basis.transposed();
  94. Vector3 shape_origin = shape_transform.origin - center_of_mass_local;
  95. inertia_tensor += shape_inertia_tensor + (Basis() * shape_origin.dot(shape_origin) - shape_origin.outer(shape_origin)) * mass;
  96. }
  97. // Set the inertia to a valid value when there are no valid shapes.
  98. if (!inertia_set) {
  99. inertia_tensor.set_diagonal(Vector3(1.0, 1.0, 1.0));
  100. }
  101. // Compute the principal axes of inertia.
  102. principal_inertia_axes_local = inertia_tensor.diagonalize().transposed();
  103. _inv_inertia = inertia_tensor.get_main_diagonal().inverse();
  104. if (mass) {
  105. _inv_mass = 1.0 / mass;
  106. } else {
  107. _inv_mass = 0;
  108. }
  109. } break;
  110. case PhysicsServer::BODY_MODE_KINEMATIC:
  111. case PhysicsServer::BODY_MODE_STATIC: {
  112. _inv_inertia_tensor.set_zero();
  113. _inv_mass = 0;
  114. } break;
  115. case PhysicsServer::BODY_MODE_CHARACTER: {
  116. _inv_inertia_tensor.set_zero();
  117. _inv_mass = 1.0 / mass;
  118. } break;
  119. }
  120. //_update_shapes();
  121. _update_transform_dependant();
  122. }
  123. void BodySW::set_active(bool p_active) {
  124. if (active == p_active) {
  125. return;
  126. }
  127. active = p_active;
  128. if (!p_active) {
  129. if (get_space()) {
  130. get_space()->body_remove_from_active_list(&active_list);
  131. }
  132. } else {
  133. if (mode == PhysicsServer::BODY_MODE_STATIC) {
  134. return; //static bodies can't become active
  135. }
  136. if (get_space()) {
  137. get_space()->body_add_to_active_list(&active_list);
  138. }
  139. //still_time=0;
  140. }
  141. /*
  142. if (!space)
  143. return;
  144. for(int i=0;i<get_shape_count();i++) {
  145. Shape &s=shapes[i];
  146. if (s.bpid>0) {
  147. get_space()->get_broadphase()->set_active(s.bpid,active);
  148. }
  149. }
  150. */
  151. }
  152. void BodySW::set_param(PhysicsServer::BodyParameter p_param, real_t p_value) {
  153. switch (p_param) {
  154. case PhysicsServer::BODY_PARAM_BOUNCE: {
  155. bounce = p_value;
  156. } break;
  157. case PhysicsServer::BODY_PARAM_FRICTION: {
  158. friction = p_value;
  159. } break;
  160. case PhysicsServer::BODY_PARAM_MASS: {
  161. ERR_FAIL_COND(p_value <= 0);
  162. mass = p_value;
  163. _update_inertia();
  164. } break;
  165. case PhysicsServer::BODY_PARAM_GRAVITY_SCALE: {
  166. gravity_scale = p_value;
  167. } break;
  168. case PhysicsServer::BODY_PARAM_LINEAR_DAMP: {
  169. linear_damp = p_value;
  170. } break;
  171. case PhysicsServer::BODY_PARAM_ANGULAR_DAMP: {
  172. angular_damp = p_value;
  173. } break;
  174. default: {
  175. }
  176. }
  177. }
  178. real_t BodySW::get_param(PhysicsServer::BodyParameter p_param) const {
  179. switch (p_param) {
  180. case PhysicsServer::BODY_PARAM_BOUNCE: {
  181. return bounce;
  182. } break;
  183. case PhysicsServer::BODY_PARAM_FRICTION: {
  184. return friction;
  185. } break;
  186. case PhysicsServer::BODY_PARAM_MASS: {
  187. return mass;
  188. } break;
  189. case PhysicsServer::BODY_PARAM_GRAVITY_SCALE: {
  190. return gravity_scale;
  191. } break;
  192. case PhysicsServer::BODY_PARAM_LINEAR_DAMP: {
  193. return linear_damp;
  194. } break;
  195. case PhysicsServer::BODY_PARAM_ANGULAR_DAMP: {
  196. return angular_damp;
  197. } break;
  198. default: {
  199. }
  200. }
  201. return 0;
  202. }
  203. void BodySW::set_mode(PhysicsServer::BodyMode p_mode) {
  204. PhysicsServer::BodyMode prev = mode;
  205. mode = p_mode;
  206. switch (p_mode) {
  207. //CLEAR UP EVERYTHING IN CASE IT NOT WORKS!
  208. case PhysicsServer::BODY_MODE_STATIC:
  209. case PhysicsServer::BODY_MODE_KINEMATIC: {
  210. _set_inv_transform(get_transform().affine_inverse());
  211. _inv_mass = 0;
  212. _set_static(p_mode == PhysicsServer::BODY_MODE_STATIC);
  213. //set_active(p_mode==PhysicsServer::BODY_MODE_KINEMATIC);
  214. set_active(p_mode == PhysicsServer::BODY_MODE_KINEMATIC && contacts.size());
  215. linear_velocity = Vector3();
  216. angular_velocity = Vector3();
  217. if (mode == PhysicsServer::BODY_MODE_KINEMATIC && prev != mode) {
  218. first_time_kinematic = true;
  219. }
  220. } break;
  221. case PhysicsServer::BODY_MODE_RIGID: {
  222. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  223. _set_static(false);
  224. set_active(true);
  225. } break;
  226. case PhysicsServer::BODY_MODE_CHARACTER: {
  227. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  228. _set_static(false);
  229. set_active(true);
  230. angular_velocity = Vector3();
  231. } break;
  232. }
  233. _update_inertia();
  234. /*
  235. if (get_space())
  236. _update_queries();
  237. */
  238. }
  239. PhysicsServer::BodyMode BodySW::get_mode() const {
  240. return mode;
  241. }
  242. void BodySW::_shapes_changed() {
  243. _update_inertia();
  244. }
  245. void BodySW::set_state(PhysicsServer::BodyState p_state, const Variant &p_variant) {
  246. switch (p_state) {
  247. case PhysicsServer::BODY_STATE_TRANSFORM: {
  248. if (mode == PhysicsServer::BODY_MODE_KINEMATIC) {
  249. new_transform = p_variant;
  250. //wakeup_neighbours();
  251. set_active(true);
  252. if (first_time_kinematic) {
  253. _set_transform(p_variant);
  254. _set_inv_transform(get_transform().affine_inverse());
  255. first_time_kinematic = false;
  256. }
  257. } else if (mode == PhysicsServer::BODY_MODE_STATIC) {
  258. _set_transform(p_variant);
  259. _set_inv_transform(get_transform().affine_inverse());
  260. wakeup_neighbours();
  261. } else {
  262. Transform t = p_variant;
  263. t.orthonormalize();
  264. new_transform = get_transform(); //used as old to compute motion
  265. if (new_transform == t) {
  266. break;
  267. }
  268. _set_transform(t);
  269. _set_inv_transform(get_transform().inverse());
  270. }
  271. wakeup();
  272. } break;
  273. case PhysicsServer::BODY_STATE_LINEAR_VELOCITY: {
  274. /*
  275. if (mode==PhysicsServer::BODY_MODE_STATIC)
  276. break;
  277. */
  278. linear_velocity = p_variant;
  279. wakeup();
  280. } break;
  281. case PhysicsServer::BODY_STATE_ANGULAR_VELOCITY: {
  282. /*
  283. if (mode!=PhysicsServer::BODY_MODE_RIGID)
  284. break;
  285. */
  286. angular_velocity = p_variant;
  287. wakeup();
  288. } break;
  289. case PhysicsServer::BODY_STATE_SLEEPING: {
  290. //?
  291. if (mode == PhysicsServer::BODY_MODE_STATIC || mode == PhysicsServer::BODY_MODE_KINEMATIC) {
  292. break;
  293. }
  294. bool do_sleep = p_variant;
  295. if (do_sleep) {
  296. linear_velocity = Vector3();
  297. //biased_linear_velocity=Vector3();
  298. angular_velocity = Vector3();
  299. //biased_angular_velocity=Vector3();
  300. set_active(false);
  301. } else {
  302. set_active(true);
  303. }
  304. } break;
  305. case PhysicsServer::BODY_STATE_CAN_SLEEP: {
  306. can_sleep = p_variant;
  307. if (mode == PhysicsServer::BODY_MODE_RIGID && !active && !can_sleep) {
  308. set_active(true);
  309. }
  310. } break;
  311. }
  312. }
  313. Variant BodySW::get_state(PhysicsServer::BodyState p_state) const {
  314. switch (p_state) {
  315. case PhysicsServer::BODY_STATE_TRANSFORM: {
  316. return get_transform();
  317. } break;
  318. case PhysicsServer::BODY_STATE_LINEAR_VELOCITY: {
  319. return linear_velocity;
  320. } break;
  321. case PhysicsServer::BODY_STATE_ANGULAR_VELOCITY: {
  322. return angular_velocity;
  323. } break;
  324. case PhysicsServer::BODY_STATE_SLEEPING: {
  325. return !is_active();
  326. } break;
  327. case PhysicsServer::BODY_STATE_CAN_SLEEP: {
  328. return can_sleep;
  329. } break;
  330. }
  331. return Variant();
  332. }
  333. void BodySW::set_space(SpaceSW *p_space) {
  334. if (get_space()) {
  335. if (inertia_update_list.in_list()) {
  336. get_space()->body_remove_from_inertia_update_list(&inertia_update_list);
  337. }
  338. if (active_list.in_list()) {
  339. get_space()->body_remove_from_active_list(&active_list);
  340. }
  341. if (direct_state_query_list.in_list()) {
  342. get_space()->body_remove_from_state_query_list(&direct_state_query_list);
  343. }
  344. }
  345. _set_space(p_space);
  346. if (get_space()) {
  347. _update_inertia();
  348. if (active) {
  349. get_space()->body_add_to_active_list(&active_list);
  350. }
  351. /*
  352. _update_queries();
  353. if (is_active()) {
  354. active=false;
  355. set_active(true);
  356. }
  357. */
  358. }
  359. first_integration = true;
  360. }
  361. void BodySW::_compute_area_gravity_and_dampenings(const AreaSW *p_area) {
  362. if (p_area->is_gravity_point()) {
  363. if (p_area->get_gravity_distance_scale() > 0) {
  364. Vector3 v = p_area->get_transform().xform(p_area->get_gravity_vector()) - get_transform().get_origin();
  365. gravity += v.normalized() * (p_area->get_gravity() / Math::pow(v.length() * p_area->get_gravity_distance_scale() + 1, 2));
  366. } else {
  367. gravity += (p_area->get_transform().xform(p_area->get_gravity_vector()) - get_transform().get_origin()).normalized() * p_area->get_gravity();
  368. }
  369. } else {
  370. gravity += p_area->get_gravity_vector() * p_area->get_gravity();
  371. }
  372. area_linear_damp += p_area->get_linear_damp();
  373. area_angular_damp += p_area->get_angular_damp();
  374. }
  375. void BodySW::set_axis_lock(PhysicsServer::BodyAxis p_axis, bool lock) {
  376. if (lock) {
  377. locked_axis |= p_axis;
  378. } else {
  379. locked_axis &= ~p_axis;
  380. }
  381. }
  382. bool BodySW::is_axis_locked(PhysicsServer::BodyAxis p_axis) const {
  383. return locked_axis & p_axis;
  384. }
  385. void BodySW::integrate_forces(real_t p_step) {
  386. if (mode == PhysicsServer::BODY_MODE_STATIC) {
  387. return;
  388. }
  389. AreaSW *def_area = get_space()->get_default_area();
  390. // AreaSW *damp_area = def_area;
  391. ERR_FAIL_COND(!def_area);
  392. int ac = areas.size();
  393. bool stopped = false;
  394. gravity = Vector3(0, 0, 0);
  395. area_linear_damp = 0;
  396. area_angular_damp = 0;
  397. if (ac) {
  398. areas.sort();
  399. const AreaCMP *aa = &areas[0];
  400. // damp_area = aa[ac-1].area;
  401. for (int i = ac - 1; i >= 0 && !stopped; i--) {
  402. PhysicsServer::AreaSpaceOverrideMode mode = aa[i].area->get_space_override_mode();
  403. switch (mode) {
  404. case PhysicsServer::AREA_SPACE_OVERRIDE_COMBINE:
  405. case PhysicsServer::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
  406. _compute_area_gravity_and_dampenings(aa[i].area);
  407. stopped = mode == PhysicsServer::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
  408. } break;
  409. case PhysicsServer::AREA_SPACE_OVERRIDE_REPLACE:
  410. case PhysicsServer::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
  411. gravity = Vector3(0, 0, 0);
  412. area_angular_damp = 0;
  413. area_linear_damp = 0;
  414. _compute_area_gravity_and_dampenings(aa[i].area);
  415. stopped = mode == PhysicsServer::AREA_SPACE_OVERRIDE_REPLACE;
  416. } break;
  417. default: {
  418. }
  419. }
  420. }
  421. }
  422. if (!stopped) {
  423. _compute_area_gravity_and_dampenings(def_area);
  424. }
  425. gravity *= gravity_scale;
  426. // If less than 0, override dampenings with that of the Body
  427. if (angular_damp >= 0) {
  428. area_angular_damp = angular_damp;
  429. }
  430. /*
  431. else
  432. area_angular_damp=damp_area->get_angular_damp();
  433. */
  434. if (linear_damp >= 0) {
  435. area_linear_damp = linear_damp;
  436. }
  437. /*
  438. else
  439. area_linear_damp=damp_area->get_linear_damp();
  440. */
  441. Vector3 motion;
  442. bool do_motion = false;
  443. if (mode == PhysicsServer::BODY_MODE_KINEMATIC) {
  444. //compute motion, angular and etc. velocities from prev transform
  445. motion = new_transform.origin - get_transform().origin;
  446. do_motion = true;
  447. linear_velocity = motion / p_step;
  448. //compute a FAKE angular velocity, not so easy
  449. Basis rot = new_transform.basis.orthonormalized() * get_transform().basis.orthonormalized().transposed();
  450. Vector3 axis;
  451. real_t angle;
  452. rot.get_axis_angle(axis, angle);
  453. axis.normalize();
  454. angular_velocity = axis * (angle / p_step);
  455. } else {
  456. if (!omit_force_integration && !first_integration) {
  457. //overridden by direct state query
  458. Vector3 force = gravity * mass;
  459. force += applied_force;
  460. Vector3 torque = applied_torque;
  461. real_t damp = 1.0 - p_step * area_linear_damp;
  462. if (damp < 0) { // reached zero in the given time
  463. damp = 0;
  464. }
  465. real_t angular_damp = 1.0 - p_step * area_angular_damp;
  466. if (angular_damp < 0) { // reached zero in the given time
  467. angular_damp = 0;
  468. }
  469. linear_velocity *= damp;
  470. angular_velocity *= angular_damp;
  471. linear_velocity += _inv_mass * force * p_step;
  472. angular_velocity += _inv_inertia_tensor.xform(torque) * p_step;
  473. }
  474. if (continuous_cd) {
  475. motion = linear_velocity * p_step;
  476. do_motion = true;
  477. }
  478. }
  479. applied_force = Vector3();
  480. applied_torque = Vector3();
  481. first_integration = false;
  482. //motion=linear_velocity*p_step;
  483. biased_angular_velocity = Vector3();
  484. biased_linear_velocity = Vector3();
  485. if (do_motion) { //shapes temporarily extend for raycast
  486. _update_shapes_with_motion(motion);
  487. }
  488. def_area = nullptr; // clear the area, so it is set in the next frame
  489. contact_count = 0;
  490. }
  491. void BodySW::integrate_velocities(real_t p_step) {
  492. if (mode == PhysicsServer::BODY_MODE_STATIC) {
  493. return;
  494. }
  495. if (fi_callback) {
  496. get_space()->body_add_to_state_query_list(&direct_state_query_list);
  497. }
  498. //apply axis lock linear
  499. for (int i = 0; i < 3; i++) {
  500. if (is_axis_locked((PhysicsServer::BodyAxis)(1 << i))) {
  501. linear_velocity[i] = 0;
  502. biased_linear_velocity[i] = 0;
  503. new_transform.origin[i] = get_transform().origin[i];
  504. }
  505. }
  506. //apply axis lock angular
  507. for (int i = 0; i < 3; i++) {
  508. if (is_axis_locked((PhysicsServer::BodyAxis)(1 << (i + 3)))) {
  509. angular_velocity[i] = 0;
  510. biased_angular_velocity[i] = 0;
  511. }
  512. }
  513. if (mode == PhysicsServer::BODY_MODE_KINEMATIC) {
  514. _set_transform(new_transform, false);
  515. _set_inv_transform(new_transform.affine_inverse());
  516. if (contacts.size() == 0 && linear_velocity == Vector3() && angular_velocity == Vector3()) {
  517. set_active(false); //stopped moving, deactivate
  518. }
  519. return;
  520. }
  521. Vector3 total_angular_velocity = angular_velocity + biased_angular_velocity;
  522. real_t ang_vel = total_angular_velocity.length();
  523. Transform transform = get_transform();
  524. if (!Math::is_zero_approx(ang_vel)) {
  525. Vector3 ang_vel_axis = total_angular_velocity / ang_vel;
  526. Basis rot(ang_vel_axis, ang_vel * p_step);
  527. Basis identity3(1, 0, 0, 0, 1, 0, 0, 0, 1);
  528. transform.origin += ((identity3 - rot) * transform.basis).xform(center_of_mass_local);
  529. transform.basis = rot * transform.basis;
  530. transform.orthonormalize();
  531. }
  532. Vector3 total_linear_velocity = linear_velocity + biased_linear_velocity;
  533. /*for(int i=0;i<3;i++) {
  534. if (axis_lock&(1<<i)) {
  535. transform.origin[i]=0.0;
  536. }
  537. }*/
  538. transform.origin += total_linear_velocity * p_step;
  539. _set_transform(transform);
  540. _set_inv_transform(get_transform().inverse());
  541. _update_transform_dependant();
  542. /*
  543. if (fi_callback) {
  544. get_space()->body_add_to_state_query_list(&direct_state_query_list);
  545. */
  546. }
  547. /*
  548. void BodySW::simulate_motion(const Transform& p_xform,real_t p_step) {
  549. Transform inv_xform = p_xform.affine_inverse();
  550. if (!get_space()) {
  551. _set_transform(p_xform);
  552. _set_inv_transform(inv_xform);
  553. return;
  554. }
  555. //compute a FAKE linear velocity - this is easy
  556. linear_velocity=(p_xform.origin - get_transform().origin)/p_step;
  557. //compute a FAKE angular velocity, not so easy
  558. Basis rot=get_transform().basis.orthonormalized().transposed() * p_xform.basis.orthonormalized();
  559. Vector3 axis;
  560. real_t angle;
  561. rot.get_axis_angle(axis,angle);
  562. axis.normalize();
  563. angular_velocity=axis.normalized() * (angle/p_step);
  564. linear_velocity = (p_xform.origin - get_transform().origin)/p_step;
  565. if (!direct_state_query_list.in_list())// - callalways, so lv and av are cleared && (state_query || direct_state_query))
  566. get_space()->body_add_to_state_query_list(&direct_state_query_list);
  567. simulated_motion=true;
  568. _set_transform(p_xform);
  569. }
  570. */
  571. void BodySW::wakeup_neighbours() {
  572. for (Map<ConstraintSW *, int>::Element *E = constraint_map.front(); E; E = E->next()) {
  573. const ConstraintSW *c = E->key();
  574. BodySW **n = c->get_body_ptr();
  575. int bc = c->get_body_count();
  576. for (int i = 0; i < bc; i++) {
  577. if (i == E->get()) {
  578. continue;
  579. }
  580. BodySW *b = n[i];
  581. if (b->mode != PhysicsServer::BODY_MODE_RIGID) {
  582. continue;
  583. }
  584. if (!b->is_active()) {
  585. b->set_active(true);
  586. }
  587. }
  588. }
  589. }
  590. void BodySW::call_queries() {
  591. if (fi_callback) {
  592. PhysicsDirectBodyStateSW *dbs = PhysicsDirectBodyStateSW::singleton;
  593. dbs->body = this;
  594. Variant v = dbs;
  595. Object *obj = ObjectDB::get_instance(fi_callback->id);
  596. if (!obj) {
  597. set_force_integration_callback(0, StringName());
  598. } else {
  599. const Variant *vp[2] = { &v, &fi_callback->udata };
  600. Variant::CallError ce;
  601. int argc = (fi_callback->udata.get_type() == Variant::NIL) ? 1 : 2;
  602. obj->call(fi_callback->method, vp, argc, ce);
  603. }
  604. }
  605. }
  606. bool BodySW::sleep_test(real_t p_step) {
  607. if (mode == PhysicsServer::BODY_MODE_STATIC || mode == PhysicsServer::BODY_MODE_KINEMATIC) {
  608. return true; //
  609. } else if (mode == PhysicsServer::BODY_MODE_CHARACTER) {
  610. return !active; // characters don't sleep unless asked to sleep
  611. } else if (!can_sleep) {
  612. return false;
  613. }
  614. if (Math::abs(angular_velocity.length()) < 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()) {
  615. still_time += p_step;
  616. return still_time > get_space()->get_body_time_to_sleep();
  617. } else {
  618. still_time = 0; //maybe this should be set to 0 on set_active?
  619. return false;
  620. }
  621. }
  622. void BodySW::set_force_integration_callback(ObjectID p_id, const StringName &p_method, const Variant &p_udata) {
  623. if (fi_callback) {
  624. memdelete(fi_callback);
  625. fi_callback = nullptr;
  626. }
  627. if (p_id != 0) {
  628. fi_callback = memnew(ForceIntegrationCallback);
  629. fi_callback->id = p_id;
  630. fi_callback->method = p_method;
  631. fi_callback->udata = p_udata;
  632. }
  633. }
  634. void BodySW::set_kinematic_margin(real_t p_margin) {
  635. kinematic_safe_margin = p_margin;
  636. }
  637. BodySW::BodySW() :
  638. CollisionObjectSW(TYPE_BODY),
  639. locked_axis(0),
  640. active_list(this),
  641. inertia_update_list(this),
  642. direct_state_query_list(this) {
  643. mode = PhysicsServer::BODY_MODE_RIGID;
  644. active = true;
  645. mass = 1;
  646. kinematic_safe_margin = 0.001;
  647. //_inv_inertia=Transform();
  648. _inv_mass = 1;
  649. bounce = 0;
  650. friction = 1;
  651. omit_force_integration = false;
  652. //applied_torque=0;
  653. island_step = 0;
  654. island_next = nullptr;
  655. island_list_next = nullptr;
  656. first_time_kinematic = false;
  657. first_integration = false;
  658. _set_static(false);
  659. contact_count = 0;
  660. gravity_scale = 1.0;
  661. linear_damp = -1;
  662. angular_damp = -1;
  663. area_angular_damp = 0;
  664. area_linear_damp = 0;
  665. still_time = 0;
  666. continuous_cd = false;
  667. can_sleep = true;
  668. fi_callback = nullptr;
  669. }
  670. BodySW::~BodySW() {
  671. if (fi_callback) {
  672. memdelete(fi_callback);
  673. }
  674. }
  675. PhysicsDirectBodyStateSW *PhysicsDirectBodyStateSW::singleton = nullptr;
  676. PhysicsDirectSpaceState *PhysicsDirectBodyStateSW::get_space_state() {
  677. return body->get_space()->get_direct_state();
  678. }