BodyInterface.cpp 26 KB

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