BodyInterface.h 13 KB

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