GearConstraint.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/GearConstraintPart.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Gear constraint settings
  8. class GearConstraintSettings final : public TwoBodyConstraintSettings
  9. {
  10. public:
  11. JPH_DECLARE_SERIALIZABLE_VIRTUAL(GearConstraintSettings)
  12. // See: ConstraintSettings::SaveBinaryState
  13. virtual void SaveBinaryState(StreamOut &inStream) const override;
  14. /// Create an an instance of this constraint.
  15. virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
  16. /// Defines the ratio between the rotation of both gears
  17. /// The ratio is defined as: Gear1Rotation(t) = -ratio * Gear2Rotation(t)
  18. /// @param inNumTeethGear1 Number of teeth that body 1 has
  19. /// @param inNumTeethGear2 Number of teeth that body 2 has
  20. void SetRatio(int inNumTeethGear1, int inNumTeethGear2)
  21. {
  22. mRatio = float(inNumTeethGear2) / float(inNumTeethGear1);
  23. }
  24. /// This determines in which space the constraint is setup, all properties below should be in the specified space
  25. EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
  26. /// Body 1 constraint reference frame (space determined by mSpace).
  27. Vec3 mHingeAxis1 = Vec3::sAxisX();
  28. /// Body 2 constraint reference frame (space determined by mSpace)
  29. Vec3 mHingeAxis2 = Vec3::sAxisX();
  30. /// Ratio between both gears, see SetRatio.
  31. float mRatio = 1.0f;
  32. protected:
  33. // See: ConstraintSettings::RestoreBinaryState
  34. virtual void RestoreBinaryState(StreamIn &inStream) override;
  35. };
  36. /// A gear constraint constrains the rotation of body1 to the rotation of body 2 using a gear.
  37. /// Note that this constraint needs to be used in conjunction with a two hinge constraints.
  38. class GearConstraint final : public TwoBodyConstraint
  39. {
  40. public:
  41. JPH_OVERRIDE_NEW_DELETE
  42. /// Construct gear constraint
  43. GearConstraint(Body &inBody1, Body &inBody2, const GearConstraintSettings &inSettings);
  44. // Generic interface of a constraint
  45. virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Gear; }
  46. virtual void SetupVelocityConstraint(float inDeltaTime) override;
  47. virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
  48. virtual bool SolveVelocityConstraint(float inDeltaTime) override;
  49. virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
  50. #ifdef JPH_DEBUG_RENDERER
  51. virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
  52. #endif // JPH_DEBUG_RENDERER
  53. virtual void SaveState(StateRecorder &inStream) const override;
  54. virtual void RestoreState(StateRecorder &inStream) override;
  55. virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
  56. // See: TwoBodyConstraint
  57. virtual Mat44 GetConstraintToBody1Matrix() const override;
  58. virtual Mat44 GetConstraintToBody2Matrix() const override;
  59. /// The constraints that constrain both gears (2 hinges), optional and used to calculate the rotation error and fix numerical drift.
  60. void SetConstraints(const Constraint *inGear1, const Constraint *inGear2) { mGear1Constraint = inGear1; mGear2Constraint = inGear2; }
  61. ///@name Get Lagrange multiplier from last physics update (relates to how much force/torque was applied to satisfy the constraint)
  62. inline float GetTotalLambda() const { return mGearConstraintPart.GetTotalLambda(); }
  63. private:
  64. // Internal helper function to calculate the values below
  65. void CalculateConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);
  66. // CONFIGURATION PROPERTIES FOLLOW
  67. // Local space hinge axis for body 1
  68. Vec3 mLocalSpaceHingeAxis1;
  69. // Local space hinge axis for body 2
  70. Vec3 mLocalSpaceHingeAxis2;
  71. // Ratio between gear 1 and 2
  72. float mRatio;
  73. // The constraints that constrain both gears (2 hinges), optional and used to calculate the rotation error and fix numerical drift.
  74. RefConst<Constraint> mGear1Constraint;
  75. RefConst<Constraint> mGear2Constraint;
  76. // RUN TIME PROPERTIES FOLLOW
  77. // World space hinge axis for body 1
  78. Vec3 mWorldSpaceHingeAxis1;
  79. // World space hinge axis for body 2
  80. Vec3 mWorldSpaceHingeAxis2;
  81. // The constraint parts
  82. GearConstraintPart mGearConstraintPart;
  83. };
  84. JPH_NAMESPACE_END