BodyInterface.cpp 27 KB

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