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