BodyInterface.cpp 22 KB

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