BodyInterface.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/Body/BodyInterface.h>
  6. #include <Jolt/Physics/Collision/BroadPhase/BroadPhase.h>
  7. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  8. #include <Jolt/Physics/Body/Body.h>
  9. #include <Jolt/Physics/Body/BodyManager.h>
  10. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  11. #include <Jolt/Physics/Body/BodyLock.h>
  12. #include <Jolt/Physics/Body/BodyLockMulti.h>
  13. #include <Jolt/Physics/Collision/PhysicsMaterial.h>
  14. #include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
  15. JPH_NAMESPACE_BEGIN
  16. void BodyInterface::ActivateBodyInternal(Body &ioBody) const
  17. {
  18. // Activate body or reset its sleep timer.
  19. // Note that BodyManager::ActivateBodies also resets the sleep timer internally, but we avoid a mutex lock if the body is already active by calling ResetSleepTimer directly.
  20. if (!ioBody.IsActive())
  21. mBodyManager->ActivateBodies(&ioBody.GetID(), 1);
  22. else
  23. ioBody.ResetSleepTimer();
  24. }
  25. Body *BodyInterface::CreateBody(const BodyCreationSettings &inSettings)
  26. {
  27. Body *body = mBodyManager->AllocateBody(inSettings);
  28. if (!mBodyManager->AddBody(body))
  29. {
  30. mBodyManager->FreeBody(body);
  31. return nullptr;
  32. }
  33. return body;
  34. }
  35. Body *BodyInterface::CreateSoftBody(const SoftBodyCreationSettings &inSettings)
  36. {
  37. Body *body = mBodyManager->AllocateSoftBody(inSettings);
  38. if (!mBodyManager->AddBody(body))
  39. {
  40. mBodyManager->FreeBody(body);
  41. return nullptr;
  42. }
  43. return body;
  44. }
  45. Body *BodyInterface::CreateBodyWithID(const BodyID &inBodyID, const BodyCreationSettings &inSettings)
  46. {
  47. Body *body = mBodyManager->AllocateBody(inSettings);
  48. if (!mBodyManager->AddBodyWithCustomID(body, inBodyID))
  49. {
  50. mBodyManager->FreeBody(body);
  51. return nullptr;
  52. }
  53. return body;
  54. }
  55. Body *BodyInterface::CreateSoftBodyWithID(const BodyID &inBodyID, const SoftBodyCreationSettings &inSettings)
  56. {
  57. Body *body = mBodyManager->AllocateSoftBody(inSettings);
  58. if (!mBodyManager->AddBodyWithCustomID(body, inBodyID))
  59. {
  60. mBodyManager->FreeBody(body);
  61. return nullptr;
  62. }
  63. return body;
  64. }
  65. Body *BodyInterface::CreateBodyWithoutID(const BodyCreationSettings &inSettings) const
  66. {
  67. return mBodyManager->AllocateBody(inSettings);
  68. }
  69. Body *BodyInterface::CreateSoftBodyWithoutID(const SoftBodyCreationSettings &inSettings) const
  70. {
  71. return mBodyManager->AllocateSoftBody(inSettings);
  72. }
  73. void BodyInterface::DestroyBodyWithoutID(Body *inBody) const
  74. {
  75. mBodyManager->FreeBody(inBody);
  76. }
  77. bool BodyInterface::AssignBodyID(Body *ioBody)
  78. {
  79. return mBodyManager->AddBody(ioBody);
  80. }
  81. bool BodyInterface::AssignBodyID(Body *ioBody, const BodyID &inBodyID)
  82. {
  83. return mBodyManager->AddBodyWithCustomID(ioBody, inBodyID);
  84. }
  85. Body *BodyInterface::UnassignBodyID(const BodyID &inBodyID)
  86. {
  87. Body *body = nullptr;
  88. mBodyManager->RemoveBodies(&inBodyID, 1, &body);
  89. return body;
  90. }
  91. void BodyInterface::UnassignBodyIDs(const BodyID *inBodyIDs, int inNumber, Body **outBodies)
  92. {
  93. mBodyManager->RemoveBodies(inBodyIDs, inNumber, outBodies);
  94. }
  95. void BodyInterface::DestroyBody(const BodyID &inBodyID)
  96. {
  97. mBodyManager->DestroyBodies(&inBodyID, 1);
  98. }
  99. void BodyInterface::DestroyBodies(const BodyID *inBodyIDs, int inNumber)
  100. {
  101. mBodyManager->DestroyBodies(inBodyIDs, inNumber);
  102. }
  103. void BodyInterface::AddBody(const BodyID &inBodyID, EActivation inActivationMode)
  104. {
  105. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  106. if (lock.Succeeded())
  107. {
  108. const Body &body = lock.GetBody();
  109. // Add to broadphase
  110. BodyID id = inBodyID;
  111. BroadPhase::AddState add_state = mBroadPhase->AddBodiesPrepare(&id, 1);
  112. mBroadPhase->AddBodiesFinalize(&id, 1, add_state);
  113. // Optionally activate body
  114. if (inActivationMode == EActivation::Activate && !body.IsStatic())
  115. mBodyManager->ActivateBodies(&inBodyID, 1);
  116. }
  117. }
  118. void BodyInterface::RemoveBody(const BodyID &inBodyID)
  119. {
  120. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  121. if (lock.Succeeded())
  122. {
  123. const Body &body = lock.GetBody();
  124. // Deactivate body
  125. if (body.IsActive())
  126. mBodyManager->DeactivateBodies(&inBodyID, 1);
  127. // Remove from broadphase
  128. BodyID id = inBodyID;
  129. mBroadPhase->RemoveBodies(&id, 1);
  130. }
  131. }
  132. bool BodyInterface::IsAdded(const BodyID &inBodyID) const
  133. {
  134. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  135. return lock.SucceededAndIsInBroadPhase();
  136. }
  137. BodyID BodyInterface::CreateAndAddBody(const BodyCreationSettings &inSettings, EActivation inActivationMode)
  138. {
  139. const Body *b = CreateBody(inSettings);
  140. if (b == nullptr)
  141. return BodyID(); // Out of bodies
  142. AddBody(b->GetID(), inActivationMode);
  143. return b->GetID();
  144. }
  145. BodyID BodyInterface::CreateAndAddSoftBody(const SoftBodyCreationSettings &inSettings, EActivation inActivationMode)
  146. {
  147. const Body *b = CreateSoftBody(inSettings);
  148. if (b == nullptr)
  149. return BodyID(); // Out of bodies
  150. AddBody(b->GetID(), inActivationMode);
  151. return b->GetID();
  152. }
  153. BodyInterface::AddState BodyInterface::AddBodiesPrepare(BodyID *ioBodies, int inNumber)
  154. {
  155. return mBroadPhase->AddBodiesPrepare(ioBodies, inNumber);
  156. }
  157. void BodyInterface::AddBodiesFinalize(BodyID *ioBodies, int inNumber, AddState inAddState, EActivation inActivationMode)
  158. {
  159. BodyLockMultiWrite lock(*mBodyLockInterface, ioBodies, inNumber);
  160. // Add to broadphase
  161. mBroadPhase->AddBodiesFinalize(ioBodies, inNumber, inAddState);
  162. // Optionally activate bodies
  163. if (inActivationMode == EActivation::Activate)
  164. mBodyManager->ActivateBodies(ioBodies, inNumber);
  165. }
  166. void BodyInterface::AddBodiesAbort(BodyID *ioBodies, int inNumber, AddState inAddState)
  167. {
  168. mBroadPhase->AddBodiesAbort(ioBodies, inNumber, inAddState);
  169. }
  170. void BodyInterface::RemoveBodies(BodyID *ioBodies, int inNumber)
  171. {
  172. BodyLockMultiWrite lock(*mBodyLockInterface, ioBodies, inNumber);
  173. // Deactivate bodies
  174. mBodyManager->DeactivateBodies(ioBodies, inNumber);
  175. // Remove from broadphase
  176. mBroadPhase->RemoveBodies(ioBodies, inNumber);
  177. }
  178. void BodyInterface::ActivateBody(const BodyID &inBodyID)
  179. {
  180. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  181. if (lock.Succeeded())
  182. {
  183. Body &body = lock.GetBody();
  184. ActivateBodyInternal(body);
  185. }
  186. }
  187. void BodyInterface::ActivateBodies(const BodyID *inBodyIDs, int inNumber)
  188. {
  189. BodyLockMultiWrite lock(*mBodyLockInterface, inBodyIDs, inNumber);
  190. mBodyManager->ActivateBodies(inBodyIDs, inNumber);
  191. }
  192. void BodyInterface::ActivateBodiesInAABox(const AABox &inBox, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter)
  193. {
  194. AllHitCollisionCollector<CollideShapeBodyCollector> collector;
  195. mBroadPhase->CollideAABox(inBox, collector, inBroadPhaseLayerFilter, inObjectLayerFilter);
  196. ActivateBodies(collector.mHits.data(), (int)collector.mHits.size());
  197. }
  198. void BodyInterface::DeactivateBody(const BodyID &inBodyID)
  199. {
  200. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  201. if (lock.Succeeded())
  202. {
  203. const Body &body = lock.GetBody();
  204. if (body.IsActive())
  205. mBodyManager->DeactivateBodies(&inBodyID, 1);
  206. }
  207. }
  208. void BodyInterface::DeactivateBodies(const BodyID *inBodyIDs, int inNumber)
  209. {
  210. BodyLockMultiWrite lock(*mBodyLockInterface, inBodyIDs, inNumber);
  211. mBodyManager->DeactivateBodies(inBodyIDs, inNumber);
  212. }
  213. bool BodyInterface::IsActive(const BodyID &inBodyID) const
  214. {
  215. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  216. return lock.Succeeded() && lock.GetBody().IsActive();
  217. }
  218. void BodyInterface::ResetSleepTimer(const BodyID &inBodyID)
  219. {
  220. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  221. if (lock.Succeeded())
  222. lock.GetBody().ResetSleepTimer();
  223. }
  224. TwoBodyConstraint *BodyInterface::CreateConstraint(const TwoBodyConstraintSettings *inSettings, const BodyID &inBodyID1, const BodyID &inBodyID2)
  225. {
  226. BodyID constraint_bodies[] = { inBodyID1, inBodyID2 };
  227. BodyLockMultiWrite lock(*mBodyLockInterface, constraint_bodies, 2);
  228. Body *body1 = lock.GetBody(0);
  229. Body *body2 = lock.GetBody(1);
  230. JPH_ASSERT(body1 != body2);
  231. JPH_ASSERT(body1 != nullptr || body2 != nullptr);
  232. return inSettings->Create(body1 != nullptr? *body1 : Body::sFixedToWorld, body2 != nullptr? *body2 : Body::sFixedToWorld);
  233. }
  234. void BodyInterface::ActivateConstraint(const TwoBodyConstraint *inConstraint)
  235. {
  236. BodyID bodies[] = { inConstraint->GetBody1()->GetID(), inConstraint->GetBody2()->GetID() };
  237. ActivateBodies(bodies, 2);
  238. }
  239. RefConst<Shape> BodyInterface::GetShape(const BodyID &inBodyID) const
  240. {
  241. RefConst<Shape> shape;
  242. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  243. if (lock.Succeeded())
  244. shape = lock.GetBody().GetShape();
  245. return shape;
  246. }
  247. void BodyInterface::SetShape(const BodyID &inBodyID, const Shape *inShape, bool inUpdateMassProperties, EActivation inActivationMode) const
  248. {
  249. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  250. if (lock.Succeeded())
  251. {
  252. Body &body = lock.GetBody();
  253. // Check if shape actually changed
  254. if (body.GetShape() != inShape)
  255. {
  256. // Update the shape
  257. body.SetShapeInternal(inShape, inUpdateMassProperties);
  258. // Notify broadphase of change
  259. if (body.IsInBroadPhase())
  260. {
  261. // Flag collision cache invalid for this body
  262. mBodyManager->InvalidateContactCacheForBody(body);
  263. BodyID id = body.GetID();
  264. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  265. // Optionally activate body
  266. if (inActivationMode == EActivation::Activate && !body.IsStatic())
  267. ActivateBodyInternal(body);
  268. }
  269. }
  270. }
  271. }
  272. void BodyInterface::NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inPreviousCenterOfMass, bool inUpdateMassProperties, EActivation inActivationMode) const
  273. {
  274. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  275. if (lock.Succeeded())
  276. {
  277. Body &body = lock.GetBody();
  278. // Update center of mass, mass and inertia
  279. body.UpdateCenterOfMassInternal(inPreviousCenterOfMass, inUpdateMassProperties);
  280. // Recalculate bounding box
  281. body.CalculateWorldSpaceBoundsInternal();
  282. // Notify broadphase of change
  283. if (body.IsInBroadPhase())
  284. {
  285. // Flag collision cache invalid for this body
  286. mBodyManager->InvalidateContactCacheForBody(body);
  287. BodyID id = body.GetID();
  288. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  289. // Optionally activate body
  290. if (inActivationMode == EActivation::Activate && !body.IsStatic())
  291. ActivateBodyInternal(body);
  292. }
  293. }
  294. }
  295. void BodyInterface::SetObjectLayer(const BodyID &inBodyID, ObjectLayer inLayer)
  296. {
  297. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  298. if (lock.Succeeded())
  299. {
  300. Body &body = lock.GetBody();
  301. // Check if layer actually changed, updating the broadphase is rather expensive
  302. if (body.GetObjectLayer() != inLayer)
  303. {
  304. // Update the layer on the body
  305. mBodyManager->SetBodyObjectLayerInternal(body, inLayer);
  306. // Notify broadphase of change
  307. if (body.IsInBroadPhase())
  308. {
  309. BodyID id = body.GetID();
  310. mBroadPhase->NotifyBodiesLayerChanged(&id, 1);
  311. }
  312. }
  313. }
  314. }
  315. ObjectLayer BodyInterface::GetObjectLayer(const BodyID &inBodyID) const
  316. {
  317. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  318. if (lock.Succeeded())
  319. return lock.GetBody().GetObjectLayer();
  320. else
  321. return cObjectLayerInvalid;
  322. }
  323. void BodyInterface::SetPositionAndRotation(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode)
  324. {
  325. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  326. if (lock.Succeeded())
  327. {
  328. Body &body = lock.GetBody();
  329. // Update the position
  330. body.SetPositionAndRotationInternal(inPosition, inRotation);
  331. // Notify broadphase of change
  332. if (body.IsInBroadPhase())
  333. {
  334. BodyID id = body.GetID();
  335. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  336. // Optionally activate body
  337. if (inActivationMode == EActivation::Activate && !body.IsStatic())
  338. ActivateBodyInternal(body);
  339. }
  340. }
  341. }
  342. void BodyInterface::SetPositionAndRotationWhenChanged(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode)
  343. {
  344. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  345. if (lock.Succeeded())
  346. {
  347. Body &body = lock.GetBody();
  348. // Check if there is enough change
  349. if (!body.GetPosition().IsClose(inPosition)
  350. || !body.GetRotation().IsClose(inRotation))
  351. {
  352. // Update the position
  353. body.SetPositionAndRotationInternal(inPosition, inRotation);
  354. // Notify broadphase of change
  355. if (body.IsInBroadPhase())
  356. {
  357. BodyID id = body.GetID();
  358. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  359. // Optionally activate body
  360. if (inActivationMode == EActivation::Activate && !body.IsStatic())
  361. ActivateBodyInternal(body);
  362. }
  363. }
  364. }
  365. }
  366. void BodyInterface::GetPositionAndRotation(const BodyID &inBodyID, RVec3 &outPosition, Quat &outRotation) const
  367. {
  368. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  369. if (lock.Succeeded())
  370. {
  371. const Body &body = lock.GetBody();
  372. outPosition = body.GetPosition();
  373. outRotation = body.GetRotation();
  374. }
  375. else
  376. {
  377. outPosition = RVec3::sZero();
  378. outRotation = Quat::sIdentity();
  379. }
  380. }
  381. void BodyInterface::SetPosition(const BodyID &inBodyID, RVec3Arg inPosition, EActivation inActivationMode)
  382. {
  383. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  384. if (lock.Succeeded())
  385. {
  386. Body &body = lock.GetBody();
  387. // Update the position
  388. body.SetPositionAndRotationInternal(inPosition, body.GetRotation());
  389. // Notify broadphase of change
  390. if (body.IsInBroadPhase())
  391. {
  392. BodyID id = body.GetID();
  393. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  394. // Optionally activate body
  395. if (inActivationMode == EActivation::Activate && !body.IsStatic())
  396. ActivateBodyInternal(body);
  397. }
  398. }
  399. }
  400. RVec3 BodyInterface::GetPosition(const BodyID &inBodyID) const
  401. {
  402. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  403. if (lock.Succeeded())
  404. return lock.GetBody().GetPosition();
  405. else
  406. return RVec3::sZero();
  407. }
  408. RVec3 BodyInterface::GetCenterOfMassPosition(const BodyID &inBodyID) const
  409. {
  410. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  411. if (lock.Succeeded())
  412. return lock.GetBody().GetCenterOfMassPosition();
  413. else
  414. return RVec3::sZero();
  415. }
  416. void BodyInterface::SetRotation(const BodyID &inBodyID, QuatArg inRotation, EActivation inActivationMode)
  417. {
  418. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  419. if (lock.Succeeded())
  420. {
  421. Body &body = lock.GetBody();
  422. // Update the position
  423. body.SetPositionAndRotationInternal(body.GetPosition(), inRotation);
  424. // Notify broadphase of change
  425. if (body.IsInBroadPhase())
  426. {
  427. BodyID id = body.GetID();
  428. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  429. // Optionally activate body
  430. if (inActivationMode == EActivation::Activate && !body.IsStatic())
  431. ActivateBodyInternal(body);
  432. }
  433. }
  434. }
  435. Quat BodyInterface::GetRotation(const BodyID &inBodyID) const
  436. {
  437. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  438. if (lock.Succeeded())
  439. return lock.GetBody().GetRotation();
  440. else
  441. return Quat::sIdentity();
  442. }
  443. RMat44 BodyInterface::GetWorldTransform(const BodyID &inBodyID) const
  444. {
  445. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  446. if (lock.Succeeded())
  447. return lock.GetBody().GetWorldTransform();
  448. else
  449. return RMat44::sIdentity();
  450. }
  451. RMat44 BodyInterface::GetCenterOfMassTransform(const BodyID &inBodyID) const
  452. {
  453. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  454. if (lock.Succeeded())
  455. return lock.GetBody().GetCenterOfMassTransform();
  456. else
  457. return RMat44::sIdentity();
  458. }
  459. void BodyInterface::MoveKinematic(const BodyID &inBodyID, RVec3Arg inTargetPosition, QuatArg inTargetRotation, float inDeltaTime)
  460. {
  461. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  462. if (lock.Succeeded())
  463. {
  464. Body &body = lock.GetBody();
  465. body.MoveKinematic(inTargetPosition, inTargetRotation, inDeltaTime);
  466. if (!body.IsActive() && (!body.GetLinearVelocity().IsNearZero() || !body.GetAngularVelocity().IsNearZero()) && body.IsInBroadPhase())
  467. mBodyManager->ActivateBodies(&inBodyID, 1);
  468. }
  469. }
  470. void BodyInterface::SetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity)
  471. {
  472. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  473. if (lock.Succeeded())
  474. {
  475. Body &body = lock.GetBody();
  476. if (!body.IsStatic())
  477. {
  478. body.SetLinearVelocityClamped(inLinearVelocity);
  479. body.SetAngularVelocityClamped(inAngularVelocity);
  480. if (!body.IsActive() && (!inLinearVelocity.IsNearZero() || !inAngularVelocity.IsNearZero()) && body.IsInBroadPhase())
  481. mBodyManager->ActivateBodies(&inBodyID, 1);
  482. }
  483. }
  484. }
  485. void BodyInterface::GetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3 &outLinearVelocity, Vec3 &outAngularVelocity) const
  486. {
  487. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  488. if (lock.Succeeded())
  489. {
  490. const Body &body = lock.GetBody();
  491. if (!body.IsStatic())
  492. {
  493. outLinearVelocity = body.GetLinearVelocity();
  494. outAngularVelocity = body.GetAngularVelocity();
  495. return;
  496. }
  497. }
  498. outLinearVelocity = outAngularVelocity = Vec3::sZero();
  499. }
  500. void BodyInterface::SetLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity)
  501. {
  502. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  503. if (lock.Succeeded())
  504. {
  505. Body &body = lock.GetBody();
  506. if (!body.IsStatic())
  507. {
  508. body.SetLinearVelocityClamped(inLinearVelocity);
  509. if (!body.IsActive() && !inLinearVelocity.IsNearZero() && body.IsInBroadPhase())
  510. mBodyManager->ActivateBodies(&inBodyID, 1);
  511. }
  512. }
  513. }
  514. Vec3 BodyInterface::GetLinearVelocity(const BodyID &inBodyID) const
  515. {
  516. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  517. if (lock.Succeeded())
  518. {
  519. const Body &body = lock.GetBody();
  520. if (!body.IsStatic())
  521. return body.GetLinearVelocity();
  522. }
  523. return Vec3::sZero();
  524. }
  525. void BodyInterface::AddLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity)
  526. {
  527. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  528. if (lock.Succeeded())
  529. {
  530. Body &body = lock.GetBody();
  531. if (!body.IsStatic())
  532. {
  533. body.SetLinearVelocityClamped(body.GetLinearVelocity() + inLinearVelocity);
  534. if (!body.IsActive() && !body.GetLinearVelocity().IsNearZero() && body.IsInBroadPhase())
  535. mBodyManager->ActivateBodies(&inBodyID, 1);
  536. }
  537. }
  538. }
  539. void BodyInterface::AddLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity)
  540. {
  541. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  542. if (lock.Succeeded())
  543. {
  544. Body &body = lock.GetBody();
  545. if (!body.IsStatic())
  546. {
  547. body.SetLinearVelocityClamped(body.GetLinearVelocity() + inLinearVelocity);
  548. body.SetAngularVelocityClamped(body.GetAngularVelocity() + inAngularVelocity);
  549. if (!body.IsActive() && (!body.GetLinearVelocity().IsNearZero() || !body.GetAngularVelocity().IsNearZero()) && body.IsInBroadPhase())
  550. mBodyManager->ActivateBodies(&inBodyID, 1);
  551. }
  552. }
  553. }
  554. void BodyInterface::SetAngularVelocity(const BodyID &inBodyID, Vec3Arg inAngularVelocity)
  555. {
  556. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  557. if (lock.Succeeded())
  558. {
  559. Body &body = lock.GetBody();
  560. if (!body.IsStatic())
  561. {
  562. body.SetAngularVelocityClamped(inAngularVelocity);
  563. if (!body.IsActive() && !inAngularVelocity.IsNearZero() && body.IsInBroadPhase())
  564. mBodyManager->ActivateBodies(&inBodyID, 1);
  565. }
  566. }
  567. }
  568. Vec3 BodyInterface::GetAngularVelocity(const BodyID &inBodyID) const
  569. {
  570. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  571. if (lock.Succeeded())
  572. {
  573. const Body &body = lock.GetBody();
  574. if (!body.IsStatic())
  575. return body.GetAngularVelocity();
  576. }
  577. return Vec3::sZero();
  578. }
  579. Vec3 BodyInterface::GetPointVelocity(const BodyID &inBodyID, RVec3Arg inPoint) const
  580. {
  581. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  582. if (lock.Succeeded())
  583. {
  584. const Body &body = lock.GetBody();
  585. if (!body.IsStatic())
  586. return body.GetPointVelocity(inPoint);
  587. }
  588. return Vec3::sZero();
  589. }
  590. void BodyInterface::AddForce(const BodyID &inBodyID, Vec3Arg inForce, EActivation inActivationMode)
  591. {
  592. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  593. if (lock.Succeeded())
  594. {
  595. Body &body = lock.GetBody();
  596. if (body.IsDynamic() && (inActivationMode == EActivation::Activate || body.IsActive()))
  597. {
  598. body.AddForce(inForce);
  599. if (inActivationMode == EActivation::Activate)
  600. ActivateBodyInternal(body);
  601. }
  602. }
  603. }
  604. void BodyInterface::AddForce(const BodyID &inBodyID, Vec3Arg inForce, RVec3Arg inPoint, EActivation inActivationMode)
  605. {
  606. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  607. if (lock.Succeeded())
  608. {
  609. Body &body = lock.GetBody();
  610. if (body.IsDynamic() && (inActivationMode == EActivation::Activate || body.IsActive()))
  611. {
  612. body.AddForce(inForce, inPoint);
  613. if (inActivationMode == EActivation::Activate)
  614. ActivateBodyInternal(body);
  615. }
  616. }
  617. }
  618. void BodyInterface::AddTorque(const BodyID &inBodyID, Vec3Arg inTorque, EActivation inActivationMode)
  619. {
  620. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  621. if (lock.Succeeded())
  622. {
  623. Body &body = lock.GetBody();
  624. if (body.IsDynamic() && (inActivationMode == EActivation::Activate || body.IsActive()))
  625. {
  626. body.AddTorque(inTorque);
  627. if (inActivationMode == EActivation::Activate)
  628. ActivateBodyInternal(body);
  629. }
  630. }
  631. }
  632. void BodyInterface::AddForceAndTorque(const BodyID &inBodyID, Vec3Arg inForce, Vec3Arg inTorque, EActivation inActivationMode)
  633. {
  634. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  635. if (lock.Succeeded())
  636. {
  637. Body &body = lock.GetBody();
  638. if (body.IsDynamic() && (inActivationMode == EActivation::Activate || body.IsActive()))
  639. {
  640. body.AddForce(inForce);
  641. body.AddTorque(inTorque);
  642. if (inActivationMode == EActivation::Activate)
  643. ActivateBodyInternal(body);
  644. }
  645. }
  646. }
  647. void BodyInterface::AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse)
  648. {
  649. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  650. if (lock.Succeeded())
  651. {
  652. Body &body = lock.GetBody();
  653. if (body.IsDynamic())
  654. {
  655. body.AddImpulse(inImpulse);
  656. if (!body.IsActive())
  657. mBodyManager->ActivateBodies(&inBodyID, 1);
  658. }
  659. }
  660. }
  661. void BodyInterface::AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse, RVec3Arg inPoint)
  662. {
  663. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  664. if (lock.Succeeded())
  665. {
  666. Body &body = lock.GetBody();
  667. if (body.IsDynamic())
  668. {
  669. body.AddImpulse(inImpulse, inPoint);
  670. if (!body.IsActive())
  671. mBodyManager->ActivateBodies(&inBodyID, 1);
  672. }
  673. }
  674. }
  675. void BodyInterface::AddAngularImpulse(const BodyID &inBodyID, Vec3Arg inAngularImpulse)
  676. {
  677. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  678. if (lock.Succeeded())
  679. {
  680. Body &body = lock.GetBody();
  681. if (body.IsDynamic())
  682. {
  683. body.AddAngularImpulse(inAngularImpulse);
  684. if (!body.IsActive())
  685. mBodyManager->ActivateBodies(&inBodyID, 1);
  686. }
  687. }
  688. }
  689. bool BodyInterface::ApplyBuoyancyImpulse(const BodyID &inBodyID, RVec3Arg inSurfacePosition, Vec3Arg inSurfaceNormal, float inBuoyancy, float inLinearDrag, float inAngularDrag, Vec3Arg inFluidVelocity, Vec3Arg inGravity, float inDeltaTime)
  690. {
  691. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  692. if (lock.Succeeded())
  693. {
  694. Body &body = lock.GetBody();
  695. if (body.IsDynamic()
  696. && body.ApplyBuoyancyImpulse(inSurfacePosition, inSurfaceNormal, inBuoyancy, inLinearDrag, inAngularDrag, inFluidVelocity, inGravity, inDeltaTime))
  697. {
  698. ActivateBodyInternal(body);
  699. return true;
  700. }
  701. }
  702. return false;
  703. }
  704. void BodyInterface::SetPositionRotationAndVelocity(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity)
  705. {
  706. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  707. if (lock.Succeeded())
  708. {
  709. Body &body = lock.GetBody();
  710. // Update the position
  711. body.SetPositionAndRotationInternal(inPosition, inRotation);
  712. // Notify broadphase of change
  713. if (body.IsInBroadPhase())
  714. {
  715. BodyID id = body.GetID();
  716. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  717. }
  718. if (!body.IsStatic())
  719. {
  720. body.SetLinearVelocityClamped(inLinearVelocity);
  721. body.SetAngularVelocityClamped(inAngularVelocity);
  722. // Optionally activate body
  723. if (!body.IsActive() && (!inLinearVelocity.IsNearZero() || !inAngularVelocity.IsNearZero()) && body.IsInBroadPhase())
  724. mBodyManager->ActivateBodies(&inBodyID, 1);
  725. }
  726. }
  727. }
  728. void BodyInterface::SetMotionType(const BodyID &inBodyID, EMotionType inMotionType, EActivation inActivationMode)
  729. {
  730. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  731. if (lock.Succeeded())
  732. {
  733. Body &body = lock.GetBody();
  734. // Deactivate if we're making the body static
  735. if (body.IsActive() && inMotionType == EMotionType::Static)
  736. mBodyManager->DeactivateBodies(&inBodyID, 1);
  737. body.SetMotionType(inMotionType);
  738. // Activate body if requested
  739. if (inMotionType != EMotionType::Static && inActivationMode == EActivation::Activate && body.IsInBroadPhase())
  740. ActivateBodyInternal(body);
  741. }
  742. }
  743. EBodyType BodyInterface::GetBodyType(const BodyID &inBodyID) const
  744. {
  745. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  746. if (lock.Succeeded())
  747. return lock.GetBody().GetBodyType();
  748. else
  749. return EBodyType::RigidBody;
  750. }
  751. EMotionType BodyInterface::GetMotionType(const BodyID &inBodyID) const
  752. {
  753. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  754. if (lock.Succeeded())
  755. return lock.GetBody().GetMotionType();
  756. else
  757. return EMotionType::Static;
  758. }
  759. void BodyInterface::SetMotionQuality(const BodyID &inBodyID, EMotionQuality inMotionQuality)
  760. {
  761. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  762. if (lock.Succeeded())
  763. mBodyManager->SetMotionQuality(lock.GetBody(), inMotionQuality);
  764. }
  765. EMotionQuality BodyInterface::GetMotionQuality(const BodyID &inBodyID) const
  766. {
  767. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  768. if (lock.Succeeded() && !lock.GetBody().IsStatic())
  769. return lock.GetBody().GetMotionProperties()->GetMotionQuality();
  770. else
  771. return EMotionQuality::Discrete;
  772. }
  773. Mat44 BodyInterface::GetInverseInertia(const BodyID &inBodyID) const
  774. {
  775. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  776. if (lock.Succeeded())
  777. return lock.GetBody().GetInverseInertia();
  778. else
  779. return Mat44::sIdentity();
  780. }
  781. void BodyInterface::SetRestitution(const BodyID &inBodyID, float inRestitution)
  782. {
  783. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  784. if (lock.Succeeded())
  785. lock.GetBody().SetRestitution(inRestitution);
  786. }
  787. float BodyInterface::GetRestitution(const BodyID &inBodyID) const
  788. {
  789. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  790. if (lock.Succeeded())
  791. return lock.GetBody().GetRestitution();
  792. else
  793. return 0.0f;
  794. }
  795. void BodyInterface::SetFriction(const BodyID &inBodyID, float inFriction)
  796. {
  797. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  798. if (lock.Succeeded())
  799. lock.GetBody().SetFriction(inFriction);
  800. }
  801. float BodyInterface::GetFriction(const BodyID &inBodyID) const
  802. {
  803. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  804. if (lock.Succeeded())
  805. return lock.GetBody().GetFriction();
  806. else
  807. return 0.0f;
  808. }
  809. void BodyInterface::SetGravityFactor(const BodyID &inBodyID, float inGravityFactor)
  810. {
  811. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  812. if (lock.Succeeded() && lock.GetBody().GetMotionPropertiesUnchecked() != nullptr)
  813. lock.GetBody().GetMotionPropertiesUnchecked()->SetGravityFactor(inGravityFactor);
  814. }
  815. float BodyInterface::GetGravityFactor(const BodyID &inBodyID) const
  816. {
  817. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  818. if (lock.Succeeded() && lock.GetBody().GetMotionPropertiesUnchecked() != nullptr)
  819. return lock.GetBody().GetMotionPropertiesUnchecked()->GetGravityFactor();
  820. else
  821. return 1.0f;
  822. }
  823. void BodyInterface::SetUseManifoldReduction(const BodyID &inBodyID, bool inUseReduction)
  824. {
  825. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  826. if (lock.Succeeded())
  827. {
  828. Body &body = lock.GetBody();
  829. if (body.GetUseManifoldReduction() != inUseReduction)
  830. {
  831. body.SetUseManifoldReduction(inUseReduction);
  832. // Flag collision cache invalid for this body
  833. if (body.IsInBroadPhase())
  834. mBodyManager->InvalidateContactCacheForBody(body);
  835. }
  836. }
  837. }
  838. bool BodyInterface::GetUseManifoldReduction(const BodyID &inBodyID) const
  839. {
  840. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  841. if (lock.Succeeded())
  842. return lock.GetBody().GetUseManifoldReduction();
  843. else
  844. return true;
  845. }
  846. void BodyInterface::SetCollisionGroup(const BodyID &inBodyID, const CollisionGroup &inCollisionGroup)
  847. {
  848. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  849. if (lock.Succeeded())
  850. lock.GetBody().SetCollisionGroup(inCollisionGroup);
  851. }
  852. const CollisionGroup &BodyInterface::GetCollisionGroup(const BodyID &inBodyID) const
  853. {
  854. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  855. if (lock.Succeeded())
  856. return lock.GetBody().GetCollisionGroup();
  857. else
  858. return CollisionGroup::sInvalid;
  859. }
  860. TransformedShape BodyInterface::GetTransformedShape(const BodyID &inBodyID) const
  861. {
  862. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  863. if (lock.Succeeded())
  864. return lock.GetBody().GetTransformedShape();
  865. else
  866. return TransformedShape();
  867. }
  868. uint64 BodyInterface::GetUserData(const BodyID &inBodyID) const
  869. {
  870. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  871. if (lock.Succeeded())
  872. return lock.GetBody().GetUserData();
  873. else
  874. return 0;
  875. }
  876. void BodyInterface::SetUserData(const BodyID &inBodyID, uint64 inUserData) const
  877. {
  878. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  879. if (lock.Succeeded())
  880. lock.GetBody().SetUserData(inUserData);
  881. }
  882. const PhysicsMaterial *BodyInterface::GetMaterial(const BodyID &inBodyID, const SubShapeID &inSubShapeID) const
  883. {
  884. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  885. if (lock.Succeeded())
  886. return lock.GetBody().GetShape()->GetMaterial(inSubShapeID);
  887. else
  888. return PhysicsMaterial::sDefault;
  889. }
  890. void BodyInterface::InvalidateContactCache(const BodyID &inBodyID)
  891. {
  892. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  893. if (lock.SucceededAndIsInBroadPhase())
  894. mBodyManager->InvalidateContactCacheForBody(lock.GetBody());
  895. }
  896. JPH_NAMESPACE_END