godot_body_3d.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /**************************************************************************/
  2. /* godot_body_3d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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()) {
  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_RIGID: {
  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_new = area * mass / total_area;
  70. // NOTE: we assume that the shape origin is also its center of mass.
  71. center_of_mass_local += mass_new * 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_new = area * mass / total_area;
  92. Basis shape_inertia_tensor = Basis::from_scale(shape->get_moment_of_inertia(mass_new));
  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_new;
  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_RIGID_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_RIGID) {
  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_RIGID) {
  178. _mass_properties_changed();
  179. }
  180. } else {
  181. calculate_inertia = false;
  182. if (mode == PhysicsServer3D::BODY_MODE_RIGID) {
  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. if (Math::is_zero_approx(gravity_scale)) {
  196. wakeup();
  197. }
  198. gravity_scale = p_value;
  199. } break;
  200. case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP_MODE: {
  201. int mode_value = p_value;
  202. linear_damp_mode = (PhysicsServer3D::BodyDampMode)mode_value;
  203. } break;
  204. case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP_MODE: {
  205. int mode_value = p_value;
  206. angular_damp_mode = (PhysicsServer3D::BodyDampMode)mode_value;
  207. } break;
  208. case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP: {
  209. linear_damp = p_value;
  210. } break;
  211. case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP: {
  212. angular_damp = p_value;
  213. } break;
  214. default: {
  215. }
  216. }
  217. }
  218. Variant GodotBody3D::get_param(PhysicsServer3D::BodyParameter p_param) const {
  219. switch (p_param) {
  220. case PhysicsServer3D::BODY_PARAM_BOUNCE: {
  221. return bounce;
  222. } break;
  223. case PhysicsServer3D::BODY_PARAM_FRICTION: {
  224. return friction;
  225. } break;
  226. case PhysicsServer3D::BODY_PARAM_MASS: {
  227. return mass;
  228. } break;
  229. case PhysicsServer3D::BODY_PARAM_INERTIA: {
  230. if (mode == PhysicsServer3D::BODY_MODE_RIGID) {
  231. return _inv_inertia.inverse();
  232. } else {
  233. return Vector3();
  234. }
  235. } break;
  236. case PhysicsServer3D::BODY_PARAM_CENTER_OF_MASS: {
  237. return center_of_mass_local;
  238. } break;
  239. case PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE: {
  240. return gravity_scale;
  241. } break;
  242. case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP_MODE: {
  243. return linear_damp_mode;
  244. }
  245. case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP_MODE: {
  246. return angular_damp_mode;
  247. }
  248. case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP: {
  249. return linear_damp;
  250. } break;
  251. case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP: {
  252. return angular_damp;
  253. } break;
  254. default: {
  255. }
  256. }
  257. return 0;
  258. }
  259. void GodotBody3D::set_mode(PhysicsServer3D::BodyMode p_mode) {
  260. PhysicsServer3D::BodyMode prev = mode;
  261. mode = p_mode;
  262. switch (p_mode) {
  263. case PhysicsServer3D::BODY_MODE_STATIC:
  264. case PhysicsServer3D::BODY_MODE_KINEMATIC: {
  265. _set_inv_transform(get_transform().affine_inverse());
  266. _inv_mass = 0;
  267. _inv_inertia = Vector3();
  268. _set_static(p_mode == PhysicsServer3D::BODY_MODE_STATIC);
  269. set_active(p_mode == PhysicsServer3D::BODY_MODE_KINEMATIC && contacts.size());
  270. linear_velocity = Vector3();
  271. angular_velocity = Vector3();
  272. if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC && prev != mode) {
  273. first_time_kinematic = true;
  274. }
  275. _update_transform_dependent();
  276. } break;
  277. case PhysicsServer3D::BODY_MODE_RIGID: {
  278. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  279. if (!calculate_inertia) {
  280. principal_inertia_axes_local = Basis();
  281. _inv_inertia = inertia.inverse();
  282. _update_transform_dependent();
  283. }
  284. _mass_properties_changed();
  285. _set_static(false);
  286. set_active(true);
  287. } break;
  288. case PhysicsServer3D::BODY_MODE_RIGID_LINEAR: {
  289. _inv_mass = mass > 0 ? (1.0 / mass) : 0;
  290. _inv_inertia = Vector3();
  291. angular_velocity = Vector3();
  292. _update_transform_dependent();
  293. _set_static(false);
  294. set_active(true);
  295. }
  296. }
  297. }
  298. PhysicsServer3D::BodyMode GodotBody3D::get_mode() const {
  299. return mode;
  300. }
  301. void GodotBody3D::_shapes_changed() {
  302. _mass_properties_changed();
  303. wakeup();
  304. wakeup_neighbours();
  305. }
  306. void GodotBody3D::set_state(PhysicsServer3D::BodyState p_state, const Variant &p_variant) {
  307. switch (p_state) {
  308. case PhysicsServer3D::BODY_STATE_TRANSFORM: {
  309. if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
  310. new_transform = p_variant;
  311. //wakeup_neighbours();
  312. set_active(true);
  313. if (first_time_kinematic) {
  314. _set_transform(p_variant);
  315. _set_inv_transform(get_transform().affine_inverse());
  316. first_time_kinematic = false;
  317. }
  318. } else if (mode == PhysicsServer3D::BODY_MODE_STATIC) {
  319. _set_transform(p_variant);
  320. _set_inv_transform(get_transform().affine_inverse());
  321. wakeup_neighbours();
  322. } else {
  323. Transform3D t = p_variant;
  324. t.orthonormalize();
  325. new_transform = get_transform(); //used as old to compute motion
  326. if (new_transform == t) {
  327. break;
  328. }
  329. _set_transform(t);
  330. _set_inv_transform(get_transform().inverse());
  331. _update_transform_dependent();
  332. }
  333. wakeup();
  334. } break;
  335. case PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY: {
  336. linear_velocity = p_variant;
  337. constant_linear_velocity = linear_velocity;
  338. wakeup();
  339. } break;
  340. case PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY: {
  341. angular_velocity = p_variant;
  342. constant_angular_velocity = angular_velocity;
  343. wakeup();
  344. } break;
  345. case PhysicsServer3D::BODY_STATE_SLEEPING: {
  346. if (mode == PhysicsServer3D::BODY_MODE_STATIC || mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
  347. break;
  348. }
  349. bool do_sleep = p_variant;
  350. if (do_sleep) {
  351. linear_velocity = Vector3();
  352. //biased_linear_velocity=Vector3();
  353. angular_velocity = Vector3();
  354. //biased_angular_velocity=Vector3();
  355. set_active(false);
  356. } else {
  357. set_active(true);
  358. }
  359. } break;
  360. case PhysicsServer3D::BODY_STATE_CAN_SLEEP: {
  361. can_sleep = p_variant;
  362. if (mode >= PhysicsServer3D::BODY_MODE_RIGID && !active && !can_sleep) {
  363. set_active(true);
  364. }
  365. } break;
  366. }
  367. }
  368. Variant GodotBody3D::get_state(PhysicsServer3D::BodyState p_state) const {
  369. switch (p_state) {
  370. case PhysicsServer3D::BODY_STATE_TRANSFORM: {
  371. return get_transform();
  372. } break;
  373. case PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY: {
  374. return linear_velocity;
  375. } break;
  376. case PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY: {
  377. return angular_velocity;
  378. } break;
  379. case PhysicsServer3D::BODY_STATE_SLEEPING: {
  380. return !is_active();
  381. } break;
  382. case PhysicsServer3D::BODY_STATE_CAN_SLEEP: {
  383. return can_sleep;
  384. } break;
  385. }
  386. return Variant();
  387. }
  388. void GodotBody3D::set_space(GodotSpace3D *p_space) {
  389. if (get_space()) {
  390. if (mass_properties_update_list.in_list()) {
  391. get_space()->body_remove_from_mass_properties_update_list(&mass_properties_update_list);
  392. }
  393. if (active_list.in_list()) {
  394. get_space()->body_remove_from_active_list(&active_list);
  395. }
  396. if (direct_state_query_list.in_list()) {
  397. get_space()->body_remove_from_state_query_list(&direct_state_query_list);
  398. }
  399. }
  400. _set_space(p_space);
  401. if (get_space()) {
  402. _mass_properties_changed();
  403. if (active && !active_list.in_list()) {
  404. get_space()->body_add_to_active_list(&active_list);
  405. }
  406. }
  407. }
  408. void GodotBody3D::set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool lock) {
  409. if (lock) {
  410. locked_axis |= p_axis;
  411. } else {
  412. locked_axis &= ~p_axis;
  413. }
  414. }
  415. bool GodotBody3D::is_axis_locked(PhysicsServer3D::BodyAxis p_axis) const {
  416. return locked_axis & p_axis;
  417. }
  418. void GodotBody3D::integrate_forces(real_t p_step) {
  419. if (mode == PhysicsServer3D::BODY_MODE_STATIC) {
  420. return;
  421. }
  422. ERR_FAIL_NULL(get_space());
  423. int ac = areas.size();
  424. bool gravity_done = false;
  425. bool linear_damp_done = false;
  426. bool angular_damp_done = false;
  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. if (!gravity_done) {
  437. PhysicsServer3D::AreaSpaceOverrideMode area_gravity_mode = (PhysicsServer3D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer3D::AREA_PARAM_GRAVITY_OVERRIDE_MODE);
  438. if (area_gravity_mode != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  439. Vector3 area_gravity;
  440. aa[i].area->compute_gravity(get_transform().get_origin(), area_gravity);
  441. switch (area_gravity_mode) {
  442. case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE:
  443. case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
  444. gravity += area_gravity;
  445. gravity_done = area_gravity_mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
  446. } break;
  447. case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE:
  448. case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
  449. gravity = area_gravity;
  450. gravity_done = area_gravity_mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE;
  451. } break;
  452. default: {
  453. }
  454. }
  455. }
  456. }
  457. if (!linear_damp_done) {
  458. PhysicsServer3D::AreaSpaceOverrideMode area_linear_damp_mode = (PhysicsServer3D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer3D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE);
  459. if (area_linear_damp_mode != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  460. real_t area_linear_damp = aa[i].area->get_linear_damp();
  461. switch (area_linear_damp_mode) {
  462. case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE:
  463. case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
  464. total_linear_damp += area_linear_damp;
  465. linear_damp_done = area_linear_damp_mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
  466. } break;
  467. case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE:
  468. case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
  469. total_linear_damp = area_linear_damp;
  470. linear_damp_done = area_linear_damp_mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE;
  471. } break;
  472. default: {
  473. }
  474. }
  475. }
  476. }
  477. if (!angular_damp_done) {
  478. PhysicsServer3D::AreaSpaceOverrideMode area_angular_damp_mode = (PhysicsServer3D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE);
  479. if (area_angular_damp_mode != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED) {
  480. real_t area_angular_damp = aa[i].area->get_angular_damp();
  481. switch (area_angular_damp_mode) {
  482. case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE:
  483. case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
  484. total_angular_damp += area_angular_damp;
  485. angular_damp_done = area_angular_damp_mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
  486. } break;
  487. case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE:
  488. case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
  489. total_angular_damp = area_angular_damp;
  490. angular_damp_done = area_angular_damp_mode == PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE;
  491. } break;
  492. default: {
  493. }
  494. }
  495. }
  496. }
  497. stopped = gravity_done && linear_damp_done && angular_damp_done;
  498. }
  499. }
  500. // Add default gravity and damping from space area.
  501. if (!stopped) {
  502. GodotArea3D *default_area = get_space()->get_default_area();
  503. ERR_FAIL_NULL(default_area);
  504. if (!gravity_done) {
  505. Vector3 default_gravity;
  506. default_area->compute_gravity(get_transform().get_origin(), default_gravity);
  507. gravity += default_gravity;
  508. }
  509. if (!linear_damp_done) {
  510. total_linear_damp += default_area->get_linear_damp();
  511. }
  512. if (!angular_damp_done) {
  513. total_angular_damp += default_area->get_angular_damp();
  514. }
  515. }
  516. // Override linear damping with body's value.
  517. switch (linear_damp_mode) {
  518. case PhysicsServer3D::BODY_DAMP_MODE_COMBINE: {
  519. total_linear_damp += linear_damp;
  520. } break;
  521. case PhysicsServer3D::BODY_DAMP_MODE_REPLACE: {
  522. total_linear_damp = linear_damp;
  523. } break;
  524. }
  525. // Override angular damping with body's value.
  526. switch (angular_damp_mode) {
  527. case PhysicsServer3D::BODY_DAMP_MODE_COMBINE: {
  528. total_angular_damp += angular_damp;
  529. } break;
  530. case PhysicsServer3D::BODY_DAMP_MODE_REPLACE: {
  531. total_angular_damp = angular_damp;
  532. } break;
  533. }
  534. gravity *= gravity_scale;
  535. prev_linear_velocity = linear_velocity;
  536. prev_angular_velocity = angular_velocity;
  537. Vector3 motion;
  538. bool do_motion = false;
  539. if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
  540. //compute motion, angular and etc. velocities from prev transform
  541. motion = new_transform.origin - get_transform().origin;
  542. do_motion = true;
  543. linear_velocity = constant_linear_velocity + motion / p_step;
  544. //compute a FAKE angular velocity, not so easy
  545. Basis rot = new_transform.basis.orthonormalized() * get_transform().basis.orthonormalized().transposed();
  546. Vector3 axis;
  547. real_t angle;
  548. rot.get_axis_angle(axis, angle);
  549. axis.normalize();
  550. angular_velocity = constant_angular_velocity + axis * (angle / p_step);
  551. } else {
  552. if (!omit_force_integration) {
  553. //overridden by direct state query
  554. Vector3 force = gravity * mass + applied_force + constant_force;
  555. Vector3 torque = applied_torque + constant_torque;
  556. real_t damp = 1.0 - p_step * total_linear_damp;
  557. if (damp < 0) { // reached zero in the given time
  558. damp = 0;
  559. }
  560. real_t angular_damp_new = 1.0 - p_step * total_angular_damp;
  561. if (angular_damp_new < 0) { // reached zero in the given time
  562. angular_damp_new = 0;
  563. }
  564. linear_velocity *= damp;
  565. angular_velocity *= angular_damp_new;
  566. linear_velocity += _inv_mass * force * p_step;
  567. angular_velocity += _inv_inertia_tensor.xform(torque) * p_step;
  568. }
  569. if (continuous_cd) {
  570. motion = linear_velocity * p_step;
  571. do_motion = true;
  572. }
  573. }
  574. applied_force = Vector3();
  575. applied_torque = Vector3();
  576. biased_angular_velocity = Vector3();
  577. biased_linear_velocity = Vector3();
  578. if (do_motion) { //shapes temporarily extend for raycast
  579. _update_shapes_with_motion(motion);
  580. }
  581. contact_count = 0;
  582. }
  583. void GodotBody3D::integrate_velocities(real_t p_step) {
  584. if (mode == PhysicsServer3D::BODY_MODE_STATIC) {
  585. return;
  586. }
  587. ERR_FAIL_NULL(get_space());
  588. if (fi_callback_data || body_state_callback.is_valid()) {
  589. get_space()->body_add_to_state_query_list(&direct_state_query_list);
  590. }
  591. //apply axis lock linear
  592. for (int i = 0; i < 3; i++) {
  593. if (is_axis_locked((PhysicsServer3D::BodyAxis)(1 << i))) {
  594. linear_velocity[i] = 0;
  595. biased_linear_velocity[i] = 0;
  596. new_transform.origin[i] = get_transform().origin[i];
  597. }
  598. }
  599. //apply axis lock angular
  600. for (int i = 0; i < 3; i++) {
  601. if (is_axis_locked((PhysicsServer3D::BodyAxis)(1 << (i + 3)))) {
  602. angular_velocity[i] = 0;
  603. biased_angular_velocity[i] = 0;
  604. }
  605. }
  606. if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
  607. _set_transform(new_transform, false);
  608. _set_inv_transform(new_transform.affine_inverse());
  609. if (contacts.size() == 0 && linear_velocity == Vector3() && angular_velocity == Vector3()) {
  610. set_active(false); //stopped moving, deactivate
  611. }
  612. return;
  613. }
  614. Vector3 total_angular_velocity = angular_velocity + biased_angular_velocity;
  615. real_t ang_vel = total_angular_velocity.length();
  616. Transform3D transform_new = get_transform();
  617. if (!Math::is_zero_approx(ang_vel)) {
  618. Vector3 ang_vel_axis = total_angular_velocity / ang_vel;
  619. Basis rot(ang_vel_axis, ang_vel * p_step);
  620. Basis identity3(1, 0, 0, 0, 1, 0, 0, 0, 1);
  621. transform_new.origin += ((identity3 - rot) * transform_new.basis).xform(center_of_mass_local);
  622. transform_new.basis = rot * transform_new.basis;
  623. transform_new.orthonormalize();
  624. }
  625. Vector3 total_linear_velocity = linear_velocity + biased_linear_velocity;
  626. /*for(int i=0;i<3;i++) {
  627. if (axis_lock&(1<<i)) {
  628. transform_new.origin[i]=0.0;
  629. }
  630. }*/
  631. transform_new.origin += total_linear_velocity * p_step;
  632. _set_transform(transform_new);
  633. _set_inv_transform(get_transform().inverse());
  634. _update_transform_dependent();
  635. }
  636. void GodotBody3D::wakeup_neighbours() {
  637. for (const KeyValue<GodotConstraint3D *, int> &E : constraint_map) {
  638. const GodotConstraint3D *c = E.key;
  639. GodotBody3D **n = c->get_body_ptr();
  640. int bc = c->get_body_count();
  641. for (int i = 0; i < bc; i++) {
  642. if (i == E.value) {
  643. continue;
  644. }
  645. GodotBody3D *b = n[i];
  646. if (b->mode < PhysicsServer3D::BODY_MODE_RIGID) {
  647. continue;
  648. }
  649. if (!b->is_active()) {
  650. b->set_active(true);
  651. }
  652. }
  653. }
  654. }
  655. void GodotBody3D::call_queries() {
  656. Variant direct_state_variant = get_direct_state();
  657. if (fi_callback_data) {
  658. if (!fi_callback_data->callable.is_valid()) {
  659. set_force_integration_callback(Callable());
  660. } else {
  661. const Variant *vp[2] = { &direct_state_variant, &fi_callback_data->udata };
  662. Callable::CallError ce;
  663. int argc = (fi_callback_data->udata.get_type() == Variant::NIL) ? 1 : 2;
  664. Variant rv;
  665. fi_callback_data->callable.callp(vp, argc, rv, ce);
  666. }
  667. }
  668. if (body_state_callback.is_valid()) {
  669. body_state_callback.call(direct_state_variant);
  670. }
  671. }
  672. bool GodotBody3D::sleep_test(real_t p_step) {
  673. if (mode == PhysicsServer3D::BODY_MODE_STATIC || mode == PhysicsServer3D::BODY_MODE_KINEMATIC) {
  674. return true;
  675. } else if (!can_sleep) {
  676. return false;
  677. }
  678. ERR_FAIL_NULL_V(get_space(), true);
  679. 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()) {
  680. still_time += p_step;
  681. return still_time > get_space()->get_body_time_to_sleep();
  682. } else {
  683. still_time = 0; //maybe this should be set to 0 on set_active?
  684. return false;
  685. }
  686. }
  687. void GodotBody3D::set_state_sync_callback(const Callable &p_callable) {
  688. body_state_callback = p_callable;
  689. }
  690. void GodotBody3D::set_force_integration_callback(const Callable &p_callable, const Variant &p_udata) {
  691. if (p_callable.is_valid()) {
  692. if (!fi_callback_data) {
  693. fi_callback_data = memnew(ForceIntegrationCallbackData);
  694. }
  695. fi_callback_data->callable = p_callable;
  696. fi_callback_data->udata = p_udata;
  697. } else if (fi_callback_data) {
  698. memdelete(fi_callback_data);
  699. fi_callback_data = nullptr;
  700. }
  701. }
  702. GodotPhysicsDirectBodyState3D *GodotBody3D::get_direct_state() {
  703. if (!direct_state) {
  704. direct_state = memnew(GodotPhysicsDirectBodyState3D);
  705. direct_state->body = this;
  706. }
  707. return direct_state;
  708. }
  709. GodotBody3D::GodotBody3D() :
  710. GodotCollisionObject3D(TYPE_BODY),
  711. active_list(this),
  712. mass_properties_update_list(this),
  713. direct_state_query_list(this) {
  714. _set_static(false);
  715. }
  716. GodotBody3D::~GodotBody3D() {
  717. if (fi_callback_data) {
  718. memdelete(fi_callback_data);
  719. }
  720. if (direct_state) {
  721. memdelete(direct_state);
  722. }
  723. }