PathConstraint.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/Physics/Constraints/TwoBodyConstraint.h>
  6. #include <Jolt/Physics/Constraints/PathConstraintPath.h>
  7. #include <Jolt/Physics/Constraints/MotorSettings.h>
  8. #include <Jolt/Physics/Constraints/ConstraintPart/AxisConstraintPart.h>
  9. #include <Jolt/Physics/Constraints/ConstraintPart/DualAxisConstraintPart.h>
  10. #include <Jolt/Physics/Constraints/ConstraintPart/HingeRotationConstraintPart.h>
  11. #include <Jolt/Physics/Constraints/ConstraintPart/RotationEulerConstraintPart.h>
  12. JPH_NAMESPACE_BEGIN
  13. JPH_SUPPRESS_WARNING_PUSH
  14. JPH_GCC_SUPPRESS_WARNING("-Wshadow") // GCC complains about the 'Free' value conflicting with the 'Free' method
  15. /// How to constrain the rotation of the body to a PathConstraint
  16. enum class EPathRotationConstraintType
  17. {
  18. Free, ///< Do not constrain the rotation of the body at all
  19. ConstrainAroundTangent, ///< Only allow rotation around the tangent vector (following the path)
  20. ConstrainAroundNormal, ///< Only allow rotation around the normal vector (perpendicular to the path)
  21. ConstrainAroundBinormal, ///< Only allow rotation around the binormal vector (perpendicular to the path)
  22. ConstrainToPath, ///< Fully constrain the rotation of body 2 to the path (following the tangent and normal of the path)
  23. FullyConstrained, ///< Fully constrain the rotation of the body 2 to the rotation of body 1
  24. };
  25. JPH_SUPPRESS_WARNING_POP
  26. /// Path constraint settings, used to constrain the degrees of freedom between two bodies to a path
  27. ///
  28. /// The requirements of the path are that:
  29. /// * Tangent, normal and bi-normal form an orthonormal basis with: tangent cross bi-normal = normal
  30. /// * The path points along the tangent vector
  31. /// * The path is continuous so doesn't contain any sharp corners
  32. ///
  33. /// The reason for all this is that the constraint acts like a slider constraint with the sliding axis being the tangent vector (the assumption here is that delta time will be small enough so that the path is linear for that delta time).
  34. class JPH_EXPORT PathConstraintSettings final : public TwoBodyConstraintSettings
  35. {
  36. JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, PathConstraintSettings)
  37. public:
  38. // See: ConstraintSettings::SaveBinaryState
  39. virtual void SaveBinaryState(StreamOut &inStream) const override;
  40. /// Create an instance of this constraint
  41. virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
  42. /// The path that constrains the two bodies
  43. RefConst<PathConstraintPath> mPath;
  44. /// The position of the path start relative to world transform of body 1
  45. Vec3 mPathPosition = Vec3::sZero();
  46. /// The rotation of the path start relative to world transform of body 1
  47. Quat mPathRotation = Quat::sIdentity();
  48. /// The fraction along the path that corresponds to the initial position of body 2. Usually this is 0, the beginning of the path. But if you want to start an object halfway the path you can calculate this with mPath->GetClosestPoint(point on path to attach body to).
  49. float mPathFraction = 0.0f;
  50. /// Maximum amount of friction force to apply (N) when not driven by a motor.
  51. float mMaxFrictionForce = 0.0f;
  52. /// In case the constraint is powered, this determines the motor settings along the path
  53. MotorSettings mPositionMotorSettings;
  54. /// How to constrain the rotation of the body to the path
  55. EPathRotationConstraintType mRotationConstraintType = EPathRotationConstraintType::Free;
  56. protected:
  57. // See: ConstraintSettings::RestoreBinaryState
  58. virtual void RestoreBinaryState(StreamIn &inStream) override;
  59. };
  60. /// Path constraint, used to constrain the degrees of freedom between two bodies to a path
  61. class JPH_EXPORT PathConstraint final : public TwoBodyConstraint
  62. {
  63. public:
  64. JPH_OVERRIDE_NEW_DELETE
  65. /// Construct point constraint
  66. PathConstraint(Body &inBody1, Body &inBody2, const PathConstraintSettings &inSettings);
  67. // Generic interface of a constraint
  68. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Path; }
  69. virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
  70. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  71. virtual void ResetWarmStart() override;
  72. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  73. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  74. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  75. #ifdef JPH_DEBUG_RENDERER
  76. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  77. #endif // JPH_DEBUG_RENDERER
  78. virtual void SaveState(StateRecorder &inStream) const override;
  79. virtual void RestoreState(StateRecorder &inStream) override;
  80. virtual bool IsActive() const override { return TwoBodyConstraint::IsActive() && mPath != nullptr; }
  81. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  82. // See: TwoBodyConstraint
  83. virtual Mat44 GetConstraintToBody1Matrix() const override { return mPathToBody1; }
  84. virtual Mat44 GetConstraintToBody2Matrix() const override { return mPathToBody2; }
  85. /// Update the path for this constraint
  86. void SetPath(const PathConstraintPath *inPath, float inPathFraction);
  87. /// Access to the current path
  88. const PathConstraintPath * GetPath() const { return mPath; }
  89. /// Access to the current fraction along the path e [0, GetPath()->GetMaxPathFraction()]
  90. float GetPathFraction() const { return mPathFraction; }
  91. /// Friction control
  92. void SetMaxFrictionForce(float inFrictionForce) { mMaxFrictionForce = inFrictionForce; }
  93. float GetMaxFrictionForce() const { return mMaxFrictionForce; }
  94. /// Position motor settings
  95. MotorSettings & GetPositionMotorSettings() { return mPositionMotorSettings; }
  96. const MotorSettings & GetPositionMotorSettings() const { return mPositionMotorSettings; }
  97. // Position motor controls (drives body 2 along the path)
  98. void SetPositionMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mPositionMotorSettings.IsValid()); mPositionMotorState = inState; }
  99. EMotorState GetPositionMotorState() const { return mPositionMotorState; }
  100. void SetTargetVelocity(float inVelocity) { mTargetVelocity = inVelocity; }
  101. float GetTargetVelocity() const { return mTargetVelocity; }
  102. void SetTargetPathFraction(float inFraction) { JPH_ASSERT(mPath->IsLooping() || (inFraction >= 0.0f && inFraction <= mPath->GetPathMaxFraction())); mTargetPathFraction = inFraction; }
  103. float GetTargetPathFraction() const { return mTargetPathFraction; }
  104. ///@name Get Lagrange multiplier from last physics update (the linear/angular impulse applied to satisfy the constraint)
  105. inline Vector<2> GetTotalLambdaPosition() const { return mPositionConstraintPart.GetTotalLambda(); }
  106. inline float GetTotalLambdaPositionLimits() const { return mPositionLimitsConstraintPart.GetTotalLambda(); }
  107. inline float GetTotalLambdaMotor() const { return mPositionMotorConstraintPart.GetTotalLambda(); }
  108. inline Vector<2> GetTotalLambdaRotationHinge() const { return mHingeConstraintPart.GetTotalLambda(); }
  109. inline Vec3 GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
  110. private:
  111. // Internal helper function to calculate the values below
  112. void CalculateConstraintProperties(float inDeltaTime);
  113. // CONFIGURATION PROPERTIES FOLLOW
  114. RefConst<PathConstraintPath> mPath; ///< The path that attaches the two bodies
  115. Mat44 mPathToBody1; ///< Transform that takes a quantity from path space to body 1 center of mass space
  116. Mat44 mPathToBody2; ///< Transform that takes a quantity from path space to body 2 center of mass space
  117. EPathRotationConstraintType mRotationConstraintType; ///< How to constrain the rotation of the path
  118. // Friction
  119. float mMaxFrictionForce;
  120. // Motor controls
  121. MotorSettings mPositionMotorSettings;
  122. EMotorState mPositionMotorState = EMotorState::Off;
  123. float mTargetVelocity = 0.0f;
  124. float mTargetPathFraction = 0.0f;
  125. // RUN TIME PROPERTIES FOLLOW
  126. // Positions where the point constraint acts on in world space
  127. Vec3 mR1;
  128. Vec3 mR2;
  129. // X2 + R2 - X1 - R1
  130. Vec3 mU;
  131. // World space path tangent
  132. Vec3 mPathTangent;
  133. // Normals to the path tangent
  134. Vec3 mPathNormal;
  135. Vec3 mPathBinormal;
  136. // Inverse of initial rotation from body 1 to body 2 in body 1 space (only used when rotation constraint type is FullyConstrained)
  137. Quat mInvInitialOrientation;
  138. // Current fraction along the path where body 2 is attached
  139. float mPathFraction = 0.0f;
  140. // Translation constraint parts
  141. DualAxisConstraintPart mPositionConstraintPart; ///< Constraint part that keeps the movement along the tangent of the path
  142. AxisConstraintPart mPositionLimitsConstraintPart; ///< Constraint part that prevents movement beyond the beginning and end of the path
  143. AxisConstraintPart mPositionMotorConstraintPart; ///< Constraint to drive the object along the path or to apply friction
  144. // Rotation constraint parts
  145. HingeRotationConstraintPart mHingeConstraintPart; ///< Constraint part that removes 2 degrees of rotation freedom
  146. RotationEulerConstraintPart mRotationConstraintPart; ///< Constraint part that removes all rotational freedom
  147. };
  148. JPH_NAMESPACE_END