Constraint.h 7.7 KB

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