BodyInterface.cpp 24 KB

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