Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/RackAndPinionConstraint.h
9912 views
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
7
#include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
8
#include <Jolt/Physics/Constraints/ConstraintPart/RackAndPinionConstraintPart.h>
9
10
JPH_NAMESPACE_BEGIN
11
12
/// Rack and pinion constraint (slider & gear) settings
13
class JPH_EXPORT RackAndPinionConstraintSettings final : public TwoBodyConstraintSettings
14
{
15
JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, RackAndPinionConstraintSettings)
16
17
public:
18
// See: ConstraintSettings::SaveBinaryState
19
virtual void SaveBinaryState(StreamOut &inStream) const override;
20
21
/// Create an instance of this constraint.
22
/// Body1 should be the pinion (gear) and body 2 the rack (slider).
23
virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
24
25
/// Defines the ratio between the rotation of the pinion and the translation of the rack.
26
/// The ratio is defined as: PinionRotation(t) = ratio * RackTranslation(t)
27
/// @param inNumTeethRack Number of teeth that the rack has
28
/// @param inRackLength Length of the rack
29
/// @param inNumTeethPinion Number of teeth the pinion has
30
void SetRatio(int inNumTeethRack, float inRackLength, int inNumTeethPinion)
31
{
32
mRatio = 2.0f * JPH_PI * inNumTeethRack / (inRackLength * inNumTeethPinion);
33
}
34
35
/// This determines in which space the constraint is setup, all properties below should be in the specified space
36
EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
37
38
/// Body 1 (pinion) constraint reference frame (space determined by mSpace).
39
Vec3 mHingeAxis = Vec3::sAxisX();
40
41
/// Body 2 (rack) constraint reference frame (space determined by mSpace)
42
Vec3 mSliderAxis = Vec3::sAxisX();
43
44
/// Ratio between the rack and pinion, see SetRatio.
45
float mRatio = 1.0f;
46
47
protected:
48
// See: ConstraintSettings::RestoreBinaryState
49
virtual void RestoreBinaryState(StreamIn &inStream) override;
50
};
51
52
/// A rack and pinion constraint constrains the rotation of body1 to the translation of body 2.
53
/// Note that this constraint needs to be used in conjunction with a hinge constraint for body 1 and a slider constraint for body 2.
54
class JPH_EXPORT RackAndPinionConstraint final : public TwoBodyConstraint
55
{
56
public:
57
JPH_OVERRIDE_NEW_DELETE
58
59
/// Construct gear constraint
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 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
81
/// 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.
82
void SetConstraints(const Constraint *inPinion, const Constraint *inRack) { mPinionConstraint = inPinion; mRackConstraint = inRack; }
83
84
///@name Get Lagrange multiplier from last physics update (the linear/angular impulse applied to satisfy the constraint)
85
inline float GetTotalLambda() const { return mRackAndPinionConstraintPart.GetTotalLambda(); }
86
87
private:
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
118
JPH_NAMESPACE_END
119
120