2
0

body_sw.cpp 21 KB

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