BodyInterface.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. 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. 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. 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. 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. Body &body = lock.GetBody();
  114. if (body.IsActive())
  115. mBodyManager->DeactivateBodies(&inBodyID, 1);
  116. }
  117. }
  118. bool BodyInterface::IsActive(const BodyID &inBodyID) const
  119. {
  120. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  121. return lock.Succeeded() && lock.GetBody().IsActive();
  122. }
  123. void BodyInterface::ActivateConstraint(const TwoBodyConstraint *inConstraint)
  124. {
  125. BodyID bodies[] = { inConstraint->GetBody1()->GetID(), inConstraint->GetBody2()->GetID() };
  126. ActivateBodies(bodies, 2);
  127. }
  128. RefConst<Shape> BodyInterface::GetShape(const BodyID &inBodyID) const
  129. {
  130. RefConst<Shape> shape;
  131. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  132. if (lock.Succeeded())
  133. shape = lock.GetBody().GetShape();
  134. return shape;
  135. }
  136. void BodyInterface::SetShape(const BodyID &inBodyID, const Shape *inShape, bool inUpdateMassProperties, EActivation inActivationMode) const
  137. {
  138. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  139. if (lock.Succeeded())
  140. {
  141. Body &body = lock.GetBody();
  142. // Check if shape actually changed
  143. if (body.GetShape() != inShape)
  144. {
  145. // Update the shape
  146. body.SetShapeInternal(inShape, inUpdateMassProperties);
  147. // Flag collision cache invalid for this body
  148. mBodyManager->InvalidateContactCacheForBody(body);
  149. // Notify broadphase of change
  150. if (body.IsInBroadPhase())
  151. {
  152. BodyID id = body.GetID();
  153. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  154. }
  155. // Optionally activate body
  156. if (inActivationMode == EActivation::Activate && !body.IsStatic())
  157. mBodyManager->ActivateBodies(&inBodyID, 1);
  158. }
  159. }
  160. }
  161. void BodyInterface::NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inPreviousCenterOfMass, bool inUpdateMassProperties, EActivation inActivationMode) const
  162. {
  163. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  164. if (lock.Succeeded())
  165. {
  166. Body &body = lock.GetBody();
  167. // Update center of mass, mass and inertia
  168. body.UpdateCenterOfMassInternal(inPreviousCenterOfMass, inUpdateMassProperties);
  169. // Recalculate bounding box
  170. body.CalculateWorldSpaceBoundsInternal();
  171. // Flag collision cache invalid for this body
  172. mBodyManager->InvalidateContactCacheForBody(body);
  173. // Notify broadphase of change
  174. if (body.IsInBroadPhase())
  175. {
  176. BodyID id = body.GetID();
  177. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  178. }
  179. // Optionally activate body
  180. if (inActivationMode == EActivation::Activate && !body.IsStatic())
  181. mBodyManager->ActivateBodies(&inBodyID, 1);
  182. }
  183. }
  184. void BodyInterface::SetObjectLayer(const BodyID &inBodyID, ObjectLayer inLayer)
  185. {
  186. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  187. if (lock.Succeeded())
  188. {
  189. Body &body = lock.GetBody();
  190. // Check if layer actually changed, updating the broadphase is rather expensive
  191. if (body.GetObjectLayer() != inLayer)
  192. {
  193. // Update the layer on the body
  194. mBodyManager->SetBodyObjectLayerInternal(body, inLayer);
  195. // Notify broadphase of change
  196. if (body.IsInBroadPhase())
  197. {
  198. BodyID id = body.GetID();
  199. mBroadPhase->NotifyBodiesLayerChanged(&id, 1);
  200. }
  201. }
  202. }
  203. }
  204. ObjectLayer BodyInterface::GetObjectLayer(const BodyID &inBodyID) const
  205. {
  206. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  207. if (lock.Succeeded())
  208. return lock.GetBody().GetObjectLayer();
  209. else
  210. return cObjectLayerInvalid;
  211. }
  212. void BodyInterface::SetPositionAndRotation(const BodyID &inBodyID, Vec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode)
  213. {
  214. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  215. if (lock.Succeeded())
  216. {
  217. Body &body = lock.GetBody();
  218. // Update the position
  219. body.SetPositionAndRotationInternal(inPosition, inRotation);
  220. // Notify broadphase of change
  221. if (body.IsInBroadPhase())
  222. {
  223. BodyID id = body.GetID();
  224. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  225. }
  226. // Optionally activate body
  227. if (inActivationMode == EActivation::Activate && !body.IsStatic())
  228. mBodyManager->ActivateBodies(&inBodyID, 1);
  229. }
  230. }
  231. void BodyInterface::SetPositionAndRotationWhenChanged(const BodyID &inBodyID, Vec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode)
  232. {
  233. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  234. if (lock.Succeeded())
  235. {
  236. Body &body = lock.GetBody();
  237. // Check if there is enough change
  238. if (!body.GetPosition().IsClose(inPosition)
  239. || !body.GetRotation().IsClose(inRotation))
  240. {
  241. // Update the position
  242. body.SetPositionAndRotationInternal(inPosition, inRotation);
  243. // Notify broadphase of change
  244. if (body.IsInBroadPhase())
  245. {
  246. BodyID id = body.GetID();
  247. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  248. }
  249. // Optionally activate body
  250. if (inActivationMode == EActivation::Activate && !body.IsStatic())
  251. mBodyManager->ActivateBodies(&inBodyID, 1);
  252. }
  253. }
  254. }
  255. void BodyInterface::GetPositionAndRotation(const BodyID &inBodyID, Vec3 &outPosition, Quat &outRotation) const
  256. {
  257. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  258. if (lock.Succeeded())
  259. {
  260. const Body &body = lock.GetBody();
  261. outPosition = body.GetPosition();
  262. outRotation = body.GetRotation();
  263. }
  264. else
  265. {
  266. outPosition = Vec3::sZero();
  267. outRotation = Quat::sIdentity();
  268. }
  269. }
  270. void BodyInterface::SetPosition(const BodyID &inBodyID, Vec3Arg inPosition, EActivation inActivationMode)
  271. {
  272. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  273. if (lock.Succeeded())
  274. {
  275. Body &body = lock.GetBody();
  276. // Update the position
  277. body.SetPositionAndRotationInternal(inPosition, body.GetRotation());
  278. // Notify broadphase of change
  279. if (body.IsInBroadPhase())
  280. {
  281. BodyID id = body.GetID();
  282. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  283. }
  284. // Optionally activate body
  285. if (inActivationMode == EActivation::Activate && !body.IsStatic())
  286. mBodyManager->ActivateBodies(&inBodyID, 1);
  287. }
  288. }
  289. Vec3 BodyInterface::GetPosition(const BodyID &inBodyID) const
  290. {
  291. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  292. if (lock.Succeeded())
  293. return lock.GetBody().GetPosition();
  294. else
  295. return Vec3::sZero();
  296. }
  297. Vec3 BodyInterface::GetCenterOfMassPosition(const BodyID &inBodyID) const
  298. {
  299. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  300. if (lock.Succeeded())
  301. return lock.GetBody().GetCenterOfMassPosition();
  302. else
  303. return Vec3::sZero();
  304. }
  305. void BodyInterface::SetRotation(const BodyID &inBodyID, QuatArg inRotation, EActivation inActivationMode)
  306. {
  307. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  308. if (lock.Succeeded())
  309. {
  310. Body &body = lock.GetBody();
  311. // Update the position
  312. body.SetPositionAndRotationInternal(body.GetPosition(), inRotation);
  313. // Notify broadphase of change
  314. if (body.IsInBroadPhase())
  315. {
  316. BodyID id = body.GetID();
  317. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  318. }
  319. // Optionally activate body
  320. if (inActivationMode == EActivation::Activate && !body.IsStatic())
  321. mBodyManager->ActivateBodies(&inBodyID, 1);
  322. }
  323. }
  324. Quat BodyInterface::GetRotation(const BodyID &inBodyID) const
  325. {
  326. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  327. if (lock.Succeeded())
  328. return lock.GetBody().GetRotation();
  329. else
  330. return Quat::sIdentity();
  331. }
  332. Mat44 BodyInterface::GetWorldTransform(const BodyID &inBodyID) const
  333. {
  334. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  335. if (lock.Succeeded())
  336. return lock.GetBody().GetWorldTransform();
  337. else
  338. return Mat44::sIdentity();
  339. }
  340. Mat44 BodyInterface::GetCenterOfMassTransform(const BodyID &inBodyID) const
  341. {
  342. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  343. if (lock.Succeeded())
  344. return lock.GetBody().GetCenterOfMassTransform();
  345. else
  346. return Mat44::sIdentity();
  347. }
  348. void BodyInterface::MoveKinematic(const BodyID &inBodyID, Vec3Arg inTargetPosition, QuatArg inTargetRotation, float inDeltaTime)
  349. {
  350. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  351. if (lock.Succeeded())
  352. {
  353. Body &body = lock.GetBody();
  354. body.MoveKinematic(inTargetPosition, inTargetRotation, inDeltaTime);
  355. if (!body.IsActive() && (!body.GetLinearVelocity().IsNearZero() || !body.GetAngularVelocity().IsNearZero()))
  356. mBodyManager->ActivateBodies(&inBodyID, 1);
  357. }
  358. }
  359. void BodyInterface::SetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity)
  360. {
  361. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  362. if (lock.Succeeded())
  363. {
  364. Body &body = lock.GetBody();
  365. if (!body.IsStatic())
  366. {
  367. body.SetLinearVelocityClamped(inLinearVelocity);
  368. body.SetAngularVelocityClamped(inAngularVelocity);
  369. if (!body.IsActive() && (!inLinearVelocity.IsNearZero() || !inAngularVelocity.IsNearZero()))
  370. mBodyManager->ActivateBodies(&inBodyID, 1);
  371. }
  372. }
  373. }
  374. void BodyInterface::GetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3 &outLinearVelocity, Vec3 &outAngularVelocity) const
  375. {
  376. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  377. if (lock.Succeeded())
  378. {
  379. const Body &body = lock.GetBody();
  380. if (!body.IsStatic())
  381. {
  382. outLinearVelocity = body.GetLinearVelocity();
  383. outAngularVelocity = body.GetAngularVelocity();
  384. return;
  385. }
  386. }
  387. outLinearVelocity = outAngularVelocity = Vec3::sZero();
  388. }
  389. void BodyInterface::SetLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity)
  390. {
  391. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  392. if (lock.Succeeded())
  393. {
  394. Body &body = lock.GetBody();
  395. if (!body.IsStatic())
  396. {
  397. body.SetLinearVelocityClamped(inLinearVelocity);
  398. if (!body.IsActive() && !inLinearVelocity.IsNearZero())
  399. mBodyManager->ActivateBodies(&inBodyID, 1);
  400. }
  401. }
  402. }
  403. Vec3 BodyInterface::GetLinearVelocity(const BodyID &inBodyID) const
  404. {
  405. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  406. if (lock.Succeeded())
  407. {
  408. const Body &body = lock.GetBody();
  409. if (!body.IsStatic())
  410. return body.GetLinearVelocity();
  411. }
  412. return Vec3::sZero();
  413. }
  414. void BodyInterface::AddLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity)
  415. {
  416. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  417. if (lock.Succeeded())
  418. {
  419. Body &body = lock.GetBody();
  420. if (!body.IsStatic())
  421. {
  422. body.SetLinearVelocityClamped(body.GetLinearVelocity() + inLinearVelocity);
  423. if (!body.IsActive() && !body.GetLinearVelocity().IsNearZero())
  424. mBodyManager->ActivateBodies(&inBodyID, 1);
  425. }
  426. }
  427. }
  428. void BodyInterface::AddLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity)
  429. {
  430. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  431. if (lock.Succeeded())
  432. {
  433. Body &body = lock.GetBody();
  434. if (!body.IsStatic())
  435. {
  436. body.SetLinearVelocityClamped(body.GetLinearVelocity() + inLinearVelocity);
  437. body.SetAngularVelocityClamped(body.GetAngularVelocity() + inAngularVelocity);
  438. if (!body.IsActive() && (!body.GetLinearVelocity().IsNearZero() || !body.GetAngularVelocity().IsNearZero()))
  439. mBodyManager->ActivateBodies(&inBodyID, 1);
  440. }
  441. }
  442. }
  443. void BodyInterface::SetAngularVelocity(const BodyID &inBodyID, 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.SetAngularVelocityClamped(inAngularVelocity);
  452. if (!body.IsActive() && !inAngularVelocity.IsNearZero())
  453. mBodyManager->ActivateBodies(&inBodyID, 1);
  454. }
  455. }
  456. }
  457. Vec3 BodyInterface::GetAngularVelocity(const BodyID &inBodyID) const
  458. {
  459. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  460. if (lock.Succeeded())
  461. {
  462. const Body &body = lock.GetBody();
  463. if (!body.IsStatic())
  464. return body.GetAngularVelocity();
  465. }
  466. return Vec3::sZero();
  467. }
  468. Vec3 BodyInterface::GetPointVelocity(const BodyID &inBodyID, Vec3Arg inPoint) const
  469. {
  470. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  471. if (lock.Succeeded())
  472. {
  473. const Body &body = lock.GetBody();
  474. if (!body.IsStatic())
  475. return body.GetPointVelocity(inPoint);
  476. }
  477. return Vec3::sZero();
  478. }
  479. void BodyInterface::AddForce(const BodyID &inBodyID, Vec3Arg inForce)
  480. {
  481. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  482. if (lock.Succeeded())
  483. {
  484. Body &body = lock.GetBody();
  485. if (body.IsDynamic())
  486. {
  487. body.AddForce(inForce);
  488. if (!body.IsActive())
  489. mBodyManager->ActivateBodies(&inBodyID, 1);
  490. }
  491. }
  492. }
  493. void BodyInterface::AddForce(const BodyID &inBodyID, Vec3Arg inForce, Vec3Arg inPoint)
  494. {
  495. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  496. if (lock.Succeeded())
  497. {
  498. Body &body = lock.GetBody();
  499. if (body.IsDynamic())
  500. {
  501. body.AddForce(inForce, inPoint);
  502. if (!body.IsActive())
  503. mBodyManager->ActivateBodies(&inBodyID, 1);
  504. }
  505. }
  506. }
  507. void BodyInterface::AddTorque(const BodyID &inBodyID, Vec3Arg inTorque)
  508. {
  509. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  510. if (lock.Succeeded())
  511. {
  512. Body &body = lock.GetBody();
  513. if (body.IsDynamic())
  514. {
  515. body.AddTorque(inTorque);
  516. if (!body.IsActive())
  517. mBodyManager->ActivateBodies(&inBodyID, 1);
  518. }
  519. }
  520. }
  521. void BodyInterface::AddForceAndTorque(const BodyID &inBodyID, Vec3Arg inForce, Vec3Arg inTorque)
  522. {
  523. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  524. if (lock.Succeeded())
  525. {
  526. Body &body = lock.GetBody();
  527. if (body.IsDynamic())
  528. {
  529. body.AddForce(inForce);
  530. body.AddTorque(inTorque);
  531. if (!body.IsActive())
  532. mBodyManager->ActivateBodies(&inBodyID, 1);
  533. }
  534. }
  535. }
  536. void BodyInterface::AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse)
  537. {
  538. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  539. if (lock.Succeeded())
  540. {
  541. Body &body = lock.GetBody();
  542. if (body.IsDynamic())
  543. {
  544. body.AddImpulse(inImpulse);
  545. if (!body.IsActive())
  546. mBodyManager->ActivateBodies(&inBodyID, 1);
  547. }
  548. }
  549. }
  550. void BodyInterface::AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse, Vec3Arg inPoint)
  551. {
  552. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  553. if (lock.Succeeded())
  554. {
  555. Body &body = lock.GetBody();
  556. if (body.IsDynamic())
  557. {
  558. body.AddImpulse(inImpulse, inPoint);
  559. if (!body.IsActive())
  560. mBodyManager->ActivateBodies(&inBodyID, 1);
  561. }
  562. }
  563. }
  564. void BodyInterface::AddAngularImpulse(const BodyID &inBodyID, Vec3Arg inAngularImpulse)
  565. {
  566. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  567. if (lock.Succeeded())
  568. {
  569. Body &body = lock.GetBody();
  570. if (body.IsDynamic())
  571. {
  572. body.AddAngularImpulse(inAngularImpulse);
  573. if (!body.IsActive())
  574. mBodyManager->ActivateBodies(&inBodyID, 1);
  575. }
  576. }
  577. }
  578. void BodyInterface::SetPositionRotationAndVelocity(const BodyID &inBodyID, Vec3Arg inPosition, QuatArg inRotation, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity)
  579. {
  580. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  581. if (lock.Succeeded())
  582. {
  583. Body &body = lock.GetBody();
  584. // Update the position
  585. body.SetPositionAndRotationInternal(inPosition, inRotation);
  586. // Notify broadphase of change
  587. if (body.IsInBroadPhase())
  588. {
  589. BodyID id = body.GetID();
  590. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  591. }
  592. if (!body.IsStatic())
  593. {
  594. body.SetLinearVelocityClamped(inLinearVelocity);
  595. body.SetAngularVelocityClamped(inAngularVelocity);
  596. // Optionally activate body
  597. if (!body.IsActive() && (!inLinearVelocity.IsNearZero() || !inAngularVelocity.IsNearZero()))
  598. mBodyManager->ActivateBodies(&inBodyID, 1);
  599. }
  600. }
  601. }
  602. void BodyInterface::SetMotionType(const BodyID &inBodyID, EMotionType inMotionType, EActivation inActivationMode)
  603. {
  604. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  605. if (lock.Succeeded())
  606. {
  607. Body &body = lock.GetBody();
  608. // Deactivate if we're making the body static
  609. if (body.IsActive() && inMotionType == EMotionType::Static)
  610. mBodyManager->DeactivateBodies(&inBodyID, 1);
  611. body.SetMotionType(inMotionType);
  612. // Activate body if requested
  613. if (inMotionType != EMotionType::Static && inActivationMode == EActivation::Activate && !body.IsActive())
  614. mBodyManager->ActivateBodies(&inBodyID, 1);
  615. }
  616. }
  617. Mat44 BodyInterface::GetInverseInertia(const BodyID &inBodyID) const
  618. {
  619. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  620. if (lock.Succeeded())
  621. return lock.GetBody().GetInverseInertia();
  622. else
  623. return Mat44::sIdentity();
  624. }
  625. void BodyInterface::SetRestitution(const BodyID &inBodyID, float inRestitution)
  626. {
  627. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  628. if (lock.Succeeded())
  629. lock.GetBody().SetRestitution(inRestitution);
  630. }
  631. float BodyInterface::GetRestitution(const BodyID &inBodyID) const
  632. {
  633. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  634. if (lock.Succeeded())
  635. return lock.GetBody().GetRestitution();
  636. else
  637. return 0.0f;
  638. }
  639. void BodyInterface::SetFriction(const BodyID &inBodyID, float inFriction)
  640. {
  641. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  642. if (lock.Succeeded())
  643. lock.GetBody().SetFriction(inFriction);
  644. }
  645. float BodyInterface::GetFriction(const BodyID &inBodyID) const
  646. {
  647. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  648. if (lock.Succeeded())
  649. return lock.GetBody().GetFriction();
  650. else
  651. return 0.0f;
  652. }
  653. void BodyInterface::SetGravityFactor(const BodyID &inBodyID, float inGravityFactor)
  654. {
  655. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  656. if (lock.Succeeded() && lock.GetBody().GetMotionPropertiesUnchecked() != nullptr)
  657. lock.GetBody().GetMotionPropertiesUnchecked()->SetGravityFactor(inGravityFactor);
  658. }
  659. float BodyInterface::GetGravityFactor(const BodyID &inBodyID) const
  660. {
  661. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  662. if (lock.Succeeded() && lock.GetBody().GetMotionPropertiesUnchecked() != nullptr)
  663. return lock.GetBody().GetMotionPropertiesUnchecked()->GetGravityFactor();
  664. else
  665. return 1.0f;
  666. }
  667. TransformedShape BodyInterface::GetTransformedShape(const BodyID &inBodyID) const
  668. {
  669. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  670. if (lock.Succeeded())
  671. return lock.GetBody().GetTransformedShape();
  672. else
  673. return TransformedShape();
  674. }
  675. uint64 BodyInterface::GetUserData(const BodyID &inBodyID) const
  676. {
  677. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  678. if (lock.Succeeded())
  679. return lock.GetBody().GetUserData();
  680. else
  681. return 0;
  682. }
  683. const PhysicsMaterial *BodyInterface::GetMaterial(const BodyID &inBodyID, const SubShapeID &inSubShapeID) const
  684. {
  685. BodyLockRead lock(*mBodyLockInterface, inBodyID);
  686. if (lock.Succeeded())
  687. return lock.GetBody().GetShape()->GetMaterial(inSubShapeID);
  688. else
  689. return PhysicsMaterial::sDefault;
  690. }
  691. void BodyInterface::InvalidateContactCache(const BodyID &inBodyID)
  692. {
  693. BodyLockWrite lock(*mBodyLockInterface, inBodyID);
  694. if (lock.Succeeded())
  695. mBodyManager->InvalidateContactCacheForBody(lock.GetBody());
  696. }
  697. JPH_NAMESPACE_END