body_3d_sw.cpp 21 KB

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