BodyInterface.h 13 KB

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