GearConstraint.h 4.4 KB

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