Constraint.h 5.9 KB

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