Constraint.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Core/Reference.h>
  5. #include <Core/NonCopyable.h>
  6. #include <Core/Result.h>
  7. #include <ObjectStream/SerializableObject.h>
  8. namespace JPH {
  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. Fixed,
  21. Point,
  22. Hinge,
  23. Slider,
  24. Distance,
  25. Cone,
  26. SwingTwist,
  27. SixDOF,
  28. Path,
  29. Vehicle,
  30. /// User defined constraint types start here
  31. User1,
  32. User2,
  33. User3,
  34. User4
  35. };
  36. /// Certain constraints support setting them up in local or world space. This governs what is used.
  37. enum class EConstraintSpace
  38. {
  39. 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!
  40. WorldSpace, ///< All constraint properties are specified in world space
  41. };
  42. /// Class used to store the configuration of a constraint. Allows run-time creation of constraints.
  43. class ConstraintSettings : public SerializableObject, public RefTarget<ConstraintSettings>, public NonCopyable
  44. {
  45. public:
  46. JPH_DECLARE_SERIALIZABLE_VIRTUAL(ConstraintSettings)
  47. using ConstraintResult = Result<Ref<ConstraintSettings>>;
  48. /// Saves the contents of the constraint settings in binary form to inStream.
  49. virtual void SaveBinaryState(StreamOut &inStream) const;
  50. /// Creates a constraint of the correct type and restores its contents from the binary stream inStream.
  51. static ConstraintResult sRestoreFromBinaryState(StreamIn &inStream);
  52. /// Size of constraint when drawing it through the debug renderer
  53. float mDrawConstraintSize = 1.0f;
  54. protected:
  55. /// This function should not be called directly, it is used by sRestoreFromBinaryState.
  56. virtual void RestoreBinaryState(StreamIn &inStream);
  57. };
  58. /// Base class for all physics constraints. A constraint removes one or more degrees of freedom for a rigid body.
  59. class Constraint : public RefTarget<Constraint>
  60. {
  61. public:
  62. /// Constructor
  63. explicit Constraint([[maybe_unused]] const ConstraintSettings &inSettings)
  64. #ifdef JPH_DEBUG_RENDERER
  65. : mDrawConstraintSize(inSettings.mDrawConstraintSize)
  66. #endif // JPH_DEBUG_RENDERER
  67. {
  68. }
  69. /// Virtual destructor
  70. virtual ~Constraint() = default;
  71. /// Get the type of a constraint
  72. virtual EConstraintType GetType() const = 0;
  73. /// Enable / disable this constraint. This can e.g. be used to implement a breakable constraint by detecting that the constraint impulse went over a certain limit and then disabling the constraint.
  74. /// Note that although a disabled constraint will not affect the simulation in any way anymore, it does incur some processing overhead.
  75. /// 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).
  76. void SetEnabled(bool inEnabled) { mEnabled = inEnabled; }
  77. /// Test if a constraint is enabled.
  78. bool GetEnabled() const { return mEnabled; }
  79. ///@name Solver interface
  80. ///@{
  81. virtual bool IsActive() const { return mEnabled; }
  82. virtual void SetupVelocityConstraint(float inDeltaTime) = 0;
  83. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) = 0;
  84. virtual bool SolveVelocityConstraint(float inDeltaTime) = 0;
  85. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) = 0;
  86. ///@}
  87. /// Link bodies that are connected by this constraint in the island builder
  88. virtual void BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager) = 0;
  89. #ifdef JPH_DEBUG_RENDERER
  90. // Drawing interface
  91. virtual void DrawConstraint(DebugRenderer *inRenderer) const = 0;
  92. virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const { }
  93. virtual void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const { }
  94. /// Size of constraint when drawing it through the debug renderer
  95. float GetDrawConstraintSize() const { return mDrawConstraintSize; }
  96. void SetDrawConstraintSize(float inSize) { mDrawConstraintSize = inSize; }
  97. #endif // JPH_DEBUG_RENDERER
  98. /// Saving state for replay
  99. virtual void SaveState(StateRecorder &inStream) const;
  100. /// Restoring state for replay
  101. virtual void RestoreState(StateRecorder &inStream);
  102. protected:
  103. #ifdef JPH_DEBUG_RENDERER
  104. /// Size of constraint when drawing it through the debug renderer
  105. float mDrawConstraintSize;
  106. #endif // JPH_DEBUG_RENDERER
  107. private:
  108. friend class ConstraintManager;
  109. /// Index that indicates this constraint is not in the constraint manager
  110. static constexpr uint32 cInvalidConstraintIndex = 0xffffffff;
  111. /// Index in the mConstraints list of the ConstraintManager for easy finding
  112. uint32 mConstraintIndex = cInvalidConstraintIndex;
  113. /// If this constraint is currently enabled
  114. bool mEnabled = true;
  115. };
  116. } // JPH