BodyInterface.cpp 19 KB

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