PathConstraint.h 8.6 KB

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