Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
RackAndPinionConstraint.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
9
11
14{
15public:
17
18 // See: ConstraintSettings::SaveBinaryState
19 virtual void SaveBinaryState(StreamOut &inStream) const override;
20
23 virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
24
30 void SetRatio(int inNumTeethRack, float inRackLength, int inNumTeethPinion)
31 {
32 mRatio = 2.0f * JPH_PI * inNumTeethRack / (inRackLength * inNumTeethPinion);
33 }
34
36 EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
37
40
43
45 float mRatio = 1.0f;
46
47protected:
48 // See: ConstraintSettings::RestoreBinaryState
49 virtual void RestoreBinaryState(StreamIn &inStream) override;
50};
51
55{
56public:
58
60 RackAndPinionConstraint(Body &inBody1, Body &inBody2, const RackAndPinionConstraintSettings &inSettings);
61
62 // Generic interface of a constraint
63 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::RackAndPinion; }
64 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override { /* Nothing */ }
65 virtual void SetupVelocityConstraint(float inDeltaTime) override;
66 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
67 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
68 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
69#ifdef JPH_DEBUG_RENDERER
70 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
71#endif // JPH_DEBUG_RENDERER
72 virtual void SaveState(StateRecorder &inStream) const override;
73 virtual void RestoreState(StateRecorder &inStream) override;
74 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
75
76 // See: TwoBodyConstraint
77 virtual Mat44 GetConstraintToBody1Matrix() const override;
78 virtual Mat44 GetConstraintToBody2Matrix() const override;
79
81 void SetConstraints(const Constraint *inPinion, const Constraint *inRack) { mPinionConstraint = inPinion; mRackConstraint = inRack; }
82
84 inline float GetTotalLambda() const { return mRackAndPinionConstraintPart.GetTotalLambda(); }
85
86private:
87 // Internal helper function to calculate the values below
88 void CalculateConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);
89
90 // CONFIGURATION PROPERTIES FOLLOW
91
92 // Local space hinge axis
93 Vec3 mLocalSpaceHingeAxis;
94
95 // Local space sliding direction
96 Vec3 mLocalSpaceSliderAxis;
97
98 // Ratio between rack and pinion
99 float mRatio;
100
101 // 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.
102 RefConst<Constraint> mPinionConstraint;
103 RefConst<Constraint> mRackConstraint;
104
105 // RUN TIME PROPERTIES FOLLOW
106
107 // World space hinge axis
108 Vec3 mWorldSpaceHingeAxis;
109
110 // World space sliding direction
111 Vec3 mWorldSpaceSliderAxis;
112
113 // The constraint parts
114 RackAndPinionConstraintPart mRackAndPinionConstraintPart;
115};
116
EConstraintSpace
Certain constraints support setting them up in local or world space. This governs what is used.
Definition: Constraint.h:58
EConstraintSubType
Enum to identify constraint sub type.
Definition: Constraint.h:34
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(class_name)
Definition: SerializableObject.h:100
Definition: Body.h:33
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition: BodyID.h:13
Base class for all physics constraints. A constraint removes one or more degrees of freedom for a rig...
Definition: Constraint.h:99
Simple triangle renderer for debugging purposes.
Definition: DebugRenderer.h:25
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
Definition: RackAndPinionConstraint.h:55
virtual Mat44 GetConstraintToBody2Matrix() const override
Calculates the transform that transforms from constraint space to body 2 space. The first column of t...
Definition: RackAndPinionConstraint.cpp:178
virtual Mat44 GetConstraintToBody1Matrix() const override
Calculates the transform that transforms from constraint space to body 1 space. The first column of t...
Definition: RackAndPinionConstraint.cpp:172
float GetTotalLambda() const
Definition: RackAndPinionConstraint.h:84
virtual bool SolveVelocityConstraint(float inDeltaTime) override
Definition: RackAndPinionConstraint.cpp:92
virtual void RestoreState(StateRecorder &inStream) override
Restoring state for replay.
Definition: RackAndPinionConstraint.cpp:154
virtual void SetupVelocityConstraint(float inDeltaTime) override
Definition: RackAndPinionConstraint.cpp:78
virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override
Definition: RackAndPinionConstraint.cpp:86
virtual void DrawConstraint(DebugRenderer *inRenderer) const override
Definition: RackAndPinionConstraint.cpp:135
virtual void SaveState(StateRecorder &inStream) const override
Saving state for replay.
Definition: RackAndPinionConstraint.cpp:147
void SetConstraints(const Constraint *inPinion, const Constraint *inRack)
The constraints that constrain the rack and pinion (a slider and a hinge), optional and used to calcu...
Definition: RackAndPinionConstraint.h:81
virtual Ref< ConstraintSettings > GetConstraintSettings() const override
Debug function to convert a constraint to its settings, note that this will not save to which bodies ...
Definition: RackAndPinionConstraint.cpp:161
virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override
Definition: RackAndPinionConstraint.h:64
virtual EConstraintSubType GetSubType() const override
Get the sub type of a constraint.
Definition: RackAndPinionConstraint.h:63
virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override
Definition: RackAndPinionConstraint.cpp:97
Definition: RackAndPinionConstraintPart.h:40
float GetTotalLambda() const
Return lagrange multiplier.
Definition: RackAndPinionConstraintPart.h:126
Rack and pinion constraint (slider & gear) settings.
Definition: RackAndPinionConstraint.h:14
EConstraintSpace mSpace
This determines in which space the constraint is setup, all properties below should be in the specifi...
Definition: RackAndPinionConstraint.h:36
Vec3 mHingeAxis
Body 1 (pinion) constraint reference frame (space determined by mSpace).
Definition: RackAndPinionConstraint.h:39
virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override
Definition: RackAndPinionConstraint.cpp:50
virtual void RestoreBinaryState(StreamIn &inStream) override
This function should not be called directly, it is used by sRestoreFromBinaryState.
Definition: RackAndPinionConstraint.cpp:40
void SetRatio(int inNumTeethRack, float inRackLength, int inNumTeethPinion)
Definition: RackAndPinionConstraint.h:30
virtual void SaveBinaryState(StreamOut &inStream) const override
Saves the contents of the constraint settings in binary form to inStream.
Definition: RackAndPinionConstraint.cpp:30
Vec3 mSliderAxis
Body 2 (rack) constraint reference frame (space determined by mSpace)
Definition: RackAndPinionConstraint.h:42
float mRatio
Ratio between the rack and pinion, see SetRatio.
Definition: RackAndPinionConstraint.h:45
Definition: Reference.h:154
Definition: Reference.h:101
Definition: StateRecorder.h:15
Simple binary input stream.
Definition: StreamIn.h:11
Simple binary output stream.
Definition: StreamOut.h:11
Base class for all constraints that involve 2 bodies. Body1 is usually considered the parent,...
Definition: TwoBodyConstraint.h:27
Base class for settings for all constraints that involve 2 bodies.
Definition: TwoBodyConstraint.h:16
Definition: Vec3.h:16
static JPH_INLINE Vec3 sAxisX()
Vectors with the principal axis.
Definition: Vec3.h:51