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{
16
17public:
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
37
39 Vec3 mHingeAxis = Vec3::sAxisX();
40
42 Vec3 mSliderAxis = Vec3::sAxisX();
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
64 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override { /* Nothing */ }
65 virtual void SetupVelocityConstraint(float inDeltaTime) override;
66 virtual void ResetWarmStart() override;
67 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
68 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
69 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
70#ifdef JPH_DEBUG_RENDERER
71 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
72#endif // JPH_DEBUG_RENDERER
73 virtual void SaveState(StateRecorder &inStream) const override;
74 virtual void RestoreState(StateRecorder &inStream) override;
75 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
76
77 // See: TwoBodyConstraint
78 virtual Mat44 GetConstraintToBody1Matrix() const override;
79 virtual Mat44 GetConstraintToBody2Matrix() const override;
80
82 void SetConstraints(const Constraint *inPinion, const Constraint *inRack) { mPinionConstraint = inPinion; mRackConstraint = inRack; }
83
85 inline float GetTotalLambda() const { return mRackAndPinionConstraintPart.GetTotalLambda(); }
86
87private:
88 // Internal helper function to calculate the values below
89 void CalculateConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);
90
91 // CONFIGURATION PROPERTIES FOLLOW
92
93 // Local space hinge axis
94 Vec3 mLocalSpaceHingeAxis;
95
96 // Local space sliding direction
97 Vec3 mLocalSpaceSliderAxis;
98
99 // Ratio between rack and pinion
100 float mRatio;
101
102 // 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.
103 RefConst<Constraint> mPinionConstraint;
104 RefConst<Constraint> mRackConstraint;
105
106 // RUN TIME PROPERTIES FOLLOW
107
108 // World space hinge axis
109 Vec3 mWorldSpaceHingeAxis;
110
111 // World space sliding direction
112 Vec3 mWorldSpaceSliderAxis;
113
114 // The constraint parts
115 RackAndPinionConstraintPart mRackAndPinionConstraintPart;
116};
117
EConstraintSpace
Certain constraints support setting them up in local or world space. This governs what is used.
Definition Constraint.h:58
@ WorldSpace
All constraint properties are specified in world space.
EConstraintSubType
Enum to identify constraint sub type.
Definition Constraint.h:34
#define JPH_EXPORT
Definition Core.h:236
#define JPH_NAMESPACE_END
Definition Core.h:379
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:31
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(linkage, class_name)
Definition SerializableObject.h:109
Definition Body.h:35
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:103
virtual void SaveBinaryState(StreamOut &inStream) const
Saves the contents of the constraint settings in binary form to inStream.
Definition Constraint.cpp:26
virtual void RestoreBinaryState(StreamIn &inStream)
This function should not be called directly, it is used by sRestoreFromBinaryState.
Definition Constraint.cpp:36
Definition DebugRenderer.h:47
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
float GetTotalLambda() const
Definition RackAndPinionConstraint.h:85
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:82
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
Definition RackAndPinionConstraintPart.h:40
Rack and pinion constraint (slider & gear) settings.
Definition RackAndPinionConstraint.h:14
void SetRatio(int inNumTeethRack, float inRackLength, int inNumTeethPinion)
Definition RackAndPinionConstraint.h:30
Definition Reference.h:163
Definition Reference.h:107
Definition StateRecorder.h:105
Simple binary input stream.
Definition StreamIn.h:13
Simple binary output stream.
Definition StreamOut.h:13
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
virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const =0
Definition Vec3.h:17
static JPH_INLINE Vec3 sAxisX()
Vectors with the principal axis.
Definition Vec3.h:53