Constraint.h 4.5 KB

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