Constraint.h 8.0 KB

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