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{
19
20public:
21 // See: ConstraintSettings::SaveBinaryState
22 virtual void SaveBinaryState(StreamOut &inStream) const override;
23
25 virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
26
29
36 RVec3 mPoint1 = RVec3::sZero();
37 Vec3 mHingeAxis1 = Vec3::sAxisY();
38 Vec3 mNormalAxis1 = Vec3::sAxisX();
39
41 RVec3 mPoint2 = RVec3::sZero();
42 Vec3 mHingeAxis2 = Vec3::sAxisY();
43 Vec3 mNormalAxis2 = Vec3::sAxisX();
44
47 float mLimitsMin = -JPH_PI;
48 float mLimitsMax = JPH_PI;
49
52
54 float mMaxFrictionTorque = 0.0f;
55
58
59protected:
60 // See: ConstraintSettings::RestoreBinaryState
61 virtual void RestoreBinaryState(StreamIn &inStream) override;
62};
63
66{
67public:
69
71 HingeConstraint(Body &inBody1, Body &inBody2, const HingeConstraintSettings &inSettings);
72
73 // Generic interface of a constraint
74 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Hinge; }
75 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
76 virtual void SetupVelocityConstraint(float inDeltaTime) override;
77 virtual void ResetWarmStart() override;
78 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
79 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
80 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
81#ifdef JPH_DEBUG_RENDERER
82 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
83 virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
84#endif // JPH_DEBUG_RENDERER
85 virtual void SaveState(StateRecorder &inStream) const override;
86 virtual void RestoreState(StateRecorder &inStream) override;
87 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
88
89 // See: TwoBodyConstraint
90 virtual Mat44 GetConstraintToBody1Matrix() const override;
91 virtual Mat44 GetConstraintToBody2Matrix() const override;
92
94 inline Vec3 GetLocalSpacePoint1() const { return mLocalSpacePosition1; }
95
97 inline Vec3 GetLocalSpacePoint2() const { return mLocalSpacePosition2; }
98
99 // Local space hinge directions (transform direction by Body::GetCenterOfMassTransform to take to world space)
100 Vec3 GetLocalSpaceHingeAxis1() const { return mLocalSpaceHingeAxis1; }
101 Vec3 GetLocalSpaceHingeAxis2() const { return mLocalSpaceHingeAxis2; }
102
103 // Local space normal directions (transform direction by Body::GetCenterOfMassTransform to take to world space)
104 Vec3 GetLocalSpaceNormalAxis1() const { return mLocalSpaceNormalAxis1; }
105 Vec3 GetLocalSpaceNormalAxis2() const { return mLocalSpaceNormalAxis2; }
106
108 float GetCurrentAngle() const;
109
110 // Friction control
111 void SetMaxFrictionTorque(float inFrictionTorque) { mMaxFrictionTorque = inFrictionTorque; }
112 float GetMaxFrictionTorque() const { return mMaxFrictionTorque; }
113
114 // Motor settings
115 MotorSettings & GetMotorSettings() { return mMotorSettings; }
116 const MotorSettings & GetMotorSettings() const { return mMotorSettings; }
117
118 // Motor controls
119 void SetMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mMotorSettings.IsValid()); mMotorState = inState; }
120 EMotorState GetMotorState() const { return mMotorState; }
121 void SetTargetAngularVelocity(float inAngularVelocity) { mTargetAngularVelocity = inAngularVelocity; }
122 float GetTargetAngularVelocity() const { return mTargetAngularVelocity; }
123 void SetTargetAngle(float inAngle) { mTargetAngle = mHasLimits? Clamp(inAngle, mLimitsMin, mLimitsMax) : inAngle; }
124 float GetTargetAngle() const { return mTargetAngle; }
125
127 void SetLimits(float inLimitsMin, float inLimitsMax);
128 float GetLimitsMin() const { return mLimitsMin; }
129 float GetLimitsMax() const { return mLimitsMax; }
130 bool HasLimits() const { return mHasLimits; }
131
133 const SpringSettings & GetLimitsSpringSettings() const { return mLimitsSpringSettings; }
134 SpringSettings & GetLimitsSpringSettings() { return mLimitsSpringSettings; }
135 void SetLimitsSpringSettings(const SpringSettings &inLimitsSpringSettings) { mLimitsSpringSettings = inLimitsSpringSettings; }
136
138 inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
139 inline Vector<2> GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
140 inline float GetTotalLambdaRotationLimits() const { return mRotationLimitsConstraintPart.GetTotalLambda(); }
141 inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }
142
143private:
144 // Internal helper function to calculate the values below
145 void CalculateA1AndTheta();
146 void CalculateRotationLimitsConstraintProperties(float inDeltaTime);
147 void CalculateMotorConstraintProperties(float inDeltaTime);
148 inline float GetSmallestAngleToLimit() const;
149 inline bool IsMinLimitClosest() const;
150
151 // CONFIGURATION PROPERTIES FOLLOW
152
153 // Local space constraint positions
154 Vec3 mLocalSpacePosition1;
155 Vec3 mLocalSpacePosition2;
156
157 // Local space hinge directions
158 Vec3 mLocalSpaceHingeAxis1;
159 Vec3 mLocalSpaceHingeAxis2;
160
161 // Local space normal direction (direction relative to which to draw constraint limits)
162 Vec3 mLocalSpaceNormalAxis1;
163 Vec3 mLocalSpaceNormalAxis2;
164
165 // Inverse of initial relative orientation between bodies (which defines hinge angle = 0)
166 Quat mInvInitialOrientation;
167
168 // Hinge limits
169 bool mHasLimits;
170 float mLimitsMin;
171 float mLimitsMax;
172
173 // Soft constraint limits
174 SpringSettings mLimitsSpringSettings;
175
176 // Friction
177 float mMaxFrictionTorque;
178
179 // Motor controls
180 MotorSettings mMotorSettings;
181 EMotorState mMotorState = EMotorState::Off;
182 float mTargetAngularVelocity = 0.0f;
183 float mTargetAngle = 0.0f;
184
185 // RUN TIME PROPERTIES FOLLOW
186
187 // Current rotation around the hinge axis
188 float mTheta = 0.0f;
189
190 // World space hinge axis for body 1
191 Vec3 mA1;
192
193 // The constraint parts
194 PointConstraintPart mPointConstraintPart;
195 HingeRotationConstraintPart mRotationConstraintPart;
196 AngleConstraintPart mRotationLimitsConstraintPart;
197 AngleConstraintPart mMotorConstraintPart;
198};
199
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_ASSERT(...)
Definition IssueReporting.h:33
JPH_INLINE 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:31
EMotorState
Definition MotorSettings.h:17
@ Off
Motor is off.
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(linkage, class_name)
Definition SerializableObject.h:109
Definition AngleConstraintPart.h:37
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
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
A hinge constraint constrains 2 bodies on a single point and allows only a single axis of rotation.
Definition HingeConstraint.h:66
EMotorState GetMotorState() const
Definition HingeConstraint.h:120
const SpringSettings & GetLimitsSpringSettings() const
Update the limits spring settings.
Definition HingeConstraint.h:133
void SetLimitsSpringSettings(const SpringSettings &inLimitsSpringSettings)
Definition HingeConstraint.h:135
const MotorSettings & GetMotorSettings() const
Definition HingeConstraint.h:116
SpringSettings & GetLimitsSpringSettings()
Definition HingeConstraint.h:134
Vec3 GetLocalSpaceNormalAxis2() const
Definition HingeConstraint.h:105
float GetLimitsMax() const
Definition HingeConstraint.h:129
float GetTotalLambdaRotationLimits() const
Definition HingeConstraint.h:140
Vector< 2 > GetTotalLambdaRotation() const
Definition HingeConstraint.h:139
MotorSettings & GetMotorSettings()
Definition HingeConstraint.h:115
Vec3 GetLocalSpacePoint2() const
Get the attachment point for body 2 relative to body 2 COM (transform by Body::GetCenterOfMassTransfo...
Definition HingeConstraint.h:97
float GetLimitsMin() const
Definition HingeConstraint.h:128
void SetTargetAngularVelocity(float inAngularVelocity)
rad/s
Definition HingeConstraint.h:121
float GetTargetAngle() const
Definition HingeConstraint.h:124
float GetMaxFrictionTorque() const
Definition HingeConstraint.h:112
Vec3 GetLocalSpaceNormalAxis1() const
Definition HingeConstraint.h:104
Vec3 GetLocalSpaceHingeAxis2() const
Definition HingeConstraint.h:101
void SetMotorState(EMotorState inState)
Definition HingeConstraint.h:119
void SetTargetAngle(float inAngle)
rad
Definition HingeConstraint.h:123
Vec3 GetLocalSpaceHingeAxis1() const
Definition HingeConstraint.h:100
virtual EConstraintSubType GetSubType() const override
Get the sub type of a constraint.
Definition HingeConstraint.h:74
float GetTotalLambdaMotor() const
Definition HingeConstraint.h:141
float GetTargetAngularVelocity() const
Definition HingeConstraint.h:122
void SetMaxFrictionTorque(float inFrictionTorque)
Definition HingeConstraint.h:111
Vec3 GetTotalLambdaPosition() const
Definition HingeConstraint.h:138
Vec3 GetLocalSpacePoint1() const
Get the attachment point for body 1 relative to body 1 COM (transform by Body::GetCenterOfMassTransfo...
Definition HingeConstraint.h:94
bool HasLimits() const
Definition HingeConstraint.h:130
Hinge constraint settings, used to create a hinge constraint.
Definition HingeConstraint.h:17
MotorSettings mMotorSettings
In case the constraint is powered, this determines the motor settings around the hinge axis.
Definition HingeConstraint.h:57
SpringSettings mLimitsSpringSettings
When enabled, this makes the limits soft. When the constraint exceeds the limits, a spring force will...
Definition HingeConstraint.h:51
Definition HingeRotationConstraintPart.h:44
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:26
Definition PointConstraintPart.h:41
Definition Quat.h:33
Definition Reference.h:107
Settings for a linear or angular spring.
Definition SpringSettings.h:23
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
static JPH_INLINE Vec3 sAxisY()
Definition Vec3.h:54
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:107
Templatized vector class.
Definition Vector.h:12