PathConstraint.h 9.3 KB

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