RackAndPinionConstraint.h 4.1 KB

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