BodyInterface.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. void DestroyBody(const BodyID &inBodyID);
  71. /// Destroy multiple bodies
  72. void DestroyBodies(const BodyID *inBodyIDs, int inNumber);
  73. /// Add body to the physics system.
  74. /// Note that if you need to add multiple bodies, use the AddBodiesPrepare/AddBodiesFinalize function.
  75. /// 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!
  76. /// After adding, to get a body by ID use the BodyLockRead or BodyLockWrite interface!
  77. void AddBody(const BodyID &inBodyID, EActivation inActivationMode);
  78. /// Remove body from the physics system.
  79. void RemoveBody(const BodyID &inBodyID);
  80. /// Check if a body has been added to the physics system.
  81. bool IsAdded(const BodyID &inBodyID) const;
  82. /// Combines CreateBody and AddBody
  83. /// @return Created body ID or an invalid ID when out of bodies
  84. BodyID CreateAndAddBody(const BodyCreationSettings &inSettings, EActivation inActivationMode);
  85. /// Combines CreateSoftBody and AddBody
  86. /// @return Created body ID or an invalid ID when out of bodies
  87. BodyID CreateAndAddSoftBody(const SoftBodyCreationSettings &inSettings, EActivation inActivationMode);
  88. /// Broadphase add state handle, used to keep track of a batch while ading to the broadphase.
  89. using AddState = void *;
  90. ///@name Batch adding interface, see Broadphase for further documentation.
  91. /// Note that ioBodies array must be kept constant while the add is in progress.
  92. ///@{
  93. AddState AddBodiesPrepare(BodyID *ioBodies, int inNumber);
  94. void AddBodiesFinalize(BodyID *ioBodies, int inNumber, AddState inAddState, EActivation inActivationMode);
  95. void AddBodiesAbort(BodyID *ioBodies, int inNumber, AddState inAddState);
  96. void RemoveBodies(BodyID *ioBodies, int inNumber);
  97. ///@}
  98. ///@name Activate / deactivate a body
  99. ///@{
  100. void ActivateBody(const BodyID &inBodyID);
  101. void ActivateBodies(const BodyID *inBodyIDs, int inNumber);
  102. void ActivateBodiesInAABox(const AABox &inBox, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter);
  103. void DeactivateBody(const BodyID &inBodyID);
  104. void DeactivateBodies(const BodyID *inBodyIDs, int inNumber);
  105. bool IsActive(const BodyID &inBodyID) const;
  106. ///@}
  107. /// Create a two body constraint
  108. TwoBodyConstraint * CreateConstraint(const TwoBodyConstraintSettings *inSettings, const BodyID &inBodyID1, const BodyID &inBodyID2);
  109. /// Activate non-static bodies attached to a constraint
  110. void ActivateConstraint(const TwoBodyConstraint *inConstraint);
  111. ///@name Access to the shape of a body
  112. ///@{
  113. /// Get the current shape
  114. RefConst<Shape> GetShape(const BodyID &inBodyID) const;
  115. /// Set a new shape on the body
  116. /// @param inBodyID Body ID of body that had its shape changed
  117. /// @param inShape The new shape
  118. /// @param inUpdateMassProperties When true, the mass and inertia tensor is recalculated
  119. /// @param inActivationMode Weather or not to activate the body
  120. void SetShape(const BodyID &inBodyID, const Shape *inShape, bool inUpdateMassProperties, EActivation inActivationMode) const;
  121. /// Notify all systems to indicate that a shape has changed (usable for MutableCompoundShapes)
  122. /// @param inBodyID Body ID of body that had its shape changed
  123. /// @param inPreviousCenterOfMass Center of mass of the shape before the alterations
  124. /// @param inUpdateMassProperties When true, the mass and inertia tensor is recalculated
  125. /// @param inActivationMode Weather or not to activate the body
  126. void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inPreviousCenterOfMass, bool inUpdateMassProperties, EActivation inActivationMode) const;
  127. ///@}
  128. ///@name Object layer of a body
  129. ///@{
  130. void SetObjectLayer(const BodyID &inBodyID, ObjectLayer inLayer);
  131. ObjectLayer GetObjectLayer(const BodyID &inBodyID) const;
  132. ///@}
  133. ///@name Position and rotation of a body
  134. ///@{
  135. void SetPositionAndRotation(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode);
  136. 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.
  137. void GetPositionAndRotation(const BodyID &inBodyID, RVec3 &outPosition, Quat &outRotation) const;
  138. void SetPosition(const BodyID &inBodyID, RVec3Arg inPosition, EActivation inActivationMode);
  139. RVec3 GetPosition(const BodyID &inBodyID) const;
  140. RVec3 GetCenterOfMassPosition(const BodyID &inBodyID) const;
  141. void SetRotation(const BodyID &inBodyID, QuatArg inRotation, EActivation inActivationMode);
  142. Quat GetRotation(const BodyID &inBodyID) const;
  143. RMat44 GetWorldTransform(const BodyID &inBodyID) const;
  144. RMat44 GetCenterOfMassTransform(const BodyID &inBodyID) const;
  145. ///@}
  146. /// Set velocity of body such that it will be positioned at inTargetPosition/Rotation in inDeltaTime seconds (will activate body if needed)
  147. void MoveKinematic(const BodyID &inBodyID, RVec3Arg inTargetPosition, QuatArg inTargetRotation, float inDeltaTime);
  148. /// Linear or angular velocity (functions will activate body if needed).
  149. /// 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$
  150. void SetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity);
  151. void GetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3 &outLinearVelocity, Vec3 &outAngularVelocity) const;
  152. void SetLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity);
  153. Vec3 GetLinearVelocity(const BodyID &inBodyID) const;
  154. void AddLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity); ///< Add velocity to current velocity
  155. void AddLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity); ///< Add linear and angular to current velocities
  156. void SetAngularVelocity(const BodyID &inBodyID, Vec3Arg inAngularVelocity);
  157. Vec3 GetAngularVelocity(const BodyID &inBodyID) const;
  158. 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
  159. /// Set the complete motion state of a body.
  160. /// 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$
  161. void SetPositionRotationAndVelocity(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity);
  162. ///@name Add forces to the body
  163. ///@{
  164. void AddForce(const BodyID &inBodyID, Vec3Arg inForce); ///< See Body::AddForce
  165. void AddForce(const BodyID &inBodyID, Vec3Arg inForce, RVec3Arg inPoint); ///< Applied at inPoint
  166. void AddTorque(const BodyID &inBodyID, Vec3Arg inTorque); ///< See Body::AddTorque
  167. void AddForceAndTorque(const BodyID &inBodyID, Vec3Arg inForce, Vec3Arg inTorque); ///< A combination of Body::AddForce and Body::AddTorque
  168. ///@}
  169. ///@name Add an impulse to the body
  170. ///@{
  171. void AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse); ///< Applied at center of mass
  172. void AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse, RVec3Arg inPoint); ///< Applied at inPoint
  173. void AddAngularImpulse(const BodyID &inBodyID, Vec3Arg inAngularImpulse);
  174. ///@}
  175. ///@name Body type
  176. ///@{
  177. EBodyType GetBodyType(const BodyID &inBodyID) const;
  178. ///@}
  179. ///@name Body motion type
  180. ///@{
  181. void SetMotionType(const BodyID &inBodyID, EMotionType inMotionType, EActivation inActivationMode);
  182. EMotionType GetMotionType(const BodyID &inBodyID) const;
  183. ///@}
  184. ///@name Body motion quality
  185. ///@{
  186. void SetMotionQuality(const BodyID &inBodyID, EMotionQuality inMotionQuality);
  187. EMotionQuality GetMotionQuality(const BodyID &inBodyID) const;
  188. ///@}
  189. /// Get inverse inertia tensor in world space
  190. Mat44 GetInverseInertia(const BodyID &inBodyID) const;
  191. ///@name Restitution
  192. ///@{
  193. void SetRestitution(const BodyID &inBodyID, float inRestitution);
  194. float GetRestitution(const BodyID &inBodyID) const;
  195. ///@}
  196. ///@name Friction
  197. ///@{
  198. void SetFriction(const BodyID &inBodyID, float inFriction);
  199. float GetFriction(const BodyID &inBodyID) const;
  200. ///@}
  201. ///@name Gravity factor
  202. ///@{
  203. void SetGravityFactor(const BodyID &inBodyID, float inGravityFactor);
  204. float GetGravityFactor(const BodyID &inBodyID) const;
  205. ///@}
  206. /// Get transform and shape for this body, used to perform collision detection
  207. TransformedShape GetTransformedShape(const BodyID &inBodyID) const;
  208. /// Get the user data for a body
  209. uint64 GetUserData(const BodyID &inBodyID) const;
  210. void SetUserData(const BodyID &inBodyID, uint64 inUserData) const;
  211. /// Get the material for a particular sub shape
  212. const PhysicsMaterial * GetMaterial(const BodyID &inBodyID, const SubShapeID &inSubShapeID) const;
  213. /// 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.
  214. void InvalidateContactCache(const BodyID &inBodyID);
  215. private:
  216. BodyLockInterface * mBodyLockInterface = nullptr;
  217. BodyManager * mBodyManager = nullptr;
  218. BroadPhase * mBroadPhase = nullptr;
  219. };
  220. JPH_NAMESPACE_END