BodyInterface.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Body/BodyID.h>
  6. #include <Jolt/Physics/EActivation.h>
  7. #include <Jolt/Physics/Collision/ObjectLayer.h>
  8. #include <Jolt/Physics/Body/MotionType.h>
  9. #include <Jolt/Physics/Body/MotionQuality.h>
  10. #include <Jolt/Physics/Body/BodyType.h>
  11. #include <Jolt/Core/Reference.h>
  12. JPH_NAMESPACE_BEGIN
  13. class Body;
  14. class BodyCreationSettings;
  15. class SoftBodyCreationSettings;
  16. class BodyLockInterface;
  17. class BroadPhase;
  18. class BodyManager;
  19. class TransformedShape;
  20. class PhysicsMaterial;
  21. class SubShapeID;
  22. class Shape;
  23. class TwoBodyConstraintSettings;
  24. class TwoBodyConstraint;
  25. class BroadPhaseLayerFilter;
  26. class AABox;
  27. /// Class that provides operations on bodies using a body ID. Note that if you need to do multiple operations on a single body, it is more efficient to lock the body once and combine the operations.
  28. /// All quantities are in world space unless otherwise specified.
  29. class JPH_EXPORT BodyInterface : public NonCopyable
  30. {
  31. public:
  32. /// Initialize the interface (should only be called by PhysicsSystem)
  33. void Init(BodyLockInterface &inBodyLockInterface, BodyManager &inBodyManager, BroadPhase &inBroadPhase) { mBodyLockInterface = &inBodyLockInterface; mBodyManager = &inBodyManager; mBroadPhase = &inBroadPhase; }
  34. /// Create a rigid body
  35. /// @return Created body or null when out of bodies
  36. Body * CreateBody(const BodyCreationSettings &inSettings);
  37. /// Create a soft body
  38. /// @return Created body or null when out of bodies
  39. Body * CreateSoftBody(const SoftBodyCreationSettings &inSettings);
  40. /// Create a rigid body with specified ID. This function can be used if a simulation is to run in sync between clients or if a simulation needs to be restored exactly.
  41. /// The ID created on the server can be replicated to the client and used to create a deterministic simulation.
  42. /// @return Created body or null when the body ID is invalid or a body of the same ID already exists.
  43. Body * CreateBodyWithID(const BodyID &inBodyID, const BodyCreationSettings &inSettings);
  44. /// Create a soft body with specified ID. See comments at CreateBodyWithID.
  45. Body * CreateSoftBodyWithID(const BodyID &inBodyID, const SoftBodyCreationSettings &inSettings);
  46. /// Advanced use only. Creates a rigid body without specifying an ID. This body cannot be added to the physics system until it has been assigned a body ID.
  47. /// This can be used to decouple allocation from registering the body. A call to CreateBodyWithoutID followed by AssignBodyID is equivalent to calling CreateBodyWithID.
  48. /// @return Created body
  49. Body * CreateBodyWithoutID(const BodyCreationSettings &inSettings) const;
  50. /// Advanced use only. Creates a body without specifying an ID. See comments at CreateBodyWithoutID.
  51. Body * CreateSoftBodyWithoutID(const SoftBodyCreationSettings &inSettings) const;
  52. /// Advanced use only. Destroy a body previously created with CreateBodyWithoutID that hasn't gotten an ID yet through the AssignBodyID function,
  53. /// or a body that has had its body ID unassigned through UnassignBodyIDs. Bodies that have an ID should be destroyed through DestroyBody.
  54. void DestroyBodyWithoutID(Body *inBody) const;
  55. /// Advanced use only. Assigns the next available body ID to a body that was created using CreateBodyWithoutID. After this call, the body can be added to the physics system.
  56. /// @return false if the body already has an ID or out of body ids.
  57. bool AssignBodyID(Body *ioBody);
  58. /// Advanced use only. Assigns a body ID to a body that was created using CreateBodyWithoutID. After this call, the body can be added to the physics system.
  59. /// @return false if the body already has an ID or if the ID is not valid.
  60. bool AssignBodyID(Body *ioBody, const BodyID &inBodyID);
  61. /// Advanced use only. See UnassignBodyIDs. Unassigns the ID of a single body.
  62. Body * UnassignBodyID(const BodyID &inBodyID);
  63. /// Advanced use only. Removes a number of body IDs from their bodies and returns the body pointers. Before calling this, the body should have been removed from the physics system.
  64. /// The body can be destroyed through DestroyBodyWithoutID. This can be used to decouple deallocation. A call to UnassignBodyIDs followed by calls to DestroyBodyWithoutID is equivalent to calling DestroyBodies.
  65. /// @param inBodyIDs A list of body IDs
  66. /// @param inNumber Number of bodies in the list
  67. /// @param outBodies If not null on input, this will contain a list of body pointers corresponding to inBodyIDs that can be destroyed afterwards (caller assumes ownership over these).
  68. void UnassignBodyIDs(const BodyID *inBodyIDs, int inNumber, Body **outBodies);
  69. /// Destroy a body.
  70. /// Make sure that you remove the body from the physics system using BodyInterface::RemoveBody before calling this function.
  71. void DestroyBody(const BodyID &inBodyID);
  72. /// Destroy multiple bodies
  73. /// Make sure that you remove the bodies from the physics system using BodyInterface::RemoveBody before calling this function.
  74. void DestroyBodies(const BodyID *inBodyIDs, int inNumber);
  75. /// Add body to the physics system.
  76. /// Note that if you need to add multiple bodies, use the AddBodiesPrepare/AddBodiesFinalize function.
  77. /// Adding many bodies, one at a time, results in a really inefficient broadphase until PhysicsSystem::OptimizeBroadPhase is called or when PhysicsSystem::Update rebuilds the tree!
  78. /// After adding, to get a body by ID use the BodyLockRead or BodyLockWrite interface!
  79. void AddBody(const BodyID &inBodyID, EActivation inActivationMode);
  80. /// Remove body from the physics system.
  81. void RemoveBody(const BodyID &inBodyID);
  82. /// Check if a body has been added to the physics system.
  83. bool IsAdded(const BodyID &inBodyID) const;
  84. /// Combines CreateBody and AddBody
  85. /// @return Created body ID or an invalid ID when out of bodies
  86. BodyID CreateAndAddBody(const BodyCreationSettings &inSettings, EActivation inActivationMode);
  87. /// Combines CreateSoftBody and AddBody
  88. /// @return Created body ID or an invalid ID when out of bodies
  89. BodyID CreateAndAddSoftBody(const SoftBodyCreationSettings &inSettings, EActivation inActivationMode);
  90. /// Broadphase add state handle, used to keep track of a batch while adding to the broadphase.
  91. using AddState = void *;
  92. ///@name Batch adding interface, see Broadphase for further documentation.
  93. /// Note that ioBodies array must be kept constant while the add is in progress.
  94. ///@{
  95. AddState AddBodiesPrepare(BodyID *ioBodies, int inNumber);
  96. void AddBodiesFinalize(BodyID *ioBodies, int inNumber, AddState inAddState, EActivation inActivationMode);
  97. void AddBodiesAbort(BodyID *ioBodies, int inNumber, AddState inAddState);
  98. void RemoveBodies(BodyID *ioBodies, int inNumber);
  99. ///@}
  100. ///@name Activate / deactivate a body
  101. ///@{
  102. void ActivateBody(const BodyID &inBodyID);
  103. void ActivateBodies(const BodyID *inBodyIDs, int inNumber);
  104. void ActivateBodiesInAABox(const AABox &inBox, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter);
  105. void DeactivateBody(const BodyID &inBodyID);
  106. void DeactivateBodies(const BodyID *inBodyIDs, int inNumber);
  107. bool IsActive(const BodyID &inBodyID) const;
  108. void ResetSleepTimer(const BodyID &inBodyID);
  109. ///@}
  110. /// Create a two body constraint
  111. TwoBodyConstraint * CreateConstraint(const TwoBodyConstraintSettings *inSettings, const BodyID &inBodyID1, const BodyID &inBodyID2);
  112. /// Activate non-static bodies attached to a constraint
  113. void ActivateConstraint(const TwoBodyConstraint *inConstraint);
  114. ///@name Access to the shape of a body
  115. ///@{
  116. /// Get the current shape
  117. RefConst<Shape> GetShape(const BodyID &inBodyID) const;
  118. /// Set a new shape on the body
  119. /// @param inBodyID Body ID of body that had its shape changed
  120. /// @param inShape The new shape
  121. /// @param inUpdateMassProperties When true, the mass and inertia tensor is recalculated
  122. /// @param inActivationMode Weather or not to activate the body
  123. void SetShape(const BodyID &inBodyID, const Shape *inShape, bool inUpdateMassProperties, EActivation inActivationMode) const;
  124. /// Notify all systems to indicate that a shape has changed (usable for MutableCompoundShapes)
  125. /// @param inBodyID Body ID of body that had its shape changed
  126. /// @param inPreviousCenterOfMass Center of mass of the shape before the alterations
  127. /// @param inUpdateMassProperties When true, the mass and inertia tensor is recalculated
  128. /// @param inActivationMode Weather or not to activate the body
  129. void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inPreviousCenterOfMass, bool inUpdateMassProperties, EActivation inActivationMode) const;
  130. ///@}
  131. ///@name Object layer of a body
  132. ///@{
  133. void SetObjectLayer(const BodyID &inBodyID, ObjectLayer inLayer);
  134. ObjectLayer GetObjectLayer(const BodyID &inBodyID) const;
  135. ///@}
  136. ///@name Position and rotation of a body
  137. ///@{
  138. void SetPositionAndRotation(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode);
  139. void SetPositionAndRotationWhenChanged(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode); ///< Will only update the position/rotation and activate the body when the difference is larger than a very small number. This avoids updating the broadphase/waking up a body when the resulting position/orientation doesn't really change.
  140. void GetPositionAndRotation(const BodyID &inBodyID, RVec3 &outPosition, Quat &outRotation) const;
  141. void SetPosition(const BodyID &inBodyID, RVec3Arg inPosition, EActivation inActivationMode);
  142. RVec3 GetPosition(const BodyID &inBodyID) const;
  143. RVec3 GetCenterOfMassPosition(const BodyID &inBodyID) const;
  144. void SetRotation(const BodyID &inBodyID, QuatArg inRotation, EActivation inActivationMode);
  145. Quat GetRotation(const BodyID &inBodyID) const;
  146. RMat44 GetWorldTransform(const BodyID &inBodyID) const;
  147. RMat44 GetCenterOfMassTransform(const BodyID &inBodyID) const;
  148. ///@}
  149. /// Set velocity of body such that it will be positioned at inTargetPosition/Rotation in inDeltaTime seconds (will activate body if needed)
  150. void MoveKinematic(const BodyID &inBodyID, RVec3Arg inTargetPosition, QuatArg inTargetRotation, float inDeltaTime);
  151. /// Linear or angular velocity (functions will activate body if needed).
  152. /// Note that the linear velocity is the velocity of the center of mass, which may not coincide with the position of your object, to correct for this: \f$VelocityCOM = Velocity - AngularVelocity \times ShapeCOM\f$
  153. void SetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity);
  154. void GetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3 &outLinearVelocity, Vec3 &outAngularVelocity) const;
  155. void SetLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity);
  156. Vec3 GetLinearVelocity(const BodyID &inBodyID) const;
  157. void AddLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity); ///< Add velocity to current velocity
  158. void AddLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity); ///< Add linear and angular to current velocities
  159. void SetAngularVelocity(const BodyID &inBodyID, Vec3Arg inAngularVelocity);
  160. Vec3 GetAngularVelocity(const BodyID &inBodyID) const;
  161. Vec3 GetPointVelocity(const BodyID &inBodyID, RVec3Arg inPoint) const; ///< Velocity of point inPoint (in world space, e.g. on the surface of the body) of the body
  162. /// Set the complete motion state of a body.
  163. /// Note that the linear velocity is the velocity of the center of mass, which may not coincide with the position of your object, to correct for this: \f$VelocityCOM = Velocity - AngularVelocity \times ShapeCOM\f$
  164. void SetPositionRotationAndVelocity(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity);
  165. ///@name Add forces to the body
  166. ///@{
  167. void AddForce(const BodyID &inBodyID, Vec3Arg inForce, EActivation inActivationMode = EActivation::Activate); ///< See Body::AddForce
  168. void AddForce(const BodyID &inBodyID, Vec3Arg inForce, RVec3Arg inPoint, EActivation inActivationMode = EActivation::Activate); ///< Applied at inPoint
  169. void AddTorque(const BodyID &inBodyID, Vec3Arg inTorque, EActivation inActivationMode = EActivation::Activate); ///< See Body::AddTorque
  170. void AddForceAndTorque(const BodyID &inBodyID, Vec3Arg inForce, Vec3Arg inTorque, EActivation inActivationMode = EActivation::Activate); ///< A combination of Body::AddForce and Body::AddTorque
  171. ///@}
  172. ///@name Add an impulse to the body
  173. ///@{
  174. void AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse); ///< Applied at center of mass
  175. void AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse, RVec3Arg inPoint); ///< Applied at inPoint
  176. void AddAngularImpulse(const BodyID &inBodyID, Vec3Arg inAngularImpulse);
  177. bool ApplyBuoyancyImpulse(const BodyID &inBodyID, RVec3Arg inSurfacePosition, Vec3Arg inSurfaceNormal, float inBuoyancy, float inLinearDrag, float inAngularDrag, Vec3Arg inFluidVelocity, Vec3Arg inGravity, float inDeltaTime);
  178. ///@}
  179. ///@name Body type
  180. ///@{
  181. EBodyType GetBodyType(const BodyID &inBodyID) const;
  182. ///@}
  183. ///@name Body motion type
  184. ///@{
  185. void SetMotionType(const BodyID &inBodyID, EMotionType inMotionType, EActivation inActivationMode);
  186. EMotionType GetMotionType(const BodyID &inBodyID) const;
  187. ///@}
  188. ///@name Body motion quality
  189. ///@{
  190. void SetMotionQuality(const BodyID &inBodyID, EMotionQuality inMotionQuality);
  191. EMotionQuality GetMotionQuality(const BodyID &inBodyID) const;
  192. ///@}
  193. /// Get inverse inertia tensor in world space
  194. Mat44 GetInverseInertia(const BodyID &inBodyID) const;
  195. ///@name Restitution
  196. ///@{
  197. void SetRestitution(const BodyID &inBodyID, float inRestitution);
  198. float GetRestitution(const BodyID &inBodyID) const;
  199. ///@}
  200. ///@name Friction
  201. ///@{
  202. void SetFriction(const BodyID &inBodyID, float inFriction);
  203. float GetFriction(const BodyID &inBodyID) const;
  204. ///@}
  205. ///@name Gravity factor
  206. ///@{
  207. void SetGravityFactor(const BodyID &inBodyID, float inGravityFactor);
  208. float GetGravityFactor(const BodyID &inBodyID) const;
  209. ///@}
  210. ///@name Manifold reduction
  211. ///@{
  212. void SetUseManifoldReduction(const BodyID &inBodyID, bool inUseReduction);
  213. bool GetUseManifoldReduction(const BodyID &inBodyID) const;
  214. ///@}
  215. /// Get transform and shape for this body, used to perform collision detection
  216. TransformedShape GetTransformedShape(const BodyID &inBodyID) const;
  217. /// Get the user data for a body
  218. uint64 GetUserData(const BodyID &inBodyID) const;
  219. void SetUserData(const BodyID &inBodyID, uint64 inUserData) const;
  220. /// Get the material for a particular sub shape
  221. const PhysicsMaterial * GetMaterial(const BodyID &inBodyID, const SubShapeID &inSubShapeID) const;
  222. /// Set the Body::EFlags::InvalidateContactCache flag for the specified body. This means that the collision cache is invalid for any body pair involving that body until the next physics step.
  223. void InvalidateContactCache(const BodyID &inBodyID);
  224. private:
  225. /// Helper function to activate a single body
  226. JPH_INLINE void ActivateBodyInternal(Body &ioBody) const;
  227. BodyLockInterface * mBodyLockInterface = nullptr;
  228. BodyManager * mBodyManager = nullptr;
  229. BroadPhase * mBroadPhase = nullptr;
  230. };
  231. JPH_NAMESPACE_END