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{
16
17public:
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
35
37 Vec3 mHingeAxis1 = Vec3::sAxisX();
38
40 Vec3 mHingeAxis2 = Vec3::sAxisX();
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 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
80 void SetConstraints(const Constraint *inGear1, const Constraint *inGear2) { mGear1Constraint = inGear1; mGear2Constraint = inGear2; }
81
83 inline float GetTotalLambda() const { return mGearConstraintPart.GetTotalLambda(); }
84
85private:
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
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
Definition GearConstraint.h:53
float GetTotalLambda() const
Definition GearConstraint.h:83
virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override
Definition GearConstraint.h:62
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:80
virtual EConstraintSubType GetSubType() const override
Get the sub type of a constraint.
Definition GearConstraint.h:61
Definition GearConstraintPart.h:40
Gear constraint settings.
Definition GearConstraint.h:14
void SetRatio(int inNumTeethGear1, int inNumTeethGear2)
Definition GearConstraint.h:28
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: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