Body.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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/Core/NonCopyable.h>
  6. #include <Jolt/Geometry/AABox.h>
  7. #include <Jolt/Physics/Collision/Shape/Shape.h>
  8. #include <Jolt/Physics/Collision/BroadPhase/BroadPhaseLayer.h>
  9. #include <Jolt/Physics/Collision/ObjectLayer.h>
  10. #include <Jolt/Physics/Collision/CollisionGroup.h>
  11. #include <Jolt/Physics/Collision/TransformedShape.h>
  12. #include <Jolt/Physics/Body/MotionProperties.h>
  13. #include <Jolt/Physics/Body/BodyID.h>
  14. #include <Jolt/Physics/Body/BodyAccess.h>
  15. #include <Jolt/Physics/Body/BodyType.h>
  16. #include <Jolt/Core/StringTools.h>
  17. JPH_NAMESPACE_BEGIN
  18. class StateRecorder;
  19. class BodyCreationSettings;
  20. class SoftBodyCreationSettings;
  21. /// A rigid body that can be simulated using the physics system
  22. ///
  23. /// Note that internally all properties (position, velocity etc.) are tracked relative to the center of mass of the object to simplify the simulation of the object.
  24. ///
  25. /// The offset between the position of the body and the center of mass position of the body is GetShape()->GetCenterOfMass().
  26. /// The functions that get/set the position of the body all indicate if they are relative to the center of mass or to the original position in which the shape was created.
  27. ///
  28. /// The linear velocity is also velocity of the center of mass, to correct for this: \f$VelocityCOM = Velocity - AngularVelocity \times ShapeCOM\f$.
  29. class alignas(JPH_RVECTOR_ALIGNMENT) JPH_EXPORT_GCC_BUG_WORKAROUND Body : public NonCopyable
  30. {
  31. public:
  32. JPH_OVERRIDE_NEW_DELETE
  33. /// Default constructor
  34. Body() = default;
  35. /// Destructor
  36. ~Body() { JPH_ASSERT(mMotionProperties == nullptr); }
  37. /// Get the id of this body
  38. inline const BodyID & GetID() const { return mID; }
  39. /// Get the type of body (rigid or soft)
  40. inline EBodyType GetBodyType() const { return mBodyType; }
  41. /// Check if this body is a rigid body
  42. inline bool IsRigidBody() const { return mBodyType == EBodyType::RigidBody; }
  43. /// Check if this body is a soft body
  44. inline bool IsSoftBody() const { return mBodyType == EBodyType::SoftBody; }
  45. /// If this body is currently actively simulating (true) or sleeping (false)
  46. inline bool IsActive() const { return mMotionProperties != nullptr && mMotionProperties->mIndexInActiveBodies != cInactiveIndex; }
  47. /// Check if this body is static (not movable)
  48. inline bool IsStatic() const { return mMotionType == EMotionType::Static; }
  49. /// Check if this body is kinematic (keyframed), which means that it will move according to its current velocity, but forces don't affect it
  50. inline bool IsKinematic() const { return mMotionType == EMotionType::Kinematic; }
  51. /// Check if this body is dynamic, which means that it moves and forces can act on it
  52. inline bool IsDynamic() const { return mMotionType == EMotionType::Dynamic; }
  53. /// Check if a body could be made kinematic or dynamic (if it was created dynamic or with mAllowDynamicOrKinematic set to true)
  54. inline bool CanBeKinematicOrDynamic() const { return mMotionProperties != nullptr; }
  55. /// Change the body to a sensor. A sensor will receive collision callbacks, but will not cause any collision responses and can be used as a trigger volume.
  56. /// The cheapest sensor (in terms of CPU usage) is a sensor with motion type Static (they can be moved around using BodyInterface::SetPosition/SetPositionAndRotation).
  57. /// These sensors will only detect collisions with active Dynamic or Kinematic bodies. As soon as a body go to sleep, the contact point with the sensor will be lost.
  58. /// If you make a sensor Dynamic or Kinematic and activate them, the sensor will be able to detect collisions with sleeping bodies too. An active sensor will never go to sleep automatically.
  59. /// When you make a Dynamic or Kinematic sensor, make sure it is in an ObjectLayer that does not collide with Static bodies or other sensors to avoid extra overhead in the broad phase.
  60. inline void SetIsSensor(bool inIsSensor) { JPH_ASSERT(IsRigidBody()); if (inIsSensor) mFlags.fetch_or(uint8(EFlags::IsSensor), memory_order_relaxed); else mFlags.fetch_and(uint8(~uint8(EFlags::IsSensor)), memory_order_relaxed); }
  61. /// Check if this body is a sensor.
  62. inline bool IsSensor() const { return (mFlags.load(memory_order_relaxed) & uint8(EFlags::IsSensor)) != 0; }
  63. // If this sensor detects static objects entering it. Note that the sensor must be kinematic and active for it to detect static objects.
  64. inline void SetSensorDetectsStatic(bool inDetectsStatic) { JPH_ASSERT(IsRigidBody()); if (inDetectsStatic) mFlags.fetch_or(uint8(EFlags::SensorDetectsStatic), memory_order_relaxed); else mFlags.fetch_and(uint8(~uint8(EFlags::SensorDetectsStatic)), memory_order_relaxed); }
  65. /// Check if this sensor detects static objects entering it.
  66. inline bool SensorDetectsStatic() const { return (mFlags.load(memory_order_relaxed) & uint8(EFlags::SensorDetectsStatic)) != 0; }
  67. /// If PhysicsSettings::mUseManifoldReduction is true, this allows turning off manifold reduction for this specific body. Manifold reduction by default will combine contacts that come from different SubShapeIDs (e.g. different triangles or different compound shapes).
  68. /// If the application requires tracking exactly which SubShapeIDs are in contact, you can turn off manifold reduction. Note that this comes at a performance cost.
  69. inline void SetUseManifoldReduction(bool inUseReduction) { JPH_ASSERT(IsRigidBody()); if (inUseReduction) mFlags.fetch_or(uint8(EFlags::UseManifoldReduction), memory_order_relaxed); else mFlags.fetch_and(uint8(~uint8(EFlags::UseManifoldReduction)), memory_order_relaxed); }
  70. /// Check if this body can use manifold reduction.
  71. inline bool GetUseManifoldReduction() const { return (mFlags.load(memory_order_relaxed) & uint8(EFlags::UseManifoldReduction)) != 0; }
  72. /// Checks if the combination of this body and inBody2 should use manifold reduction
  73. inline bool GetUseManifoldReductionWithBody(const Body &inBody2) const { return ((mFlags.load(memory_order_relaxed) & inBody2.mFlags.load(memory_order_relaxed)) & uint8(EFlags::UseManifoldReduction)) != 0; }
  74. /// Motion type of this body
  75. inline EMotionType GetMotionType() const { return mMotionType; }
  76. void SetMotionType(EMotionType inMotionType);
  77. /// Get broadphase layer, this determines in which broad phase sub-tree the object is placed
  78. inline BroadPhaseLayer GetBroadPhaseLayer() const { return mBroadPhaseLayer; }
  79. /// Get object layer, this determines which other objects it collides with
  80. inline ObjectLayer GetObjectLayer() const { return mObjectLayer; }
  81. /// Collision group and sub-group ID, determines which other objects it collides with
  82. const CollisionGroup & GetCollisionGroup() const { return mCollisionGroup; }
  83. CollisionGroup & GetCollisionGroup() { return mCollisionGroup; }
  84. void SetCollisionGroup(const CollisionGroup &inGroup) { mCollisionGroup = inGroup; }
  85. /// If this body can go to sleep. Note that disabling sleeping on a sleeping object wil not wake it up.
  86. bool GetAllowSleeping() const { return mMotionProperties->mAllowSleeping; }
  87. void SetAllowSleeping(bool inAllow);
  88. /// Friction (dimensionless number, usually between 0 and 1, 0 = no friction, 1 = friction force equals force that presses the two bodies together). Note that bodies can have negative friction but the combined friction (see PhysicsSystem::SetCombineFriction) should never go below zero.
  89. inline float GetFriction() const { return mFriction; }
  90. void SetFriction(float inFriction) { mFriction = inFriction; }
  91. /// Restitution (dimensionless number, usually between 0 and 1, 0 = completely inelastic collision response, 1 = completely elastic collision response). Note that bodies can have negative restitution but the combined restitution (see PhysicsSystem::SetCombineRestitution) should never go below zero.
  92. inline float GetRestitution() const { return mRestitution; }
  93. void SetRestitution(float inRestitution) { mRestitution = inRestitution; }
  94. /// Get world space linear velocity of the center of mass (unit: m/s)
  95. inline Vec3 GetLinearVelocity() const { return !IsStatic()? mMotionProperties->GetLinearVelocity() : Vec3::sZero(); }
  96. /// Set world space linear velocity of the center of mass (unit: m/s)
  97. void SetLinearVelocity(Vec3Arg inLinearVelocity) { JPH_ASSERT(!IsStatic()); mMotionProperties->SetLinearVelocity(inLinearVelocity); }
  98. /// Set world space linear velocity of the center of mass, will make sure the value is clamped against the maximum linear velocity
  99. void SetLinearVelocityClamped(Vec3Arg inLinearVelocity) { JPH_ASSERT(!IsStatic()); mMotionProperties->SetLinearVelocityClamped(inLinearVelocity); }
  100. /// Get world space angular velocity of the center of mass (unit: rad/s)
  101. inline Vec3 GetAngularVelocity() const { return !IsStatic()? mMotionProperties->GetAngularVelocity() : Vec3::sZero(); }
  102. /// Set world space angular velocity of the center of mass (unit: rad/s)
  103. void SetAngularVelocity(Vec3Arg inAngularVelocity) { JPH_ASSERT(!IsStatic()); mMotionProperties->SetAngularVelocity(inAngularVelocity); }
  104. /// Set world space angular velocity of the center of mass, will make sure the value is clamped against the maximum angular velocity
  105. void SetAngularVelocityClamped(Vec3Arg inAngularVelocity) { JPH_ASSERT(!IsStatic()); mMotionProperties->SetAngularVelocityClamped(inAngularVelocity); }
  106. /// Velocity of point inPoint (in center of mass space, e.g. on the surface of the body) of the body (unit: m/s)
  107. inline Vec3 GetPointVelocityCOM(Vec3Arg inPointRelativeToCOM) const { return !IsStatic()? mMotionProperties->GetPointVelocityCOM(inPointRelativeToCOM) : Vec3::sZero(); }
  108. /// Velocity of point inPoint (in world space, e.g. on the surface of the body) of the body (unit: m/s)
  109. inline Vec3 GetPointVelocity(RVec3Arg inPoint) const { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::Read)); return GetPointVelocityCOM(Vec3(inPoint - mPosition)); }
  110. /// Add force (unit: N) at center of mass for the next time step, will be reset after the next call to PhysicsSimulation::Update
  111. inline void AddForce(Vec3Arg inForce) { JPH_ASSERT(IsDynamic()); (Vec3::sLoadFloat3Unsafe(mMotionProperties->mForce) + inForce).StoreFloat3(&mMotionProperties->mForce); }
  112. /// Add force (unit: N) at inPosition for the next time step, will be reset after the next call to PhysicsSimulation::Update
  113. inline void AddForce(Vec3Arg inForce, RVec3Arg inPosition);
  114. /// Add torque (unit: N m) for the next time step, will be reset after the next call to PhysicsSimulation::Update
  115. inline void AddTorque(Vec3Arg inTorque) { JPH_ASSERT(IsDynamic()); (Vec3::sLoadFloat3Unsafe(mMotionProperties->mTorque) + inTorque).StoreFloat3(&mMotionProperties->mTorque); }
  116. // Get the total amount of force applied to the center of mass this time step (through AddForce calls). Note that it will reset to zero after PhysicsSimulation::Update.
  117. inline Vec3 GetAccumulatedForce() const { JPH_ASSERT(IsDynamic()); return mMotionProperties->GetAccumulatedForce(); }
  118. // Get the total amount of torque applied to the center of mass this time step (through AddForce/AddTorque calls). Note that it will reset to zero after PhysicsSimulation::Update.
  119. inline Vec3 GetAccumulatedTorque() const { JPH_ASSERT(IsDynamic()); return mMotionProperties->GetAccumulatedTorque(); }
  120. // Reset the total accumulated force, not that this will be done automatically after every time step.
  121. JPH_INLINE void ResetForce() { JPH_ASSERT(IsDynamic()); return mMotionProperties->ResetForce(); }
  122. // Reset the total accumulated torque, not that this will be done automatically after every time step.
  123. JPH_INLINE void ResetTorque() { JPH_ASSERT(IsDynamic()); return mMotionProperties->ResetTorque(); }
  124. /// Get inverse inertia tensor in world space
  125. inline Mat44 GetInverseInertia() const;
  126. /// Add impulse to center of mass (unit: kg m/s)
  127. inline void AddImpulse(Vec3Arg inImpulse);
  128. /// Add impulse to point in world space (unit: kg m/s)
  129. inline void AddImpulse(Vec3Arg inImpulse, RVec3Arg inPosition);
  130. /// Add angular impulse in world space (unit: N m s)
  131. inline void AddAngularImpulse(Vec3Arg inAngularImpulse);
  132. /// Set velocity of body such that it will be positioned at inTargetPosition/Rotation in inDeltaTime seconds.
  133. void MoveKinematic(RVec3Arg inTargetPosition, QuatArg inTargetRotation, float inDeltaTime);
  134. /// Applies an impulse to the body that simulates fluid buoyancy and drag
  135. /// @param inSurfacePosition Position on the fluid surface in world space
  136. /// @param inSurfaceNormal Normal of the fluid surface (should point up)
  137. /// @param inBuoyancy The buoyancy factor for the body. 1 = neutral body, < 1 sinks, > 1 floats. Note that we don't use the fluid density since it is harder to configure than a simple number between [0, 2]
  138. /// @param inLinearDrag Linear drag factor that slows down the body when in the fluid (approx. 0.5)
  139. /// @param inAngularDrag Angular drag factor that slows down rotation when the body is in the fluid (approx. 0.01)
  140. /// @param inFluidVelocity The average velocity of the fluid (in m/s) in which the body resides
  141. /// @param inGravity The graviy vector (pointing down)
  142. /// @param inDeltaTime Delta time of the next simulation step (in s)
  143. /// @return true if an impulse was applied, false if the body was not in the fluid
  144. bool ApplyBuoyancyImpulse(RVec3Arg inSurfacePosition, Vec3Arg inSurfaceNormal, float inBuoyancy, float inLinearDrag, float inAngularDrag, Vec3Arg inFluidVelocity, Vec3Arg inGravity, float inDeltaTime);
  145. /// Check if this body has been added to the physics system
  146. inline bool IsInBroadPhase() const { return (mFlags.load(memory_order_relaxed) & uint8(EFlags::IsInBroadPhase)) != 0; }
  147. /// Check if this body has been changed in such a way that the collision cache should be considered invalid for any body interacting with this body
  148. inline bool IsCollisionCacheInvalid() const { return (mFlags.load(memory_order_relaxed) & uint8(EFlags::InvalidateContactCache)) != 0; }
  149. /// Get the shape of this body
  150. inline const Shape * GetShape() const { return mShape; }
  151. /// World space position of the body
  152. inline RVec3 GetPosition() const { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::Read)); return mPosition - mRotation * mShape->GetCenterOfMass(); }
  153. /// World space rotation of the body
  154. inline Quat GetRotation() const { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::Read)); return mRotation; }
  155. /// Calculates the transform of this body
  156. inline RMat44 GetWorldTransform() const;
  157. /// Gets the world space position of this body's center of mass
  158. inline RVec3 GetCenterOfMassPosition() const { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::Read)); return mPosition; }
  159. /// Calculates the transform for this body's center of mass
  160. inline RMat44 GetCenterOfMassTransform() const;
  161. /// Calculates the inverse of the transform for this body's center of mass
  162. inline RMat44 GetInverseCenterOfMassTransform() const;
  163. /// Get world space bounding box
  164. inline const AABox & GetWorldSpaceBounds() const { return mBounds; }
  165. /// Access to the motion properties
  166. const MotionProperties *GetMotionProperties() const { JPH_ASSERT(!IsStatic()); return mMotionProperties; }
  167. MotionProperties * GetMotionProperties() { JPH_ASSERT(!IsStatic()); return mMotionProperties; }
  168. /// Access to the motion properties (version that does not check if the object is kinematic or dynamic)
  169. const MotionProperties *GetMotionPropertiesUnchecked() const { return mMotionProperties; }
  170. MotionProperties * GetMotionPropertiesUnchecked() { return mMotionProperties; }
  171. /// Access to the user data, can be used for anything by the application
  172. uint64 GetUserData() const { return mUserData; }
  173. void SetUserData(uint64 inUserData) { mUserData = inUserData; }
  174. /// Get surface normal of a particular sub shape and its world space surface position on this body
  175. inline Vec3 GetWorldSpaceSurfaceNormal(const SubShapeID &inSubShapeID, RVec3Arg inPosition) const;
  176. /// Get the transformed shape of this body, which can be used to do collision detection outside of a body lock
  177. inline TransformedShape GetTransformedShape() const { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::Read)); return TransformedShape(mPosition, mRotation, mShape, mID); }
  178. /// Debug function to convert a body back to a body creation settings object to be able to save/recreate the body later
  179. BodyCreationSettings GetBodyCreationSettings() const;
  180. /// Debug function to convert a soft body back to a soft body creation settings object to be able to save/recreate the body later
  181. SoftBodyCreationSettings GetSoftBodyCreationSettings() const;
  182. /// A dummy body that can be used by constraints to attach a constraint to the world instead of another body
  183. static Body sFixedToWorld;
  184. ///@name THESE FUNCTIONS ARE FOR INTERNAL USE ONLY AND SHOULD NOT BE CALLED BY THE APPLICATION
  185. ///@{
  186. /// Helper function for BroadPhase::FindCollidingPairs that returns true when two bodies can collide
  187. /// It assumes that body 1 is dynamic and active and guarantees that it body 1 collides with body 2 that body 2 will not collide with body 1 in order to avoid finding duplicate collision pairs
  188. static inline bool sFindCollidingPairsCanCollide(const Body &inBody1, const Body &inBody2);
  189. /// Update position using an Euler step (used during position integrate & constraint solving)
  190. inline void AddPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime) { JPH_ASSERT(IsRigidBody()); JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::ReadWrite)); mPosition += mMotionProperties->LockTranslation(inLinearVelocityTimesDeltaTime); JPH_ASSERT(!mPosition.IsNaN()); }
  191. inline void SubPositionStep(Vec3Arg inLinearVelocityTimesDeltaTime) { JPH_ASSERT(IsRigidBody()); JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sPositionAccess, BodyAccess::EAccess::ReadWrite)); mPosition -= mMotionProperties->LockTranslation(inLinearVelocityTimesDeltaTime); JPH_ASSERT(!mPosition.IsNaN()); }
  192. /// Update rotation using an Euler step (using during position integrate & constraint solving)
  193. inline void AddRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime);
  194. inline void SubRotationStep(Vec3Arg inAngularVelocityTimesDeltaTime);
  195. /// Flag if body is in the broadphase (should only be called by the BroadPhase)
  196. inline void SetInBroadPhaseInternal(bool inInBroadPhase) { if (inInBroadPhase) mFlags.fetch_or(uint8(EFlags::IsInBroadPhase), memory_order_relaxed); else mFlags.fetch_and(uint8(~uint8(EFlags::IsInBroadPhase)), memory_order_relaxed); }
  197. /// Invalidate the contact cache (should only be called by the BodyManager), will be reset the next simulation step. Returns true if the contact cache was still valid.
  198. inline bool InvalidateContactCacheInternal() { return (mFlags.fetch_or(uint8(EFlags::InvalidateContactCache), memory_order_relaxed) & uint8(EFlags::InvalidateContactCache)) == 0; }
  199. /// Reset the collision cache invalid flag (should only be called by the BodyManager).
  200. inline void ValidateContactCacheInternal() { JPH_IF_ENABLE_ASSERTS(uint8 old_val = ) mFlags.fetch_and(uint8(~uint8(EFlags::InvalidateContactCache)), memory_order_relaxed); JPH_ASSERT((old_val & uint8(EFlags::InvalidateContactCache)) != 0); }
  201. /// Updates world space bounding box (should only be called by the PhysicsSystem)
  202. void CalculateWorldSpaceBoundsInternal();
  203. /// Function to update body's position (should only be called by the BodyInterface since it also requires updating the broadphase)
  204. void SetPositionAndRotationInternal(RVec3Arg inPosition, QuatArg inRotation, bool inResetSleepTestSpheres = true);
  205. /// Updates the center of mass and optionally mass propertes after shifting the center of mass or changes to the shape (should only be called by the BodyInterface since it also requires updating the broadphase)
  206. /// @param inPreviousCenterOfMass Center of mass of the shape before the alterations
  207. /// @param inUpdateMassProperties When true, the mass and inertia tensor is recalculated
  208. void UpdateCenterOfMassInternal(Vec3Arg inPreviousCenterOfMass, bool inUpdateMassProperties);
  209. /// Function to update a body's shape (should only be called by the BodyInterface since it also requires updating the broadphase)
  210. /// @param inShape The new shape for this body
  211. /// @param inUpdateMassProperties When true, the mass and inertia tensor is recalculated
  212. void SetShapeInternal(const Shape *inShape, bool inUpdateMassProperties);
  213. /// Access to the index in the BodyManager::mActiveBodies list
  214. uint32 GetIndexInActiveBodiesInternal() const { return mMotionProperties != nullptr? mMotionProperties->mIndexInActiveBodies : cInactiveIndex; }
  215. /// Update eligibility for sleeping
  216. ECanSleep UpdateSleepStateInternal(float inDeltaTime, float inMaxMovement, float inTimeBeforeSleep);
  217. /// Saving state for replay
  218. void SaveState(StateRecorder &inStream) const;
  219. /// Restoring state for replay
  220. void RestoreState(StateRecorder &inStream);
  221. ///@}
  222. static constexpr uint32 cInactiveIndex = MotionProperties::cInactiveIndex; ///< Constant indicating that body is not active
  223. private:
  224. friend class BodyManager;
  225. explicit Body(bool); ///< Alternative constructor that initializes all members
  226. inline void GetSleepTestPoints(RVec3 *outPoints) const; ///< Determine points to test for checking if body is sleeping: COM, COM + largest bounding box axis, COM + second largest bounding box axis
  227. inline void ResetSleepTestSpheres(); ///< Reset spheres to current position as returned by GetSleepTestPoints
  228. enum class EFlags : uint8
  229. {
  230. IsSensor = 1 << 0, ///< If this object is a sensor. A sensor will receive collision callbacks, but will not cause any collision responses and can be used as a trigger volume.
  231. SensorDetectsStatic = 1 << 1, ///< If this sensor detects static objects entering it.
  232. IsInBroadPhase = 1 << 2, ///< Set this bit to indicate that the body is in the broadphase
  233. InvalidateContactCache = 1 << 3, ///< Set this bit to indicate that all collision caches for this body are invalid, will be reset the next simulation step.
  234. UseManifoldReduction = 1 << 4, ///< Set this bit to indicate that this body can use manifold reduction (if PhysicsSettings::mUseManifoldReduction is true)
  235. };
  236. // 16 byte aligned
  237. RVec3 mPosition; ///< World space position of center of mass
  238. Quat mRotation; ///< World space rotation of center of mass
  239. AABox mBounds; ///< World space bounding box of the body
  240. // 8 byte aligned
  241. RefConst<Shape> mShape; ///< Shape representing the volume of this body
  242. MotionProperties * mMotionProperties = nullptr; ///< If this is a keyframed or dynamic object, this object holds all information about the movement
  243. uint64 mUserData = 0; ///< User data, can be used for anything by the application
  244. CollisionGroup mCollisionGroup; ///< The collision group this body belongs to (determines if two objects can collide)
  245. // 4 byte aligned
  246. float mFriction; ///< Friction of the body (dimensionless number, usually between 0 and 1, 0 = no friction, 1 = friction force equals force that presses the two bodies together). Note that bodies can have negative friction but the combined friction (see PhysicsSystem::SetCombineFriction) should never go below zero.
  247. float mRestitution; ///< Restitution of body (dimensionless number, usually between 0 and 1, 0 = completely inelastic collision response, 1 = completely elastic collision response). Note that bodies can have negative restitution but the combined restitution (see PhysicsSystem::SetCombineRestitution) should never go below zero.
  248. BodyID mID; ///< ID of the body (index in the bodies array)
  249. // 2 or 4 bytes aligned
  250. ObjectLayer mObjectLayer; ///< The collision layer this body belongs to (determines if two objects can collide)
  251. // 1 byte aligned
  252. EBodyType mBodyType; ///< Type of body (rigid or soft)
  253. BroadPhaseLayer mBroadPhaseLayer; ///< The broad phase layer this body belongs to
  254. EMotionType mMotionType; ///< Type of motion (static, dynamic or kinematic)
  255. atomic<uint8> mFlags = 0; ///< See EFlags for possible flags
  256. // 122 bytes up to here (64-bit mode, single precision, 16-bit ObjectLayer)
  257. #if JPH_CPU_ADDRESS_BITS == 32
  258. // Padding for mShape, mMotionProperties, mCollisionGroup.mGroupFilter being 4 instead of 8 bytes in 32 bit mode
  259. uint8 mPadding[12];
  260. #endif
  261. };
  262. static_assert(sizeof(Body) == JPH_IF_SINGLE_PRECISION_ELSE(128, 160), "Body size is incorrect");
  263. static_assert(alignof(Body) == JPH_RVECTOR_ALIGNMENT, "Body should properly align");
  264. JPH_NAMESPACE_END
  265. #include "Body.inl"