Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
HingeConstraint.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
12
14
17{
18public:
20
21 // See: ConstraintSettings::SaveBinaryState
22 virtual void SaveBinaryState(StreamOut &inStream) const override;
23
25 virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
26
28 EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
29
35
40
43 float mLimitsMin = -JPH_PI;
44 float mLimitsMax = JPH_PI;
45
47 float mMaxFrictionTorque = 0.0f;
48
51
52protected:
53 // See: ConstraintSettings::RestoreBinaryState
54 virtual void RestoreBinaryState(StreamIn &inStream) override;
55};
56
59{
60public:
62
64 HingeConstraint(Body &inBody1, Body &inBody2, const HingeConstraintSettings &inSettings);
65
66 // Generic interface of a constraint
67 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Hinge; }
68 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
69 virtual void SetupVelocityConstraint(float inDeltaTime) override;
70 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
71 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
72 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
73#ifdef JPH_DEBUG_RENDERER
74 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
75 virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
76#endif // JPH_DEBUG_RENDERER
77 virtual void SaveState(StateRecorder &inStream) const override;
78 virtual void RestoreState(StateRecorder &inStream) override;
79 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
80
81 // See: TwoBodyConstraint
82 virtual Mat44 GetConstraintToBody1Matrix() const override;
83 virtual Mat44 GetConstraintToBody2Matrix() const override;
84
86 float GetCurrentAngle() const;
87
88 // Friction control
89 void SetMaxFrictionTorque(float inFrictionTorque) { mMaxFrictionTorque = inFrictionTorque; }
90 float GetMaxFrictionTorque() const { return mMaxFrictionTorque; }
91
92 // Motor settings
93 MotorSettings & GetMotorSettings() { return mMotorSettings; }
94 const MotorSettings & GetMotorSettings() const { return mMotorSettings; }
95
96 // Motor controls
97 void SetMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mMotorSettings.IsValid()); mMotorState = inState; }
98 EMotorState GetMotorState() const { return mMotorState; }
99 void SetTargetAngularVelocity(float inAngularVelocity) { mTargetAngularVelocity = inAngularVelocity; }
100 float GetTargetAngularVelocity() const { return mTargetAngularVelocity; }
101 void SetTargetAngle(float inAngle) { mTargetAngle = mHasLimits? Clamp(inAngle, mLimitsMin, mLimitsMax) : inAngle; }
102 float GetTargetAngle() const { return mTargetAngle; }
103
105 void SetLimits(float inLimitsMin, float inLimitsMax);
106 float GetLimitsMin() const { return mLimitsMin; }
107 float GetLimitsMax() const { return mLimitsMax; }
108 bool HasLimits() const { return mHasLimits; }
109
111 inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
112 inline Vector<2> GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
113 inline float GetTotalLambdaRotationLimits() const { return mRotationLimitsConstraintPart.GetTotalLambda(); }
114 inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }
115
116private:
117 // Internal helper function to calculate the values below
118 void CalculateA1AndTheta();
119 void CalculateRotationLimitsConstraintProperties(float inDeltaTime);
120 void CalculateMotorConstraintProperties(float inDeltaTime);
121 inline float GetSmallestAngleToLimit() const;
122
123 // CONFIGURATION PROPERTIES FOLLOW
124
125 // Local space constraint positions
126 Vec3 mLocalSpacePosition1;
127 Vec3 mLocalSpacePosition2;
128
129 // Local space hinge directions
130 Vec3 mLocalSpaceHingeAxis1;
131 Vec3 mLocalSpaceHingeAxis2;
132
133 // Local space normal direction (direction relative to which to draw constraint limits)
134 Vec3 mLocalSpaceNormalAxis1;
135 Vec3 mLocalSpaceNormalAxis2;
136
137 // Inverse of initial relative orientation between bodies (which defines hinge angle = 0)
138 Quat mInvInitialOrientation;
139
140 // Hinge limits
141 bool mHasLimits;
142 float mLimitsMin;
143 float mLimitsMax;
144
145 // Friction
146 float mMaxFrictionTorque;
147
148 // Motor controls
149 MotorSettings mMotorSettings;
150 EMotorState mMotorState = EMotorState::Off;
151 float mTargetAngularVelocity = 0.0f;
152 float mTargetAngle = 0.0f;
153
154 // RUN TIME PROPERTIES FOLLOW
155
156 // Current rotation around the hinge axis
157 float mTheta = 0.0f;
158
159 // World space hinge axis for body 1
160 Vec3 mA1;
161
162 // The constraint parts
163 PointConstraintPart mPointConstraintPart;
164 HingeRotationConstraintPart mRotationConstraintPart;
165 AngleConstraintPart mRotationLimitsConstraintPart;
166 AngleConstraintPart mMotorConstraintPart;
167};
168
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_ASSERT(...)
Definition: IssueReporting.h:33
constexpr T Clamp(T inV, T inMin, T inMax)
Clamp a value between two values.
Definition: Math.h:45
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
EMotorState
Definition: MotorSettings.h:16
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(class_name)
Definition: SerializableObject.h:100
Definition: AngleConstraintPart.h:36
float GetTotalLambda() const
Return lagrange multiplier.
Definition: AngleConstraintPart.h:129
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
Simple triangle renderer for debugging purposes.
Definition: DebugRenderer.h:25
A hinge constraint constrains 2 bodies on a single point and allows only a single axis of rotation.
Definition: HingeConstraint.h:59
EMotorState GetMotorState() const
Definition: HingeConstraint.h:98
virtual Mat44 GetConstraintToBody1Matrix() const override
Calculates the transform that transforms from constraint space to body 1 space. The first column of t...
Definition: HingeConstraint.cpp:373
virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override
Definition: HingeConstraint.cpp:213
virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override
Definition: HingeConstraint.cpp:270
const MotorSettings & GetMotorSettings() const
Definition: HingeConstraint.h:94
virtual void SetupVelocityConstraint(float inDeltaTime) override
Definition: HingeConstraint.cpp:201
float GetLimitsMax() const
Definition: HingeConstraint.h:107
virtual void SaveState(StateRecorder &inStream) const override
Saving state for replay.
Definition: HingeConstraint.cpp:326
float GetTotalLambdaRotationLimits() const
Definition: HingeConstraint.h:113
Vector< 2 > GetTotalLambdaRotation() const
Definition: HingeConstraint.h:112
float GetCurrentAngle() const
Get the current rotation angle from the rest position.
Definition: HingeConstraint.cpp:123
void SetLimits(float inLimitsMin, float inLimitsMax)
Update the rotation limits of the hinge, value in radians (see HingeConstraintSettings)
Definition: HingeConstraint.cpp:131
virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override
Definition: HingeConstraint.cpp:311
MotorSettings & GetMotorSettings()
Definition: HingeConstraint.h:93
float GetLimitsMin() const
Definition: HingeConstraint.h:106
void SetTargetAngularVelocity(float inAngularVelocity)
rad/s
Definition: HingeConstraint.h:99
float GetTargetAngle() const
Definition: HingeConstraint.h:102
float GetMaxFrictionTorque() const
Definition: HingeConstraint.h:90
virtual Mat44 GetConstraintToBody2Matrix() const override
Calculates the transform that transforms from constraint space to body 2 space. The first column of t...
Definition: HingeConstraint.cpp:378
virtual bool SolveVelocityConstraint(float inDeltaTime) override
Definition: HingeConstraint.cpp:229
void SetMotorState(EMotorState inState)
Definition: HingeConstraint.h:97
virtual void RestoreState(StateRecorder &inStream) override
Restoring state for replay.
Definition: HingeConstraint.cpp:340
virtual void DrawConstraint(DebugRenderer *inRenderer) const override
Definition: HingeConstraint.cpp:295
virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override
Definition: HingeConstraint.cpp:115
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: HingeConstraint.cpp:355
void SetTargetAngle(float inAngle)
rad
Definition: HingeConstraint.h:101
virtual EConstraintSubType GetSubType() const override
Get the sub type of a constraint.
Definition: HingeConstraint.h:67
float GetTotalLambdaMotor() const
Definition: HingeConstraint.h:114
float GetTargetAngularVelocity() const
Definition: HingeConstraint.h:100
void SetMaxFrictionTorque(float inFrictionTorque)
Definition: HingeConstraint.h:89
Vec3 GetTotalLambdaPosition() const
Definition: HingeConstraint.h:111
bool HasLimits() const
Definition: HingeConstraint.h:108
Hinge constraint settings, used to create a hinge constraint.
Definition: HingeConstraint.h:17
float mLimitsMin
Definition: HingeConstraint.h:43
float mMaxFrictionTorque
Maximum amount of torque (N m) to apply as friction when the constraint is not powered by a motor.
Definition: HingeConstraint.h:47
Vec3 mHingeAxis2
Definition: HingeConstraint.h:38
Vec3 mNormalAxis2
Definition: HingeConstraint.h:39
EConstraintSpace mSpace
This determines in which space the constraint is setup, all properties below should be in the specifi...
Definition: HingeConstraint.h:28
Vec3 mHingeAxis1
Definition: HingeConstraint.h:33
float mLimitsMax
Definition: HingeConstraint.h:44
virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override
Create an an instance of this constraint.
Definition: HingeConstraint.cpp:69
RVec3 mPoint1
Definition: HingeConstraint.h:32
virtual void RestoreBinaryState(StreamIn &inStream) override
This function should not be called directly, it is used by sRestoreFromBinaryState.
Definition: HingeConstraint.cpp:53
RVec3 mPoint2
Body 2 constraint reference frame (space determined by mSpace)
Definition: HingeConstraint.h:37
Vec3 mNormalAxis1
Definition: HingeConstraint.h:34
virtual void SaveBinaryState(StreamOut &inStream) const override
Saves the contents of the constraint settings in binary form to inStream.
Definition: HingeConstraint.cpp:36
MotorSettings mMotorSettings
In case the constraint is powered, this determines the motor settings around the hinge axis.
Definition: HingeConstraint.h:50
Definition: HingeRotationConstraintPart.h:42
const Vec2 & GetTotalLambda() const
Return lagrange multiplier.
Definition: HingeRotationConstraintPart.h:194
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
Definition: MotorSettings.h:25
bool IsValid() const
Check if settings are valid.
Definition: MotorSettings.h:48
Definition: PointConstraintPart.h:41
Vec3 GetTotalLambda() const
Return lagrange multiplier.
Definition: PointConstraintPart.h:199
Definition: Quat.h:33
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
static JPH_INLINE Vec3 sAxisY()
Definition: Vec3.h:52
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107
Templatized vector class.
Definition: Vector.h:12