PathConstraint.h 8.7 KB

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