FixedConstraint.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/ConstraintPart/RotationEulerConstraintPart.h>
  6. #include <Jolt/Physics/Constraints/ConstraintPart/PointConstraintPart.h>
  7. JPH_NAMESPACE_BEGIN
  8. /// Fixed constraint settings, used to create a fixed constraint
  9. class FixedConstraintSettings final : public TwoBodyConstraintSettings
  10. {
  11. public:
  12. JPH_DECLARE_SERIALIZABLE_VIRTUAL(FixedConstraintSettings)
  13. // See: ConstraintSettings::SaveBinaryState
  14. virtual void SaveBinaryState(StreamOut &inStream) const override;
  15. /// Create an an instance of this constraint
  16. virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
  17. /// This determines in which space the constraint is setup, all properties below should be in the specified space
  18. EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
  19. /// When mSpace is WorldSpace mPoint1 and mPoint2 can be automatically calculated based on the positions of the bodies when the constraint is created (they will be fixated in their current relative position/orientation). Set this to false if you want to supply the attachment points yourself.
  20. bool mAutoDetectPoint = false;
  21. /// Body 1 constraint reference frame (space determined by mSpace)
  22. Vec3 mPoint1 = Vec3::sZero();
  23. Vec3 mAxisX1 = Vec3::sAxisX();
  24. Vec3 mAxisY1 = Vec3::sAxisY();
  25. /// Body 2 constraint reference frame (space determined by mSpace)
  26. Vec3 mPoint2 = Vec3::sZero();
  27. Vec3 mAxisX2 = Vec3::sAxisX();
  28. Vec3 mAxisY2 = Vec3::sAxisY();
  29. protected:
  30. // See: ConstraintSettings::RestoreBinaryState
  31. virtual void RestoreBinaryState(StreamIn &inStream) override;
  32. };
  33. /// A fixed constraint welds two bodies together removing all degrees of freedom between them.
  34. /// This variant uses euler angles for the rotation constraint.
  35. class FixedConstraint final : public TwoBodyConstraint
  36. {
  37. public:
  38. JPH_OVERRIDE_NEW_DELETE
  39. /// Constructor
  40. FixedConstraint(Body &inBody1, Body &inBody2, const FixedConstraintSettings &inSettings);
  41. // Generic interface of a constraint
  42. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Fixed; }
  43. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  44. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  45. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  46. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  47. #ifdef JPH_DEBUG_RENDERER
  48. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  49. #endif // JPH_DEBUG_RENDERER
  50. virtual void SaveState(StateRecorder &inStream) const override;
  51. virtual void RestoreState(StateRecorder &inStream) override;
  52. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  53. // See: TwoBodyConstraint
  54. virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition1); }
  55. virtual Mat44 GetConstraintToBody2Matrix() const override { return Mat44::sRotationTranslation(mInvInitialOrientation, mLocalSpacePosition2); }
  56. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  57. inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
  58. inline Vec3 GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
  59. private:
  60. // CONFIGURATION PROPERTIES FOLLOW
  61. // Local space constraint positions
  62. Vec3 mLocalSpacePosition1;
  63. Vec3 mLocalSpacePosition2;
  64. // Inverse of initial rotation from body 1 to body 2 in body 1 space
  65. Quat mInvInitialOrientation;
  66. // RUN TIME PROPERTIES FOLLOW
  67. // The constraint parts
  68. RotationEulerConstraintPart mRotationConstraintPart;
  69. PointConstraintPart mPointConstraintPart;
  70. };
  71. JPH_NAMESPACE_END