Constraint.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/Reference.h>
  6. #include <Jolt/Core/NonCopyable.h>
  7. #include <Jolt/Core/Result.h>
  8. #include <Jolt/ObjectStream/SerializableObject.h>
  9. JPH_NAMESPACE_BEGIN
  10. class BodyID;
  11. class IslandBuilder;
  12. class LargeIslandSplitter;
  13. class BodyManager;
  14. class StateRecorder;
  15. class StreamIn;
  16. class StreamOut;
  17. #ifdef JPH_DEBUG_RENDERER
  18. class DebugRenderer;
  19. #endif // JPH_DEBUG_RENDERER
  20. /// Enum to identify constraint type
  21. enum class EConstraintType
  22. {
  23. Constraint,
  24. TwoBodyConstraint,
  25. };
  26. /// Enum to identify constraint sub type
  27. enum class EConstraintSubType
  28. {
  29. Fixed,
  30. Point,
  31. Hinge,
  32. Slider,
  33. Distance,
  34. Cone,
  35. SwingTwist,
  36. SixDOF,
  37. Path,
  38. Vehicle,
  39. RackAndPinion,
  40. Gear,
  41. Pulley,
  42. /// User defined constraint types start here
  43. User1,
  44. User2,
  45. User3,
  46. User4
  47. };
  48. /// Certain constraints support setting them up in local or world space. This governs what is used.
  49. enum class EConstraintSpace
  50. {
  51. LocalToBodyCOM, ///< All constraint properties are specified in local space to center of mass of the bodies that are being constrained (so e.g. 'constraint position 1' will be local to body 1 COM, 'constraint position 2' will be local to body 2 COM). Note that this means you need to subtract Shape::GetCenterOfMass() from positions!
  52. WorldSpace, ///< All constraint properties are specified in world space
  53. };
  54. /// Class used to store the configuration of a constraint. Allows run-time creation of constraints.
  55. class ConstraintSettings : public SerializableObject, public RefTarget<ConstraintSettings>
  56. {
  57. public:
  58. JPH_DECLARE_SERIALIZABLE_VIRTUAL(ConstraintSettings)
  59. using ConstraintResult = Result<Ref<ConstraintSettings>>;
  60. /// Saves the contents of the constraint settings in binary form to inStream.
  61. virtual void SaveBinaryState(StreamOut &inStream) const;
  62. /// Creates a constraint of the correct type and restores its contents from the binary stream inStream.
  63. static ConstraintResult sRestoreFromBinaryState(StreamIn &inStream);
  64. /// If this constraint is enabled initially. Use Constraint::SetEnabled to toggle after creation.
  65. bool mEnabled = true;
  66. /// Override for the number of solver velocity iterations to run, the total amount of iterations is the max of PhysicsSettings::mNumVelocitySteps and this for all constraints in the island.
  67. int mNumVelocityStepsOverride = 0;
  68. /// Override for the number of position velocity iterations to run, the total amount of iterations is the max of PhysicsSettings::mNumPositionSteps and this for all constraints in the island.
  69. int mNumPositionStepsOverride = 0;
  70. /// Size of constraint when drawing it through the debug renderer
  71. float mDrawConstraintSize = 1.0f;
  72. /// User data value (can be used by application)
  73. uint64 mUserData = 0;
  74. protected:
  75. /// This function should not be called directly, it is used by sRestoreFromBinaryState.
  76. virtual void RestoreBinaryState(StreamIn &inStream);
  77. };
  78. /// Base class for all physics constraints. A constraint removes one or more degrees of freedom for a rigid body.
  79. class Constraint : public RefTarget<Constraint>, public NonCopyable
  80. {
  81. public:
  82. JPH_OVERRIDE_NEW_DELETE
  83. /// Constructor
  84. explicit Constraint(const ConstraintSettings &inSettings) :
  85. #ifdef JPH_DEBUG_RENDERER
  86. mDrawConstraintSize(inSettings.mDrawConstraintSize),
  87. #endif // JPH_DEBUG_RENDERER
  88. mNumVelocityStepsOverride(inSettings.mNumVelocityStepsOverride),
  89. mNumPositionStepsOverride(inSettings.mNumPositionStepsOverride),
  90. mEnabled(inSettings.mEnabled),
  91. mUserData(inSettings.mUserData)
  92. {
  93. }
  94. /// Virtual destructor
  95. virtual ~Constraint() = default;
  96. /// Get the type of a constraint
  97. virtual EConstraintType GetType() const { return EConstraintType::Constraint; }
  98. /// Get the sub type of a constraint
  99. virtual EConstraintSubType GetSubType() const = 0;
  100. /// Override for the number of solver velocity iterations to run, the total amount of iterations is the max of PhysicsSettings::mNumVelocitySteps and this for all constraints in the island.
  101. void SetNumVelocityStepsOverride(int inN) { mNumVelocityStepsOverride = inN; }
  102. int GetNumVelocityStepsOverride() const { return mNumVelocityStepsOverride; }
  103. /// Override for the number of position velocity iterations to run, the total amount of iterations is the max of PhysicsSettings::mNumPositionSteps and this for all constraints in the island.
  104. void SetNumPositionStepsOverride(int inN) { mNumPositionStepsOverride = inN; }
  105. int GetNumPositionStepsOverride() const { return mNumPositionStepsOverride; }
  106. /// Enable / disable this constraint. This can e.g. be used to implement a breakable constraint by detecting that the constraint impulse
  107. /// (see e.g. PointConstraint::GetTotalLambdaPosition) went over a certain limit and then disabling the constraint.
  108. /// Note that although a disabled constraint will not affect the simulation in any way anymore, it does incur some processing overhead.
  109. /// Alternatively you can remove a constraint from the constraint manager (which may be more costly if you want to disable the constraint for a short while).
  110. void SetEnabled(bool inEnabled) { mEnabled = inEnabled; }
  111. /// Test if a constraint is enabled.
  112. bool GetEnabled() const { return mEnabled; }
  113. /// Access to the user data, can be used for anything by the application
  114. uint64 GetUserData() const { return mUserData; }
  115. void SetUserData(uint64 inUserData) { mUserData = inUserData; }
  116. /// Notify the constraint that the shape of a body has changed and that its center of mass has moved by inDeltaCOM.
  117. /// Bodies don't know which constraints are connected to them so the user is responsible for notifying the relevant constraints when a body changes.
  118. /// @param inBodyID ID of the body that has changed
  119. /// @param inDeltaCOM The delta of the center of mass of the body (shape->GetCenterOfMass() - shape_before_change->GetCenterOfMass())
  120. virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) = 0;
  121. ///@name Solver interface
  122. ///@{
  123. virtual bool IsActive() const { return mEnabled; }
  124. virtual void SetupVelocityConstraint(float inDeltaTime) = 0;
  125. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) = 0;
  126. virtual bool SolveVelocityConstraint(float inDeltaTime) = 0;
  127. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) = 0;
  128. ///@}
  129. /// Link bodies that are connected by this constraint in the island builder
  130. virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) = 0;
  131. /// Link bodies that are connected by this constraint in the same split. Returns the split index.
  132. virtual uint BuildIslandSplits(LargeIslandSplitter &ioSplitter) const = 0;
  133. #ifdef JPH_DEBUG_RENDERER
  134. // Drawing interface
  135. virtual void DrawConstraint(DebugRenderer *inRenderer) const = 0;
  136. virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const { }
  137. virtual void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const { }
  138. /// Size of constraint when drawing it through the debug renderer
  139. float GetDrawConstraintSize() const { return mDrawConstraintSize; }
  140. void SetDrawConstraintSize(float inSize) { mDrawConstraintSize = inSize; }
  141. #endif // JPH_DEBUG_RENDERER
  142. /// Saving state for replay
  143. virtual void SaveState(StateRecorder &inStream) const;
  144. /// Restoring state for replay
  145. virtual void RestoreState(StateRecorder &inStream);
  146. /// Debug function to convert a constraint to its settings, note that this will not save to which bodies the constraint is connected to
  147. virtual Ref<ConstraintSettings> GetConstraintSettings() const = 0;
  148. protected:
  149. /// Helper function to copy settings back to constraint settings for this base class
  150. void ToConstraintSettings(ConstraintSettings &outSettings) const;
  151. #ifdef JPH_DEBUG_RENDERER
  152. /// Size of constraint when drawing it through the debug renderer
  153. float mDrawConstraintSize;
  154. #endif // JPH_DEBUG_RENDERER
  155. private:
  156. friend class ConstraintManager;
  157. /// Index that indicates this constraint is not in the constraint manager
  158. static constexpr uint32 cInvalidConstraintIndex = 0xffffffff;
  159. /// Index in the mConstraints list of the ConstraintManager for easy finding
  160. uint32 mConstraintIndex = cInvalidConstraintIndex;
  161. /// Override for the number of solver velocity iterations to run, the total amount of iterations is the max of PhysicsSettings::mNumVelocitySteps and this for all constraints in the island.
  162. int mNumVelocityStepsOverride = 0;
  163. /// Override for the number of position velocity iterations to run, the total amount of iterations is the max of PhysicsSettings::mNumPositionSteps and this for all constraints in the island.
  164. int mNumPositionStepsOverride = 0;
  165. /// If this constraint is currently enabled
  166. bool mEnabled = true;
  167. /// User data value (can be used by application)
  168. uint64 mUserData;
  169. };
  170. JPH_NAMESPACE_END