jolt_body_3d.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. /**************************************************************************/
  2. /* jolt_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 "jolt_body_3d.h"
  31. #include "../joints/jolt_joint_3d.h"
  32. #include "../jolt_project_settings.h"
  33. #include "../misc/jolt_math_funcs.h"
  34. #include "../misc/jolt_type_conversions.h"
  35. #include "../shapes/jolt_shape_3d.h"
  36. #include "../spaces/jolt_broad_phase_layer.h"
  37. #include "../spaces/jolt_space_3d.h"
  38. #include "jolt_area_3d.h"
  39. #include "jolt_group_filter.h"
  40. #include "jolt_physics_direct_body_state_3d.h"
  41. #include "jolt_soft_body_3d.h"
  42. namespace {
  43. template <typename TValue, typename TGetter>
  44. bool integrate(TValue &p_value, PhysicsServer3D::AreaSpaceOverrideMode p_mode, TGetter &&p_getter) {
  45. switch (p_mode) {
  46. case PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED: {
  47. return false;
  48. }
  49. case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE: {
  50. p_value += p_getter();
  51. return false;
  52. }
  53. case PhysicsServer3D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
  54. p_value += p_getter();
  55. return true;
  56. }
  57. case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE: {
  58. p_value = p_getter();
  59. return true;
  60. }
  61. case PhysicsServer3D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
  62. p_value = p_getter();
  63. return false;
  64. }
  65. default: {
  66. ERR_FAIL_V_MSG(false, vformat("Unhandled override mode: '%d'. This should not happen. Please report this.", p_mode));
  67. }
  68. }
  69. }
  70. } // namespace
  71. JPH::BroadPhaseLayer JoltBody3D::_get_broad_phase_layer() const {
  72. switch (mode) {
  73. case PhysicsServer3D::BODY_MODE_STATIC: {
  74. return _is_big() ? JoltBroadPhaseLayer::BODY_STATIC_BIG : JoltBroadPhaseLayer::BODY_STATIC;
  75. }
  76. case PhysicsServer3D::BODY_MODE_KINEMATIC:
  77. case PhysicsServer3D::BODY_MODE_RIGID:
  78. case PhysicsServer3D::BODY_MODE_RIGID_LINEAR: {
  79. return JoltBroadPhaseLayer::BODY_DYNAMIC;
  80. }
  81. default: {
  82. ERR_FAIL_V_MSG(JoltBroadPhaseLayer::BODY_STATIC, vformat("Unhandled body mode: '%d'. This should not happen. Please report this.", mode));
  83. }
  84. }
  85. }
  86. JPH::ObjectLayer JoltBody3D::_get_object_layer() const {
  87. ERR_FAIL_NULL_V(space, 0);
  88. if (jolt_shape == nullptr || jolt_shape->GetType() == JPH::EShapeType::Empty) {
  89. // No point doing collision checks against a shapeless object.
  90. return space->map_to_object_layer(_get_broad_phase_layer(), 0, 0);
  91. }
  92. return space->map_to_object_layer(_get_broad_phase_layer(), collision_layer, collision_mask);
  93. }
  94. JPH::EMotionType JoltBody3D::_get_motion_type() const {
  95. switch (mode) {
  96. case PhysicsServer3D::BODY_MODE_STATIC: {
  97. return JPH::EMotionType::Static;
  98. }
  99. case PhysicsServer3D::BODY_MODE_KINEMATIC: {
  100. return JPH::EMotionType::Kinematic;
  101. }
  102. case PhysicsServer3D::BODY_MODE_RIGID:
  103. case PhysicsServer3D::BODY_MODE_RIGID_LINEAR: {
  104. return JPH::EMotionType::Dynamic;
  105. }
  106. default: {
  107. ERR_FAIL_V_MSG(JPH::EMotionType::Static, vformat("Unhandled body mode: '%d'. This should not happen. Please report this.", mode));
  108. }
  109. }
  110. }
  111. void JoltBody3D::_add_to_space() {
  112. jolt_shape = build_shapes(true);
  113. JPH::CollisionGroup::GroupID group_id = 0;
  114. JPH::CollisionGroup::SubGroupID sub_group_id = 0;
  115. JoltGroupFilter::encode_object(this, group_id, sub_group_id);
  116. jolt_settings->mUserData = reinterpret_cast<JPH::uint64>(this);
  117. jolt_settings->mObjectLayer = _get_object_layer();
  118. jolt_settings->mCollisionGroup = JPH::CollisionGroup(nullptr, group_id, sub_group_id);
  119. jolt_settings->mMotionType = _get_motion_type();
  120. jolt_settings->mAllowedDOFs = _calculate_allowed_dofs();
  121. jolt_settings->mAllowDynamicOrKinematic = true;
  122. jolt_settings->mCollideKinematicVsNonDynamic = reports_all_kinematic_contacts();
  123. jolt_settings->mUseManifoldReduction = !reports_contacts();
  124. jolt_settings->mAllowSleeping = is_sleep_actually_allowed();
  125. jolt_settings->mLinearDamping = 0.0f;
  126. jolt_settings->mAngularDamping = 0.0f;
  127. jolt_settings->mMaxLinearVelocity = JoltProjectSettings::max_linear_velocity;
  128. jolt_settings->mMaxAngularVelocity = JoltProjectSettings::max_angular_velocity;
  129. if (JoltProjectSettings::use_enhanced_internal_edge_removal_for_bodies) {
  130. jolt_settings->mEnhancedInternalEdgeRemoval = true;
  131. }
  132. jolt_settings->mOverrideMassProperties = JPH::EOverrideMassProperties::MassAndInertiaProvided;
  133. jolt_settings->mMassPropertiesOverride = _calculate_mass_properties();
  134. jolt_settings->SetShape(jolt_shape);
  135. JPH::Body *new_jolt_body = space->add_object(*this, *jolt_settings, sleep_initially);
  136. if (new_jolt_body == nullptr) {
  137. return;
  138. }
  139. jolt_body = new_jolt_body;
  140. delete jolt_settings;
  141. jolt_settings = nullptr;
  142. }
  143. void JoltBody3D::_enqueue_call_queries() {
  144. // This method will be called from the body activation listener on multiple threads during the simulation step.
  145. if (space != nullptr) {
  146. space->enqueue_call_queries(&call_queries_element);
  147. }
  148. }
  149. void JoltBody3D::_dequeue_call_queries() {
  150. if (space != nullptr) {
  151. space->dequeue_call_queries(&call_queries_element);
  152. }
  153. }
  154. void JoltBody3D::_integrate_forces(float p_step, JPH::Body &p_jolt_body) {
  155. _update_gravity(p_jolt_body);
  156. if (!custom_integrator) {
  157. JPH::MotionProperties &motion_properties = *p_jolt_body.GetMotionPropertiesUnchecked();
  158. JPH::Vec3 linear_velocity = motion_properties.GetLinearVelocity();
  159. JPH::Vec3 angular_velocity = motion_properties.GetAngularVelocity();
  160. // Jolt applies damping differently from Godot Physics, where Godot Physics applies damping before integrating
  161. // forces whereas Jolt does it after integrating forces. The way Godot Physics does it seems to yield more
  162. // consistent results across different update frequencies when using high (>1) damping values, so we apply the
  163. // damping ourselves instead, before any force integration happens.
  164. linear_velocity *= MAX(1.0f - total_linear_damp * p_step, 0.0f);
  165. angular_velocity *= MAX(1.0f - total_angular_damp * p_step, 0.0f);
  166. linear_velocity += to_jolt(gravity) * p_step;
  167. motion_properties.SetLinearVelocityClamped(linear_velocity);
  168. motion_properties.SetAngularVelocityClamped(angular_velocity);
  169. p_jolt_body.AddForce(to_jolt(constant_force));
  170. p_jolt_body.AddTorque(to_jolt(constant_torque));
  171. }
  172. }
  173. void JoltBody3D::_move_kinematic(float p_step, JPH::Body &p_jolt_body) {
  174. p_jolt_body.SetLinearVelocity(JPH::Vec3::sZero());
  175. p_jolt_body.SetAngularVelocity(JPH::Vec3::sZero());
  176. const JPH::RVec3 current_position = p_jolt_body.GetPosition();
  177. const JPH::Quat current_rotation = p_jolt_body.GetRotation();
  178. const JPH::RVec3 new_position = to_jolt_r(kinematic_transform.origin);
  179. const JPH::Quat new_rotation = to_jolt(kinematic_transform.basis);
  180. if (new_position == current_position && new_rotation == current_rotation) {
  181. return;
  182. }
  183. p_jolt_body.MoveKinematic(new_position, new_rotation, p_step);
  184. }
  185. JPH::EAllowedDOFs JoltBody3D::_calculate_allowed_dofs() const {
  186. if (is_static()) {
  187. return JPH::EAllowedDOFs::All;
  188. }
  189. JPH::EAllowedDOFs allowed_dofs = JPH::EAllowedDOFs::All;
  190. if (is_axis_locked(PhysicsServer3D::BODY_AXIS_LINEAR_X)) {
  191. allowed_dofs &= ~JPH::EAllowedDOFs::TranslationX;
  192. }
  193. if (is_axis_locked(PhysicsServer3D::BODY_AXIS_LINEAR_Y)) {
  194. allowed_dofs &= ~JPH::EAllowedDOFs::TranslationY;
  195. }
  196. if (is_axis_locked(PhysicsServer3D::BODY_AXIS_LINEAR_Z)) {
  197. allowed_dofs &= ~JPH::EAllowedDOFs::TranslationZ;
  198. }
  199. if (is_axis_locked(PhysicsServer3D::BODY_AXIS_ANGULAR_X) || is_rigid_linear()) {
  200. allowed_dofs &= ~JPH::EAllowedDOFs::RotationX;
  201. }
  202. if (is_axis_locked(PhysicsServer3D::BODY_AXIS_ANGULAR_Y) || is_rigid_linear()) {
  203. allowed_dofs &= ~JPH::EAllowedDOFs::RotationY;
  204. }
  205. if (is_axis_locked(PhysicsServer3D::BODY_AXIS_ANGULAR_Z) || is_rigid_linear()) {
  206. allowed_dofs &= ~JPH::EAllowedDOFs::RotationZ;
  207. }
  208. ERR_FAIL_COND_V_MSG(allowed_dofs == JPH::EAllowedDOFs::None, JPH::EAllowedDOFs::All, vformat("Invalid axis locks for '%s'. Locking all axes is not supported when using Jolt Physics. All axes will be unlocked. Considering freezing the body instead.", to_string()));
  209. return allowed_dofs;
  210. }
  211. JPH::MassProperties JoltBody3D::_calculate_mass_properties(const JPH::Shape &p_shape) const {
  212. const bool calculate_mass = mass <= 0;
  213. const bool calculate_inertia = inertia.x <= 0 || inertia.y <= 0 || inertia.z <= 0;
  214. JPH::MassProperties mass_properties = p_shape.GetMassProperties();
  215. if (calculate_mass && calculate_inertia) {
  216. // Use the mass properties calculated by the shape
  217. } else if (calculate_inertia) {
  218. mass_properties.ScaleToMass(mass);
  219. } else {
  220. mass_properties.mMass = mass;
  221. }
  222. if (inertia.x > 0) {
  223. mass_properties.mInertia(0, 0) = (float)inertia.x;
  224. mass_properties.mInertia(0, 1) = 0;
  225. mass_properties.mInertia(0, 2) = 0;
  226. mass_properties.mInertia(1, 0) = 0;
  227. mass_properties.mInertia(2, 0) = 0;
  228. }
  229. if (inertia.y > 0) {
  230. mass_properties.mInertia(1, 1) = (float)inertia.y;
  231. mass_properties.mInertia(1, 0) = 0;
  232. mass_properties.mInertia(1, 2) = 0;
  233. mass_properties.mInertia(0, 1) = 0;
  234. mass_properties.mInertia(2, 1) = 0;
  235. }
  236. if (inertia.z > 0) {
  237. mass_properties.mInertia(2, 2) = (float)inertia.z;
  238. mass_properties.mInertia(2, 0) = 0;
  239. mass_properties.mInertia(2, 1) = 0;
  240. mass_properties.mInertia(0, 2) = 0;
  241. mass_properties.mInertia(1, 2) = 0;
  242. }
  243. mass_properties.mInertia(3, 3) = 1.0f;
  244. return mass_properties;
  245. }
  246. JPH::MassProperties JoltBody3D::_calculate_mass_properties() const {
  247. return _calculate_mass_properties(*jolt_shape);
  248. }
  249. void JoltBody3D::_on_wake_up() {
  250. // This method will be called from the body activation listener on multiple threads during the simulation step.
  251. if (_should_call_queries()) {
  252. _enqueue_call_queries();
  253. }
  254. }
  255. void JoltBody3D::_update_mass_properties() {
  256. if (in_space()) {
  257. jolt_body->GetMotionPropertiesUnchecked()->SetMassProperties(_calculate_allowed_dofs(), _calculate_mass_properties());
  258. }
  259. }
  260. void JoltBody3D::_update_gravity(JPH::Body &p_jolt_body) {
  261. gravity = Vector3();
  262. const Vector3 position = to_godot(p_jolt_body.GetPosition());
  263. bool gravity_done = false;
  264. for (const JoltArea3D *area : areas) {
  265. gravity_done = integrate(gravity, area->get_gravity_mode(), [&]() {
  266. return area->compute_gravity(position);
  267. });
  268. if (gravity_done) {
  269. break;
  270. }
  271. }
  272. if (!gravity_done) {
  273. gravity += space->get_default_area()->compute_gravity(position);
  274. }
  275. gravity *= gravity_scale;
  276. }
  277. void JoltBody3D::_update_damp() {
  278. if (!in_space()) {
  279. return;
  280. }
  281. total_linear_damp = 0.0;
  282. total_angular_damp = 0.0;
  283. bool linear_damp_done = linear_damp_mode == PhysicsServer3D::BODY_DAMP_MODE_REPLACE;
  284. bool angular_damp_done = angular_damp_mode == PhysicsServer3D::BODY_DAMP_MODE_REPLACE;
  285. for (const JoltArea3D *area : areas) {
  286. if (!linear_damp_done) {
  287. linear_damp_done = integrate(total_linear_damp, area->get_linear_damp_mode(), [&]() {
  288. return area->get_linear_damp();
  289. });
  290. }
  291. if (!angular_damp_done) {
  292. angular_damp_done = integrate(total_angular_damp, area->get_angular_damp_mode(), [&]() {
  293. return area->get_angular_damp();
  294. });
  295. }
  296. if (linear_damp_done && angular_damp_done) {
  297. break;
  298. }
  299. }
  300. const JoltArea3D *default_area = space->get_default_area();
  301. if (!linear_damp_done) {
  302. total_linear_damp += default_area->get_linear_damp();
  303. }
  304. if (!angular_damp_done) {
  305. total_angular_damp += default_area->get_angular_damp();
  306. }
  307. switch (linear_damp_mode) {
  308. case PhysicsServer3D::BODY_DAMP_MODE_COMBINE: {
  309. total_linear_damp += linear_damp;
  310. } break;
  311. case PhysicsServer3D::BODY_DAMP_MODE_REPLACE: {
  312. total_linear_damp = linear_damp;
  313. } break;
  314. }
  315. switch (angular_damp_mode) {
  316. case PhysicsServer3D::BODY_DAMP_MODE_COMBINE: {
  317. total_angular_damp += angular_damp;
  318. } break;
  319. case PhysicsServer3D::BODY_DAMP_MODE_REPLACE: {
  320. total_angular_damp = angular_damp;
  321. } break;
  322. }
  323. _motion_changed();
  324. }
  325. void JoltBody3D::_update_kinematic_transform() {
  326. if (is_kinematic()) {
  327. kinematic_transform = get_transform_unscaled();
  328. }
  329. }
  330. void JoltBody3D::_update_joint_constraints() {
  331. for (JoltJoint3D *joint : joints) {
  332. joint->rebuild();
  333. }
  334. }
  335. void JoltBody3D::_update_possible_kinematic_contacts() {
  336. const bool value = reports_all_kinematic_contacts();
  337. if (!in_space()) {
  338. jolt_settings->mCollideKinematicVsNonDynamic = value;
  339. } else {
  340. jolt_body->SetCollideKinematicVsNonDynamic(value);
  341. }
  342. }
  343. void JoltBody3D::_update_sleep_allowed() {
  344. const bool value = is_sleep_actually_allowed();
  345. if (!in_space()) {
  346. jolt_settings->mAllowSleeping = value;
  347. } else {
  348. jolt_body->SetAllowSleeping(value);
  349. }
  350. }
  351. void JoltBody3D::_destroy_joint_constraints() {
  352. for (JoltJoint3D *joint : joints) {
  353. joint->destroy();
  354. }
  355. }
  356. void JoltBody3D::_exit_all_areas() {
  357. if (!in_space()) {
  358. return;
  359. }
  360. for (JoltArea3D *area : areas) {
  361. area->body_exited(jolt_body->GetID(), false);
  362. }
  363. areas.clear();
  364. }
  365. void JoltBody3D::_update_group_filter() {
  366. JPH::GroupFilter *group_filter = !exceptions.is_empty() ? JoltGroupFilter::instance : nullptr;
  367. if (!in_space()) {
  368. jolt_settings->mCollisionGroup.SetGroupFilter(group_filter);
  369. } else {
  370. jolt_body->GetCollisionGroup().SetGroupFilter(group_filter);
  371. }
  372. }
  373. void JoltBody3D::_mode_changed() {
  374. _update_object_layer();
  375. _update_kinematic_transform();
  376. _update_mass_properties();
  377. _update_sleep_allowed();
  378. wake_up();
  379. }
  380. void JoltBody3D::_shapes_committed() {
  381. JoltShapedObject3D::_shapes_committed();
  382. _update_mass_properties();
  383. _update_joint_constraints();
  384. wake_up();
  385. }
  386. void JoltBody3D::_space_changing() {
  387. JoltShapedObject3D::_space_changing();
  388. sleep_initially = is_sleeping();
  389. _destroy_joint_constraints();
  390. _exit_all_areas();
  391. _dequeue_call_queries();
  392. }
  393. void JoltBody3D::_space_changed() {
  394. JoltShapedObject3D::_space_changed();
  395. _update_kinematic_transform();
  396. _update_group_filter();
  397. _update_joint_constraints();
  398. _update_sleep_allowed();
  399. _areas_changed();
  400. }
  401. void JoltBody3D::_areas_changed() {
  402. _update_damp();
  403. wake_up();
  404. }
  405. void JoltBody3D::_joints_changed() {
  406. wake_up();
  407. }
  408. void JoltBody3D::_transform_changed() {
  409. wake_up();
  410. }
  411. void JoltBody3D::_motion_changed() {
  412. wake_up();
  413. }
  414. void JoltBody3D::_exceptions_changed() {
  415. _update_group_filter();
  416. }
  417. void JoltBody3D::_axis_lock_changed() {
  418. _update_mass_properties();
  419. wake_up();
  420. }
  421. void JoltBody3D::_contact_reporting_changed() {
  422. _update_possible_kinematic_contacts();
  423. _update_sleep_allowed();
  424. wake_up();
  425. }
  426. void JoltBody3D::_sleep_allowed_changed() {
  427. _update_sleep_allowed();
  428. wake_up();
  429. }
  430. JoltBody3D::JoltBody3D() :
  431. JoltShapedObject3D(OBJECT_TYPE_BODY),
  432. call_queries_element(this) {
  433. }
  434. JoltBody3D::~JoltBody3D() {
  435. if (direct_state != nullptr) {
  436. memdelete(direct_state);
  437. direct_state = nullptr;
  438. }
  439. }
  440. void JoltBody3D::set_transform(Transform3D p_transform) {
  441. JOLT_ENSURE_SCALE_NOT_ZERO(p_transform, vformat("An invalid transform was passed to physics body '%s'.", to_string()));
  442. Vector3 new_scale;
  443. JoltMath::decompose(p_transform, new_scale);
  444. // Ideally we would do an exact comparison here, but due to floating-point precision this would be invalidated very often.
  445. if (!scale.is_equal_approx(new_scale)) {
  446. scale = new_scale;
  447. _shapes_changed();
  448. }
  449. if (!in_space()) {
  450. jolt_settings->mPosition = to_jolt_r(p_transform.origin);
  451. jolt_settings->mRotation = to_jolt(p_transform.basis);
  452. } else if (is_kinematic()) {
  453. kinematic_transform = p_transform;
  454. } else {
  455. space->get_body_iface().SetPositionAndRotation(jolt_body->GetID(), to_jolt_r(p_transform.origin), to_jolt(p_transform.basis), JPH::EActivation::DontActivate);
  456. }
  457. _transform_changed();
  458. }
  459. Variant JoltBody3D::get_state(PhysicsServer3D::BodyState p_state) const {
  460. switch (p_state) {
  461. case PhysicsServer3D::BODY_STATE_TRANSFORM: {
  462. return get_transform_scaled();
  463. }
  464. case PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY: {
  465. return get_linear_velocity();
  466. }
  467. case PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY: {
  468. return get_angular_velocity();
  469. }
  470. case PhysicsServer3D::BODY_STATE_SLEEPING: {
  471. return is_sleeping();
  472. }
  473. case PhysicsServer3D::BODY_STATE_CAN_SLEEP: {
  474. return is_sleep_allowed();
  475. }
  476. default: {
  477. ERR_FAIL_V_MSG(Variant(), vformat("Unhandled body state: '%d'. This should not happen. Please report this.", p_state));
  478. }
  479. }
  480. }
  481. void JoltBody3D::set_state(PhysicsServer3D::BodyState p_state, const Variant &p_value) {
  482. switch (p_state) {
  483. case PhysicsServer3D::BODY_STATE_TRANSFORM: {
  484. set_transform(p_value);
  485. } break;
  486. case PhysicsServer3D::BODY_STATE_LINEAR_VELOCITY: {
  487. set_linear_velocity(p_value);
  488. } break;
  489. case PhysicsServer3D::BODY_STATE_ANGULAR_VELOCITY: {
  490. set_angular_velocity(p_value);
  491. } break;
  492. case PhysicsServer3D::BODY_STATE_SLEEPING: {
  493. set_is_sleeping(p_value);
  494. } break;
  495. case PhysicsServer3D::BODY_STATE_CAN_SLEEP: {
  496. set_is_sleep_allowed(p_value);
  497. } break;
  498. default: {
  499. ERR_FAIL_MSG(vformat("Unhandled body state: '%d'. This should not happen. Please report this.", p_state));
  500. } break;
  501. }
  502. }
  503. Variant JoltBody3D::get_param(PhysicsServer3D::BodyParameter p_param) const {
  504. switch (p_param) {
  505. case PhysicsServer3D::BODY_PARAM_BOUNCE: {
  506. return get_bounce();
  507. }
  508. case PhysicsServer3D::BODY_PARAM_FRICTION: {
  509. return get_friction();
  510. }
  511. case PhysicsServer3D::BODY_PARAM_MASS: {
  512. return get_mass();
  513. }
  514. case PhysicsServer3D::BODY_PARAM_INERTIA: {
  515. return get_inertia();
  516. }
  517. case PhysicsServer3D::BODY_PARAM_CENTER_OF_MASS: {
  518. return get_center_of_mass_custom();
  519. }
  520. case PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE: {
  521. return get_gravity_scale();
  522. }
  523. case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP_MODE: {
  524. return get_linear_damp_mode();
  525. }
  526. case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP_MODE: {
  527. return get_angular_damp_mode();
  528. }
  529. case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP: {
  530. return get_linear_damp();
  531. }
  532. case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP: {
  533. return get_angular_damp();
  534. }
  535. default: {
  536. ERR_FAIL_V_MSG(Variant(), vformat("Unhandled body parameter: '%d'. This should not happen. Please report this.", p_param));
  537. }
  538. }
  539. }
  540. void JoltBody3D::set_param(PhysicsServer3D::BodyParameter p_param, const Variant &p_value) {
  541. switch (p_param) {
  542. case PhysicsServer3D::BODY_PARAM_BOUNCE: {
  543. set_bounce(p_value);
  544. } break;
  545. case PhysicsServer3D::BODY_PARAM_FRICTION: {
  546. set_friction(p_value);
  547. } break;
  548. case PhysicsServer3D::BODY_PARAM_MASS: {
  549. set_mass(p_value);
  550. } break;
  551. case PhysicsServer3D::BODY_PARAM_INERTIA: {
  552. set_inertia(p_value);
  553. } break;
  554. case PhysicsServer3D::BODY_PARAM_CENTER_OF_MASS: {
  555. set_center_of_mass_custom(p_value);
  556. } break;
  557. case PhysicsServer3D::BODY_PARAM_GRAVITY_SCALE: {
  558. set_gravity_scale(p_value);
  559. } break;
  560. case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP_MODE: {
  561. set_linear_damp_mode((DampMode)(int)p_value);
  562. } break;
  563. case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP_MODE: {
  564. set_angular_damp_mode((DampMode)(int)p_value);
  565. } break;
  566. case PhysicsServer3D::BODY_PARAM_LINEAR_DAMP: {
  567. set_linear_damp(p_value);
  568. } break;
  569. case PhysicsServer3D::BODY_PARAM_ANGULAR_DAMP: {
  570. set_angular_damp(p_value);
  571. } break;
  572. default: {
  573. ERR_FAIL_MSG(vformat("Unhandled body parameter: '%d'. This should not happen. Please report this.", p_param));
  574. } break;
  575. }
  576. }
  577. void JoltBody3D::set_custom_integrator(bool p_enabled) {
  578. if (custom_integrator == p_enabled) {
  579. return;
  580. }
  581. custom_integrator = p_enabled;
  582. if (in_space()) {
  583. jolt_body->ResetForce();
  584. jolt_body->ResetTorque();
  585. }
  586. _motion_changed();
  587. }
  588. bool JoltBody3D::is_sleeping() const {
  589. if (!in_space()) {
  590. return sleep_initially;
  591. } else {
  592. return !jolt_body->IsActive();
  593. }
  594. }
  595. bool JoltBody3D::is_sleep_actually_allowed() const {
  596. return sleep_allowed && !(is_kinematic() && reports_contacts());
  597. }
  598. void JoltBody3D::set_is_sleeping(bool p_enabled) {
  599. if (!in_space()) {
  600. sleep_initially = p_enabled;
  601. } else {
  602. space->set_is_object_sleeping(jolt_body->GetID(), p_enabled);
  603. }
  604. }
  605. void JoltBody3D::set_is_sleep_allowed(bool p_enabled) {
  606. if (sleep_allowed == p_enabled) {
  607. return;
  608. }
  609. sleep_allowed = p_enabled;
  610. _sleep_allowed_changed();
  611. }
  612. Basis JoltBody3D::get_principal_inertia_axes() const {
  613. ERR_FAIL_COND_V_MSG(!in_space(), Basis(), vformat("Failed to retrieve principal inertia axes of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
  614. if (unlikely(is_static() || is_kinematic())) {
  615. return Basis();
  616. }
  617. return to_godot(jolt_body->GetRotation() * jolt_body->GetMotionPropertiesUnchecked()->GetInertiaRotation());
  618. }
  619. Vector3 JoltBody3D::get_inverse_inertia() const {
  620. ERR_FAIL_COND_V_MSG(!in_space(), Vector3(), vformat("Failed to retrieve inverse inertia of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
  621. if (unlikely(is_static() || is_kinematic())) {
  622. return Vector3();
  623. }
  624. return to_godot(jolt_body->GetMotionPropertiesUnchecked()->GetLocalSpaceInverseInertia().GetDiagonal3());
  625. }
  626. Basis JoltBody3D::get_inverse_inertia_tensor() const {
  627. ERR_FAIL_COND_V_MSG(!in_space(), Basis(), vformat("Failed to retrieve inverse inertia tensor of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
  628. if (unlikely(is_static() || is_kinematic())) {
  629. return Basis();
  630. }
  631. return to_godot(jolt_body->GetInverseInertia()).basis;
  632. }
  633. void JoltBody3D::set_linear_velocity(const Vector3 &p_velocity) {
  634. if (is_static() || is_kinematic()) {
  635. linear_surface_velocity = p_velocity;
  636. } else {
  637. if (!in_space()) {
  638. jolt_settings->mLinearVelocity = to_jolt(p_velocity);
  639. } else {
  640. jolt_body->GetMotionPropertiesUnchecked()->SetLinearVelocityClamped(to_jolt(p_velocity));
  641. }
  642. }
  643. _motion_changed();
  644. }
  645. void JoltBody3D::set_angular_velocity(const Vector3 &p_velocity) {
  646. if (is_static() || is_kinematic()) {
  647. angular_surface_velocity = p_velocity;
  648. } else {
  649. if (!in_space()) {
  650. jolt_settings->mAngularVelocity = to_jolt(p_velocity);
  651. } else {
  652. jolt_body->GetMotionPropertiesUnchecked()->SetAngularVelocityClamped(to_jolt(p_velocity));
  653. }
  654. }
  655. _motion_changed();
  656. }
  657. void JoltBody3D::set_axis_velocity(const Vector3 &p_axis_velocity) {
  658. const Vector3 axis = p_axis_velocity.normalized();
  659. if (!in_space()) {
  660. Vector3 linear_velocity = to_godot(jolt_settings->mLinearVelocity);
  661. linear_velocity -= axis * axis.dot(linear_velocity);
  662. linear_velocity += p_axis_velocity;
  663. jolt_settings->mLinearVelocity = to_jolt(linear_velocity);
  664. } else {
  665. Vector3 linear_velocity = get_linear_velocity();
  666. linear_velocity -= axis * axis.dot(linear_velocity);
  667. linear_velocity += p_axis_velocity;
  668. set_linear_velocity(linear_velocity);
  669. }
  670. _motion_changed();
  671. }
  672. Vector3 JoltBody3D::get_velocity_at_position(const Vector3 &p_position) const {
  673. if (unlikely(!in_space())) {
  674. return Vector3();
  675. }
  676. const JPH::MotionProperties &motion_properties = *jolt_body->GetMotionPropertiesUnchecked();
  677. const Vector3 total_linear_velocity = to_godot(motion_properties.GetLinearVelocity()) + linear_surface_velocity;
  678. const Vector3 total_angular_velocity = to_godot(motion_properties.GetAngularVelocity()) + angular_surface_velocity;
  679. const Vector3 com_to_pos = p_position - to_godot(jolt_body->GetCenterOfMassPosition());
  680. return total_linear_velocity + total_angular_velocity.cross(com_to_pos);
  681. }
  682. void JoltBody3D::set_center_of_mass_custom(const Vector3 &p_center_of_mass) {
  683. if (custom_center_of_mass && p_center_of_mass == center_of_mass_custom) {
  684. return;
  685. }
  686. custom_center_of_mass = true;
  687. center_of_mass_custom = p_center_of_mass;
  688. _shapes_changed();
  689. }
  690. void JoltBody3D::set_max_contacts_reported(int p_count) {
  691. ERR_FAIL_INDEX(p_count, MAX_CONTACTS_REPORTED_3D_MAX);
  692. if (unlikely((int)contacts.size() == p_count)) {
  693. return;
  694. }
  695. contacts.resize(p_count);
  696. contact_count = MIN(contact_count, p_count);
  697. const bool use_manifold_reduction = !reports_contacts();
  698. if (!in_space()) {
  699. jolt_settings->mUseManifoldReduction = use_manifold_reduction;
  700. } else {
  701. space->get_body_iface().SetUseManifoldReduction(jolt_body->GetID(), use_manifold_reduction);
  702. }
  703. _contact_reporting_changed();
  704. }
  705. bool JoltBody3D::reports_all_kinematic_contacts() const {
  706. return reports_contacts() && JoltProjectSettings::generate_all_kinematic_contacts;
  707. }
  708. void JoltBody3D::add_contact(const JoltBody3D *p_collider, float p_depth, int p_shape_index, int p_collider_shape_index, const Vector3 &p_normal, const Vector3 &p_position, const Vector3 &p_collider_position, const Vector3 &p_velocity, const Vector3 &p_collider_velocity, const Vector3 &p_impulse) {
  709. const int max_contacts = get_max_contacts_reported();
  710. if (max_contacts == 0) {
  711. return;
  712. }
  713. Contact *contact = nullptr;
  714. if (contact_count < max_contacts) {
  715. contact = &contacts[contact_count++];
  716. } else {
  717. Contact *shallowest_contact = &contacts[0];
  718. for (int i = 1; i < (int)contacts.size(); i++) {
  719. Contact &other_contact = contacts[i];
  720. if (other_contact.depth < shallowest_contact->depth) {
  721. shallowest_contact = &other_contact;
  722. }
  723. }
  724. if (shallowest_contact->depth < p_depth) {
  725. contact = shallowest_contact;
  726. }
  727. }
  728. if (contact != nullptr) {
  729. contact->normal = p_normal;
  730. contact->position = p_position;
  731. contact->collider_position = p_collider_position;
  732. contact->velocity = p_velocity;
  733. contact->collider_velocity = p_collider_velocity;
  734. contact->impulse = p_impulse;
  735. contact->collider_id = p_collider->get_instance_id();
  736. contact->collider_rid = p_collider->get_rid();
  737. contact->shape_index = p_shape_index;
  738. contact->collider_shape_index = p_collider_shape_index;
  739. }
  740. }
  741. void JoltBody3D::reset_mass_properties() {
  742. if (custom_center_of_mass) {
  743. custom_center_of_mass = false;
  744. center_of_mass_custom.zero();
  745. _shapes_changed();
  746. }
  747. inertia.zero();
  748. _update_mass_properties();
  749. }
  750. void JoltBody3D::apply_force(const Vector3 &p_force, const Vector3 &p_position) {
  751. ERR_FAIL_COND_MSG(!in_space(), vformat("Failed to apply force to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
  752. if (unlikely(!is_rigid()) || custom_integrator || p_force == Vector3()) {
  753. return;
  754. }
  755. jolt_body->AddForce(to_jolt(p_force), jolt_body->GetPosition() + to_jolt(p_position));
  756. _motion_changed();
  757. }
  758. void JoltBody3D::apply_central_force(const Vector3 &p_force) {
  759. ERR_FAIL_COND_MSG(!in_space(), vformat("Failed to apply central force to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
  760. if (unlikely(!is_rigid()) || custom_integrator || p_force == Vector3()) {
  761. return;
  762. }
  763. jolt_body->AddForce(to_jolt(p_force));
  764. _motion_changed();
  765. }
  766. void JoltBody3D::apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position) {
  767. ERR_FAIL_COND_MSG(!in_space(), vformat("Failed to apply impulse to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
  768. if (unlikely(!is_rigid()) || p_impulse == Vector3()) {
  769. return;
  770. }
  771. jolt_body->AddImpulse(to_jolt(p_impulse), jolt_body->GetPosition() + to_jolt(p_position));
  772. _motion_changed();
  773. }
  774. void JoltBody3D::apply_central_impulse(const Vector3 &p_impulse) {
  775. ERR_FAIL_COND_MSG(!in_space(), vformat("Failed to apply central impulse to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
  776. if (unlikely(!is_rigid()) || p_impulse == Vector3()) {
  777. return;
  778. }
  779. jolt_body->AddImpulse(to_jolt(p_impulse));
  780. _motion_changed();
  781. }
  782. void JoltBody3D::apply_torque(const Vector3 &p_torque) {
  783. ERR_FAIL_COND_MSG(!in_space(), vformat("Failed to apply torque to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
  784. if (unlikely(!is_rigid()) || custom_integrator || p_torque == Vector3()) {
  785. return;
  786. }
  787. jolt_body->AddTorque(to_jolt(p_torque));
  788. _motion_changed();
  789. }
  790. void JoltBody3D::apply_torque_impulse(const Vector3 &p_impulse) {
  791. ERR_FAIL_COND_MSG(!in_space(), vformat("Failed to apply torque impulse to '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
  792. if (unlikely(!is_rigid()) || p_impulse == Vector3()) {
  793. return;
  794. }
  795. jolt_body->AddAngularImpulse(to_jolt(p_impulse));
  796. _motion_changed();
  797. }
  798. void JoltBody3D::add_constant_central_force(const Vector3 &p_force) {
  799. if (p_force == Vector3()) {
  800. return;
  801. }
  802. constant_force += p_force;
  803. _motion_changed();
  804. }
  805. void JoltBody3D::add_constant_force(const Vector3 &p_force, const Vector3 &p_position) {
  806. if (p_force == Vector3()) {
  807. return;
  808. }
  809. constant_force += p_force;
  810. constant_torque += (p_position - get_center_of_mass_relative()).cross(p_force);
  811. _motion_changed();
  812. }
  813. void JoltBody3D::add_constant_torque(const Vector3 &p_torque) {
  814. if (p_torque == Vector3()) {
  815. return;
  816. }
  817. constant_torque += p_torque;
  818. _motion_changed();
  819. }
  820. Vector3 JoltBody3D::get_constant_force() const {
  821. return constant_force;
  822. }
  823. void JoltBody3D::set_constant_force(const Vector3 &p_force) {
  824. if (constant_force == p_force) {
  825. return;
  826. }
  827. constant_force = p_force;
  828. _motion_changed();
  829. }
  830. Vector3 JoltBody3D::get_constant_torque() const {
  831. return constant_torque;
  832. }
  833. void JoltBody3D::set_constant_torque(const Vector3 &p_torque) {
  834. if (constant_torque == p_torque) {
  835. return;
  836. }
  837. constant_torque = p_torque;
  838. _motion_changed();
  839. }
  840. void JoltBody3D::add_collision_exception(const RID &p_excepted_body) {
  841. exceptions.push_back(p_excepted_body);
  842. _exceptions_changed();
  843. }
  844. void JoltBody3D::remove_collision_exception(const RID &p_excepted_body) {
  845. exceptions.erase(p_excepted_body);
  846. _exceptions_changed();
  847. }
  848. bool JoltBody3D::has_collision_exception(const RID &p_excepted_body) const {
  849. return exceptions.find(p_excepted_body) >= 0;
  850. }
  851. void JoltBody3D::add_area(JoltArea3D *p_area) {
  852. int i = 0;
  853. for (; i < (int)areas.size(); i++) {
  854. if (p_area->get_priority() > areas[i]->get_priority()) {
  855. break;
  856. }
  857. }
  858. areas.insert(i, p_area);
  859. _areas_changed();
  860. }
  861. void JoltBody3D::remove_area(JoltArea3D *p_area) {
  862. areas.erase(p_area);
  863. _areas_changed();
  864. }
  865. void JoltBody3D::add_joint(JoltJoint3D *p_joint) {
  866. joints.push_back(p_joint);
  867. _joints_changed();
  868. }
  869. void JoltBody3D::remove_joint(JoltJoint3D *p_joint) {
  870. joints.erase(p_joint);
  871. _joints_changed();
  872. }
  873. void JoltBody3D::call_queries() {
  874. if (custom_integration_callback.is_valid()) {
  875. const Variant direct_state_variant = get_direct_state();
  876. const Variant *args[2] = { &direct_state_variant, &custom_integration_userdata };
  877. const int argc = custom_integration_userdata.get_type() != Variant::NIL ? 2 : 1;
  878. Callable::CallError ce;
  879. Variant ret;
  880. custom_integration_callback.callp(args, argc, ret, ce);
  881. if (unlikely(ce.error != Callable::CallError::CALL_OK)) {
  882. ERR_PRINT_ONCE(vformat("Failed to call force integration callback for '%s'. It returned the following error: '%s'.", to_string(), Variant::get_callable_error_text(custom_integration_callback, args, argc, ce)));
  883. }
  884. }
  885. if (state_sync_callback.is_valid()) {
  886. const Variant direct_state_variant = get_direct_state();
  887. const Variant *args[1] = { &direct_state_variant };
  888. Callable::CallError ce;
  889. Variant ret;
  890. state_sync_callback.callp(args, 1, ret, ce);
  891. if (unlikely(ce.error != Callable::CallError::CALL_OK)) {
  892. ERR_PRINT_ONCE(vformat("Failed to call state synchronization callback for '%s'. It returned the following error: '%s'.", to_string(), Variant::get_callable_error_text(state_sync_callback, args, 1, ce)));
  893. }
  894. }
  895. }
  896. void JoltBody3D::pre_step(float p_step, JPH::Body &p_jolt_body) {
  897. JoltObject3D::pre_step(p_step, p_jolt_body);
  898. switch (mode) {
  899. case PhysicsServer3D::BODY_MODE_STATIC: {
  900. // Will never happen.
  901. } break;
  902. case PhysicsServer3D::BODY_MODE_RIGID:
  903. case PhysicsServer3D::BODY_MODE_RIGID_LINEAR: {
  904. _integrate_forces(p_step, p_jolt_body);
  905. } break;
  906. case PhysicsServer3D::BODY_MODE_KINEMATIC: {
  907. _update_gravity(p_jolt_body);
  908. _move_kinematic(p_step, p_jolt_body);
  909. } break;
  910. }
  911. if (_should_call_queries()) {
  912. _enqueue_call_queries();
  913. }
  914. contact_count = 0;
  915. }
  916. JoltPhysicsDirectBodyState3D *JoltBody3D::get_direct_state() {
  917. if (direct_state == nullptr) {
  918. direct_state = memnew(JoltPhysicsDirectBodyState3D(this));
  919. }
  920. return direct_state;
  921. }
  922. void JoltBody3D::set_mode(PhysicsServer3D::BodyMode p_mode) {
  923. if (p_mode == mode) {
  924. return;
  925. }
  926. mode = p_mode;
  927. if (!in_space()) {
  928. _mode_changed();
  929. return;
  930. }
  931. const JPH::EMotionType motion_type = _get_motion_type();
  932. if (motion_type == JPH::EMotionType::Static) {
  933. put_to_sleep();
  934. }
  935. jolt_body->SetMotionType(motion_type);
  936. if (motion_type != JPH::EMotionType::Static) {
  937. wake_up();
  938. }
  939. if (motion_type == JPH::EMotionType::Kinematic) {
  940. jolt_body->SetLinearVelocity(JPH::Vec3::sZero());
  941. jolt_body->SetAngularVelocity(JPH::Vec3::sZero());
  942. }
  943. linear_surface_velocity = Vector3();
  944. angular_surface_velocity = Vector3();
  945. _mode_changed();
  946. }
  947. bool JoltBody3D::is_ccd_enabled() const {
  948. if (!in_space()) {
  949. return jolt_settings->mMotionQuality == JPH::EMotionQuality::LinearCast;
  950. } else {
  951. return !is_static() && jolt_body->GetMotionProperties()->GetMotionQuality() == JPH::EMotionQuality::LinearCast;
  952. }
  953. }
  954. void JoltBody3D::set_ccd_enabled(bool p_enabled) {
  955. const JPH::EMotionQuality motion_quality = p_enabled ? JPH::EMotionQuality::LinearCast : JPH::EMotionQuality::Discrete;
  956. if (!in_space()) {
  957. jolt_settings->mMotionQuality = motion_quality;
  958. } else {
  959. space->get_body_iface().SetMotionQuality(jolt_body->GetID(), motion_quality);
  960. }
  961. }
  962. void JoltBody3D::set_mass(float p_mass) {
  963. if (p_mass != mass) {
  964. mass = p_mass;
  965. _update_mass_properties();
  966. }
  967. }
  968. void JoltBody3D::set_inertia(const Vector3 &p_inertia) {
  969. if (p_inertia != inertia) {
  970. inertia = p_inertia;
  971. _update_mass_properties();
  972. }
  973. }
  974. float JoltBody3D::get_bounce() const {
  975. if (!in_space()) {
  976. return jolt_settings->mRestitution;
  977. } else {
  978. return jolt_body->GetRestitution();
  979. }
  980. }
  981. void JoltBody3D::set_bounce(float p_bounce) {
  982. if (!in_space()) {
  983. jolt_settings->mRestitution = p_bounce;
  984. } else {
  985. jolt_body->SetRestitution(p_bounce);
  986. }
  987. }
  988. float JoltBody3D::get_friction() const {
  989. if (!in_space()) {
  990. return jolt_settings->mFriction;
  991. } else {
  992. return jolt_body->GetFriction();
  993. }
  994. }
  995. void JoltBody3D::set_friction(float p_friction) {
  996. if (!in_space()) {
  997. jolt_settings->mFriction = p_friction;
  998. } else {
  999. jolt_body->SetFriction(p_friction);
  1000. }
  1001. }
  1002. void JoltBody3D::set_gravity_scale(float p_scale) {
  1003. if (gravity_scale == p_scale) {
  1004. return;
  1005. }
  1006. gravity_scale = p_scale;
  1007. _motion_changed();
  1008. }
  1009. void JoltBody3D::set_linear_damp(float p_damp) {
  1010. p_damp = MAX(0.0f, p_damp);
  1011. if (p_damp == linear_damp) {
  1012. return;
  1013. }
  1014. linear_damp = p_damp;
  1015. _update_damp();
  1016. }
  1017. void JoltBody3D::set_angular_damp(float p_damp) {
  1018. p_damp = MAX(0.0f, p_damp);
  1019. if (p_damp == angular_damp) {
  1020. return;
  1021. }
  1022. angular_damp = p_damp;
  1023. _update_damp();
  1024. }
  1025. void JoltBody3D::set_linear_damp_mode(DampMode p_mode) {
  1026. if (p_mode == linear_damp_mode) {
  1027. return;
  1028. }
  1029. linear_damp_mode = p_mode;
  1030. _update_damp();
  1031. }
  1032. void JoltBody3D::set_angular_damp_mode(DampMode p_mode) {
  1033. if (p_mode == angular_damp_mode) {
  1034. return;
  1035. }
  1036. angular_damp_mode = p_mode;
  1037. _update_damp();
  1038. }
  1039. bool JoltBody3D::is_axis_locked(PhysicsServer3D::BodyAxis p_axis) const {
  1040. return (locked_axes & (uint32_t)p_axis) != 0;
  1041. }
  1042. void JoltBody3D::set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool p_enabled) {
  1043. const uint32_t previous_locked_axes = locked_axes;
  1044. if (p_enabled) {
  1045. locked_axes |= (uint32_t)p_axis;
  1046. } else {
  1047. locked_axes &= ~(uint32_t)p_axis;
  1048. }
  1049. if (previous_locked_axes != locked_axes) {
  1050. _axis_lock_changed();
  1051. }
  1052. }
  1053. bool JoltBody3D::can_interact_with(const JoltBody3D &p_other) const {
  1054. return (can_collide_with(p_other) || p_other.can_collide_with(*this)) && !has_collision_exception(p_other.get_rid()) && !p_other.has_collision_exception(rid);
  1055. }
  1056. bool JoltBody3D::can_interact_with(const JoltSoftBody3D &p_other) const {
  1057. return p_other.can_interact_with(*this);
  1058. }
  1059. bool JoltBody3D::can_interact_with(const JoltArea3D &p_other) const {
  1060. return p_other.can_interact_with(*this);
  1061. }