godot_body_3d.cpp 25 KB

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