RackAndPinionConstraint.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/RackAndPinionConstraintPart.h>
  7. JPH_NAMESPACE_BEGIN
  8. /// Rack and pinion constraint (slider & gear) settings
  9. class JPH_EXPORT RackAndPinionConstraintSettings final : public TwoBodyConstraintSettings
  10. {
  11. public:
  12. JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, RackAndPinionConstraintSettings)
  13. // See: ConstraintSettings::SaveBinaryState
  14. virtual void SaveBinaryState(StreamOut &inStream) const override;
  15. /// Create an an instance of this constraint.
  16. /// Body1 should be the pinion (gear) and body 2 the rack (slider).
  17. virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
  18. /// Defines the ratio between the rotation of the pinion and the translation of the rack.
  19. /// The ratio is defined as: PinionRotation(t) = ratio * RackTranslation(t)
  20. /// @param inNumTeethRack Number of teeth that the rack has
  21. /// @param inRackLength Length of the rack
  22. /// @param inNumTeethPinion Number of teeth the pinion has
  23. void SetRatio(int inNumTeethRack, float inRackLength, int inNumTeethPinion)
  24. {
  25. mRatio = 2.0f * JPH_PI * inNumTeethRack / (inRackLength * inNumTeethPinion);
  26. }
  27. /// This determines in which space the constraint is setup, all properties below should be in the specified space
  28. EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
  29. /// Body 1 (pinion) constraint reference frame (space determined by mSpace).
  30. Vec3 mHingeAxis = Vec3::sAxisX();
  31. /// Body 2 (rack) constraint reference frame (space determined by mSpace)
  32. Vec3 mSliderAxis = Vec3::sAxisX();
  33. /// Ratio between the rack and pinion, see SetRatio.
  34. float mRatio = 1.0f;
  35. protected:
  36. // See: ConstraintSettings::RestoreBinaryState
  37. virtual void RestoreBinaryState(StreamIn &inStream) override;
  38. };
  39. /// A rack and pinion constraint constrains the rotation of body1 to the translation of body 2.
  40. /// Note that this constraint needs to be used in conjunction with a hinge constraint for body 1 and a slider constraint for body 2.
  41. class JPH_EXPORT RackAndPinionConstraint final : public TwoBodyConstraint
  42. {
  43. public:
  44. JPH_OVERRIDE_NEW_DELETE
  45. /// Construct gear constraint
  46. RackAndPinionConstraint(Body &inBody1, Body &inBody2, const RackAndPinionConstraintSettings &inSettings);
  47. // Generic interface of a constraint
  48. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::RackAndPinion; }
  49. virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override { /* Nothing */ }
  50. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  51. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  52. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  53. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  54. #ifdef JPH_DEBUG_RENDERER
  55. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  56. #endif // JPH_DEBUG_RENDERER
  57. virtual void SaveState(StateRecorder &inStream) const override;
  58. virtual void RestoreState(StateRecorder &inStream) override;
  59. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  60. // See: TwoBodyConstraint
  61. virtual Mat44 GetConstraintToBody1Matrix() const override;
  62. virtual Mat44 GetConstraintToBody2Matrix() const override;
  63. /// The constraints that constrain the rack and pinion (a slider and a hinge), optional and used to calculate the position error and fix numerical drift.
  64. void SetConstraints(const Constraint *inPinion, const Constraint *inRack) { mPinionConstraint = inPinion; mRackConstraint = inRack; }
  65. ///@name Get Lagrange multiplier from last physics update (the linear/angular impulse applied to satisfy the constraint)
  66. inline float GetTotalLambda() const { return mRackAndPinionConstraintPart.GetTotalLambda(); }
  67. private:
  68. // Internal helper function to calculate the values below
  69. void CalculateConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);
  70. // CONFIGURATION PROPERTIES FOLLOW
  71. // Local space hinge axis
  72. Vec3 mLocalSpaceHingeAxis;
  73. // Local space sliding direction
  74. Vec3 mLocalSpaceSliderAxis;
  75. // Ratio between rack and pinion
  76. float mRatio;
  77. // The constraints that constrain the rack and pinion (a slider and a hinge), optional and used to calculate the position error and fix numerical drift.
  78. RefConst<Constraint> mPinionConstraint;
  79. RefConst<Constraint> mRackConstraint;
  80. // RUN TIME PROPERTIES FOLLOW
  81. // World space hinge axis
  82. Vec3 mWorldSpaceHingeAxis;
  83. // World space sliding direction
  84. Vec3 mWorldSpaceSliderAxis;
  85. // The constraint parts
  86. RackAndPinionConstraintPart mRackAndPinionConstraintPart;
  87. };
  88. JPH_NAMESPACE_END