FixedConstraint.h 4.1 KB

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