godot_body_3d.cpp 22 KB

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