BodyInterface.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Body/BodyID.h>
  5. #include <Jolt/Physics/EActivation.h>
  6. #include <Jolt/Physics/Collision/ObjectLayer.h>
  7. #include <Jolt/Physics/Body/MotionType.h>
  8. #include <Jolt/Physics/Body/MotionQuality.h>
  9. #include <Jolt/Core/Reference.h>
  10. JPH_NAMESPACE_BEGIN
  11. class Body;
  12. class BodyCreationSettings;
  13. class BodyLockInterface;
  14. class BroadPhase;
  15. class BodyManager;
  16. class TransformedShape;
  17. class PhysicsMaterial;
  18. class SubShapeID;
  19. class Shape;
  20. class TwoBodyConstraintSettings;
  21. class TwoBodyConstraint;
  22. /// 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.
  23. /// All quantities are in world space unless otherwise specified.
  24. class BodyInterface : public NonCopyable
  25. {
  26. public:
  27. /// Initialize the interface (should only be called by PhysicsSystem)
  28. void Init(BodyLockInterface &inBodyLockInterface, BodyManager &inBodyManager, BroadPhase &inBroadPhase) { mBodyLockInterface = &inBodyLockInterface; mBodyManager = &inBodyManager; mBroadPhase = &inBroadPhase; }
  29. /// Create a body
  30. /// @return Created body or null when out of bodies
  31. Body * CreateBody(const BodyCreationSettings &inSettings);
  32. /// Create a 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.
  33. /// The ID created on the server can be replicated to the client and used to create a deterministic simulation.
  34. /// @return Created body or null when the body ID is invalid or a body of the same ID already exists.
  35. Body * CreateBodyWithID(const BodyID &inBodyID, const BodyCreationSettings &inSettings);
  36. /// Advanced use only. Creates a body without specifying an ID. This body cannot be added to the physics system until it has been assigned a body ID.
  37. /// This can be used to decouple allocation from registering the body. A call to CreateBodyWithoutID followed by AssignBodyID is equivalent to calling CreateBodyWithID.
  38. /// @return Created body
  39. Body * CreateBodyWithoutID(const BodyCreationSettings &inSettings) const;
  40. /// Advanced use only. Destroy a body previously created with CreateBodyWithoutID that hasn't gotten an ID yet through the AssignBodyID function,
  41. /// or a body that has had its body ID unassigned through UnassignBodyIDs. Bodies that have an ID should be destroyed through DestroyBody.
  42. void DestroyBodyWithoutID(Body *inBody) const;
  43. /// 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.
  44. /// @return false if the body already has an ID or out of body ids.
  45. bool AssignBodyID(Body *ioBody);
  46. /// 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.
  47. /// @return false if the body already has an ID or if the ID is not valid.
  48. bool AssignBodyID(Body *ioBody, const BodyID &inBodyID);
  49. /// Advanced use only. See UnassignBodyIDs. Unassigns the ID of a single body.
  50. Body * UnassignBodyID(const BodyID &inBodyID);
  51. /// 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.
  52. /// 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.
  53. /// @param inBodyIDs A list of body IDs
  54. /// @param inNumber Number of bodies in the list
  55. /// @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).
  56. void UnassignBodyIDs(const BodyID *inBodyIDs, int inNumber, Body **outBodies);
  57. /// Destroy a body
  58. void DestroyBody(const BodyID &inBodyID);
  59. /// Destroy multiple bodies
  60. void DestroyBodies(const BodyID *inBodyIDs, int inNumber);
  61. /// Add body to the physics system.
  62. /// Note that if you need to add multiple bodies, use the AddBodiesPrepare/AddBodiesFinalize function.
  63. /// 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!
  64. /// After adding, to get a body by ID use the BodyLockRead or BodyLockWrite interface!
  65. void AddBody(const BodyID &inBodyID, EActivation inActivationMode);
  66. /// Remove body from the physics system.
  67. void RemoveBody(const BodyID &inBodyID);
  68. /// Check if a body has been added to the physics system.
  69. bool IsAdded(const BodyID &inBodyID) const;
  70. /// Combines CreateBody and AddBody
  71. /// @return Created body ID or an invalid ID when out of bodies
  72. BodyID CreateAndAddBody(const BodyCreationSettings &inSettings, EActivation inActivationMode);
  73. /// Broadphase add state handle, used to keep track of a batch while ading to the broadphase.
  74. using AddState = void *;
  75. ///@name Batch adding interface, see Broadphase for further documentation.
  76. /// Note that ioBodies array must be kept constant while the add is in progress.
  77. ///@{
  78. AddState AddBodiesPrepare(BodyID *ioBodies, int inNumber);
  79. void AddBodiesFinalize(BodyID *ioBodies, int inNumber, AddState inAddState, EActivation inActivationMode);
  80. void AddBodiesAbort(BodyID *ioBodies, int inNumber, AddState inAddState);
  81. void RemoveBodies(BodyID *ioBodies, int inNumber);
  82. ///@}
  83. ///@name Activate / deactivate a body
  84. ///@{
  85. void ActivateBody(const BodyID &inBodyID);
  86. void ActivateBodies(const BodyID *inBodyIDs, int inNumber);
  87. void DeactivateBody(const BodyID &inBodyID);
  88. void DeactivateBodies(const BodyID *inBodyIDs, int inNumber);
  89. bool IsActive(const BodyID &inBodyID) const;
  90. ///@}
  91. /// Create a two body constraint
  92. TwoBodyConstraint * CreateConstraint(const TwoBodyConstraintSettings *inSettings, const BodyID &inBodyID1, const BodyID &inBodyID2);
  93. /// Activate non-static bodies attached to a constraint
  94. void ActivateConstraint(const TwoBodyConstraint *inConstraint);
  95. ///@name Access to the shape of a body
  96. ///@{
  97. /// Get the current shape
  98. RefConst<Shape> GetShape(const BodyID &inBodyID) const;
  99. /// Set a new shape on the body
  100. /// @param inBodyID Body ID of body that had its shape changed
  101. /// @param inShape The new shape
  102. /// @param inUpdateMassProperties When true, the mass and inertia tensor is recalculated
  103. /// @param inActivationMode Weather or not to activate the body
  104. void SetShape(const BodyID &inBodyID, const Shape *inShape, bool inUpdateMassProperties, EActivation inActivationMode) const;
  105. /// Notify all systems to indicate that a shape has changed (usable for MutableCompoundShapes)
  106. /// @param inBodyID Body ID of body that had its shape changed
  107. /// @param inPreviousCenterOfMass Center of mass of the shape before the alterations
  108. /// @param inUpdateMassProperties When true, the mass and inertia tensor is recalculated
  109. /// @param inActivationMode Weather or not to activate the body
  110. void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inPreviousCenterOfMass, bool inUpdateMassProperties, EActivation inActivationMode) const;
  111. ///@}
  112. ///@name Object layer of a body
  113. ///@{
  114. void SetObjectLayer(const BodyID &inBodyID, ObjectLayer inLayer);
  115. ObjectLayer GetObjectLayer(const BodyID &inBodyID) const;
  116. ///@}
  117. ///@name Position and rotation of a body
  118. ///@{
  119. void SetPositionAndRotation(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode);
  120. 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.
  121. void GetPositionAndRotation(const BodyID &inBodyID, RVec3 &outPosition, Quat &outRotation) const;
  122. void SetPosition(const BodyID &inBodyID, RVec3Arg inPosition, EActivation inActivationMode);
  123. RVec3 GetPosition(const BodyID &inBodyID) const;
  124. RVec3 GetCenterOfMassPosition(const BodyID &inBodyID) const;
  125. void SetRotation(const BodyID &inBodyID, QuatArg inRotation, EActivation inActivationMode);
  126. Quat GetRotation(const BodyID &inBodyID) const;
  127. RMat44 GetWorldTransform(const BodyID &inBodyID) const;
  128. RMat44 GetCenterOfMassTransform(const BodyID &inBodyID) const;
  129. ///@}
  130. /// Set velocity of body such that it will be positioned at inTargetPosition/Rotation in inDeltaTime seconds (will activate body if needed)
  131. void MoveKinematic(const BodyID &inBodyID, RVec3Arg inTargetPosition, QuatArg inTargetRotation, float inDeltaTime);
  132. /// Linear or angular velocity (functions will activate body if needed).
  133. /// 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$
  134. void SetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity);
  135. void GetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3 &outLinearVelocity, Vec3 &outAngularVelocity) const;
  136. void SetLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity);
  137. Vec3 GetLinearVelocity(const BodyID &inBodyID) const;
  138. void AddLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity); ///< Add velocity to current velocity
  139. void AddLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity); ///< Add linear and angular to current velocities
  140. void SetAngularVelocity(const BodyID &inBodyID, Vec3Arg inAngularVelocity);
  141. Vec3 GetAngularVelocity(const BodyID &inBodyID) const;
  142. 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
  143. /// Set the complete motion state of a body.
  144. /// 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$
  145. void SetPositionRotationAndVelocity(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity);
  146. ///@name Add forces to the body
  147. ///@{
  148. void AddForce(const BodyID &inBodyID, Vec3Arg inForce); ///< See Body::AddForce
  149. void AddForce(const BodyID &inBodyID, Vec3Arg inForce, RVec3Arg inPoint); ///< Applied at inPoint
  150. void AddTorque(const BodyID &inBodyID, Vec3Arg inTorque); ///< See Body::AddTorque
  151. void AddForceAndTorque(const BodyID &inBodyID, Vec3Arg inForce, Vec3Arg inTorque); ///< A combination of Body::AddForce and Body::AddTorque
  152. ///@}
  153. ///@name Add an impulse to the body
  154. ///@{
  155. void AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse); ///< Applied at center of mass
  156. void AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse, RVec3Arg inPoint); ///< Applied at inPoint
  157. void AddAngularImpulse(const BodyID &inBodyID, Vec3Arg inAngularImpulse);
  158. ///@}
  159. ///@name Body motion type
  160. ///@{
  161. void SetMotionType(const BodyID &inBodyID, EMotionType inMotionType, EActivation inActivationMode);
  162. EMotionType GetMotionType(const BodyID &inBodyID) const;
  163. ///@}
  164. ///@name Body motion quality
  165. ///@{
  166. void SetMotionQuality(const BodyID &inBodyID, EMotionQuality inMotionQuality);
  167. EMotionQuality GetMotionQuality(const BodyID &inBodyID) const;
  168. ///@}
  169. /// Get inverse inertia tensor in world space
  170. Mat44 GetInverseInertia(const BodyID &inBodyID) const;
  171. ///@name Restitution
  172. ///@{
  173. void SetRestitution(const BodyID &inBodyID, float inRestitution);
  174. float GetRestitution(const BodyID &inBodyID) const;
  175. ///@}
  176. ///@name Friction
  177. ///@{
  178. void SetFriction(const BodyID &inBodyID, float inFriction);
  179. float GetFriction(const BodyID &inBodyID) const;
  180. ///@}
  181. ///@name Gravity factor
  182. ///@{
  183. void SetGravityFactor(const BodyID &inBodyID, float inGravityFactor);
  184. float GetGravityFactor(const BodyID &inBodyID) const;
  185. ///@}
  186. /// Get transform and shape for this body, used to perform collision detection
  187. TransformedShape GetTransformedShape(const BodyID &inBodyID) const;
  188. /// Get the user data for a body
  189. uint64 GetUserData(const BodyID &inBodyID) const;
  190. /// Get the material for a particular sub shape
  191. const PhysicsMaterial * GetMaterial(const BodyID &inBodyID, const SubShapeID &inSubShapeID) const;
  192. /// 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.
  193. void InvalidateContactCache(const BodyID &inBodyID);
  194. private:
  195. BodyLockInterface * mBodyLockInterface = nullptr;
  196. BodyManager * mBodyManager = nullptr;
  197. BroadPhase * mBroadPhase = nullptr;
  198. };
  199. JPH_NAMESPACE_END