Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
GearConstraint.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
22 virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
23
28 void SetRatio(int inNumTeethGear1, int inNumTeethGear2)
29 {
30 mRatio = float(inNumTeethGear2) / float(inNumTeethGear1);
31 }
32
34 EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
35
38
41
43 float mRatio = 1.0f;
44
45protected:
46 // See: ConstraintSettings::RestoreBinaryState
47 virtual void RestoreBinaryState(StreamIn &inStream) override;
48};
49
53{
54public:
56
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 WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
65 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
66 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
67#ifdef JPH_DEBUG_RENDERER
68 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
69#endif // JPH_DEBUG_RENDERER
70 virtual void SaveState(StateRecorder &inStream) const override;
71 virtual void RestoreState(StateRecorder &inStream) override;
72 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
73
74 // See: TwoBodyConstraint
75 virtual Mat44 GetConstraintToBody1Matrix() const override;
76 virtual Mat44 GetConstraintToBody2Matrix() const override;
77
79 void SetConstraints(const Constraint *inGear1, const Constraint *inGear2) { mGear1Constraint = inGear1; mGear2Constraint = inGear2; }
80
82 inline float GetTotalLambda() const { return mGearConstraintPart.GetTotalLambda(); }
83
84private:
85 // Internal helper function to calculate the values below
86 void CalculateConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);
87
88 // CONFIGURATION PROPERTIES FOLLOW
89
90 // Local space hinge axis for body 1
91 Vec3 mLocalSpaceHingeAxis1;
92
93 // Local space hinge axis for body 2
94 Vec3 mLocalSpaceHingeAxis2;
95
96 // Ratio between gear 1 and 2
97 float mRatio;
98
99 // The constraints that constrain both gears (2 hinges), optional and used to calculate the rotation error and fix numerical drift.
100 RefConst<Constraint> mGear1Constraint;
101 RefConst<Constraint> mGear2Constraint;
102
103 // RUN TIME PROPERTIES FOLLOW
104
105 // World space hinge axis for body 1
106 Vec3 mWorldSpaceHingeAxis1;
107
108 // World space hinge axis for body 2
109 Vec3 mWorldSpaceHingeAxis2;
110
111 // The constraint parts
112 GearConstraintPart mGearConstraintPart;
113};
114
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
Definition: GearConstraint.h:53
float GetTotalLambda() const
Definition: GearConstraint.h:82
virtual Mat44 GetConstraintToBody2Matrix() const override
Calculates the transform that transforms from constraint space to body 2 space. The first column of t...
Definition: GearConstraint.cpp:177
virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override
Definition: GearConstraint.h:62
virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override
Definition: GearConstraint.cpp:85
virtual void DrawConstraint(DebugRenderer *inRenderer) const override
Definition: GearConstraint.cpp:134
virtual void SaveState(StateRecorder &inStream) const override
Saving state for replay.
Definition: GearConstraint.cpp:146
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: GearConstraint.cpp:160
virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override
Definition: GearConstraint.cpp:96
virtual Mat44 GetConstraintToBody1Matrix() const override
Calculates the transform that transforms from constraint space to body 1 space. The first column of t...
Definition: GearConstraint.cpp:171
void SetConstraints(const Constraint *inGear1, const Constraint *inGear2)
The constraints that constrain both gears (2 hinges), optional and used to calculate the rotation err...
Definition: GearConstraint.h:79
virtual void SetupVelocityConstraint(float inDeltaTime) override
Definition: GearConstraint.cpp:77
virtual void RestoreState(StateRecorder &inStream) override
Restoring state for replay.
Definition: GearConstraint.cpp:153
virtual bool SolveVelocityConstraint(float inDeltaTime) override
Definition: GearConstraint.cpp:91
virtual EConstraintSubType GetSubType() const override
Get the sub type of a constraint.
Definition: GearConstraint.h:61
Definition: GearConstraintPart.h:40
float GetTotalLambda() const
Return lagrange multiplier.
Definition: GearConstraintPart.h:125
Gear constraint settings.
Definition: GearConstraint.h:14
EConstraintSpace mSpace
This determines in which space the constraint is setup, all properties below should be in the specifi...
Definition: GearConstraint.h:34
virtual void RestoreBinaryState(StreamIn &inStream) override
This function should not be called directly, it is used by sRestoreFromBinaryState.
Definition: GearConstraint.cpp:39
Vec3 mHingeAxis1
Body 1 constraint reference frame (space determined by mSpace).
Definition: GearConstraint.h:37
Vec3 mHingeAxis2
Body 2 constraint reference frame (space determined by mSpace)
Definition: GearConstraint.h:40
void SetRatio(int inNumTeethGear1, int inNumTeethGear2)
Definition: GearConstraint.h:28
virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override
Create an an instance of this constraint.
Definition: GearConstraint.cpp:49
float mRatio
Ratio between both gears, see SetRatio.
Definition: GearConstraint.h:43
virtual void SaveBinaryState(StreamOut &inStream) const override
Saves the contents of the constraint settings in binary form to inStream.
Definition: GearConstraint.cpp:29
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
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