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
32 RVec3 mPoint1 = RVec3::sZero();
33 Vec3 mHingeAxis1 = Vec3::sAxisY();
34 Vec3 mNormalAxis1 = Vec3::sAxisX();
35
37 RVec3 mPoint2 = RVec3::sZero();
38 Vec3 mHingeAxis2 = Vec3::sAxisY();
39 Vec3 mNormalAxis2 = Vec3::sAxisX();
40
43 float mLimitsMin = -JPH_PI;
44 float mLimitsMax = JPH_PI;
45
48
50 float mMaxFrictionTorque = 0.0f;
51
54
55protected:
56 // See: ConstraintSettings::RestoreBinaryState
57 virtual void RestoreBinaryState(StreamIn &inStream) override;
58};
59
62{
63public:
65
67 HingeConstraint(Body &inBody1, Body &inBody2, const HingeConstraintSettings &inSettings);
68
69 // Generic interface of a constraint
70 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Hinge; }
71 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
72 virtual void SetupVelocityConstraint(float inDeltaTime) override;
73 virtual void ResetWarmStart() override;
74 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
75 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
76 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
77#ifdef JPH_DEBUG_RENDERER
78 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
79 virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
80#endif // JPH_DEBUG_RENDERER
81 virtual void SaveState(StateRecorder &inStream) const override;
82 virtual void RestoreState(StateRecorder &inStream) override;
83 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
84
85 // See: TwoBodyConstraint
86 virtual Mat44 GetConstraintToBody1Matrix() const override;
87 virtual Mat44 GetConstraintToBody2Matrix() const override;
88
90 float GetCurrentAngle() const;
91
92 // Friction control
93 void SetMaxFrictionTorque(float inFrictionTorque) { mMaxFrictionTorque = inFrictionTorque; }
94 float GetMaxFrictionTorque() const { return mMaxFrictionTorque; }
95
96 // Motor settings
97 MotorSettings & GetMotorSettings() { return mMotorSettings; }
98 const MotorSettings & GetMotorSettings() const { return mMotorSettings; }
99
100 // Motor controls
101 void SetMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mMotorSettings.IsValid()); mMotorState = inState; }
102 EMotorState GetMotorState() const { return mMotorState; }
103 void SetTargetAngularVelocity(float inAngularVelocity) { mTargetAngularVelocity = inAngularVelocity; }
104 float GetTargetAngularVelocity() const { return mTargetAngularVelocity; }
105 void SetTargetAngle(float inAngle) { mTargetAngle = mHasLimits? Clamp(inAngle, mLimitsMin, mLimitsMax) : inAngle; }
106 float GetTargetAngle() const { return mTargetAngle; }
107
109 void SetLimits(float inLimitsMin, float inLimitsMax);
110 float GetLimitsMin() const { return mLimitsMin; }
111 float GetLimitsMax() const { return mLimitsMax; }
112 bool HasLimits() const { return mHasLimits; }
113
115 const SpringSettings & GetLimitsSpringSettings() const { return mLimitsSpringSettings; }
116 SpringSettings & GetLimitsSpringSettings() { return mLimitsSpringSettings; }
117 void SetLimitsSpringSettings(const SpringSettings &inLimitsSpringSettings) { mLimitsSpringSettings = inLimitsSpringSettings; }
118
120 inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
121 inline Vector<2> GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
122 inline float GetTotalLambdaRotationLimits() const { return mRotationLimitsConstraintPart.GetTotalLambda(); }
123 inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }
124
125private:
126 // Internal helper function to calculate the values below
127 void CalculateA1AndTheta();
128 void CalculateRotationLimitsConstraintProperties(float inDeltaTime);
129 void CalculateMotorConstraintProperties(float inDeltaTime);
130 inline float GetSmallestAngleToLimit() const;
131 inline bool IsMinLimitClosest() const;
132
133 // CONFIGURATION PROPERTIES FOLLOW
134
135 // Local space constraint positions
136 Vec3 mLocalSpacePosition1;
137 Vec3 mLocalSpacePosition2;
138
139 // Local space hinge directions
140 Vec3 mLocalSpaceHingeAxis1;
141 Vec3 mLocalSpaceHingeAxis2;
142
143 // Local space normal direction (direction relative to which to draw constraint limits)
144 Vec3 mLocalSpaceNormalAxis1;
145 Vec3 mLocalSpaceNormalAxis2;
146
147 // Inverse of initial relative orientation between bodies (which defines hinge angle = 0)
148 Quat mInvInitialOrientation;
149
150 // Hinge limits
151 bool mHasLimits;
152 float mLimitsMin;
153 float mLimitsMax;
154
155 // Soft constraint limits
156 SpringSettings mLimitsSpringSettings;
157
158 // Friction
159 float mMaxFrictionTorque;
160
161 // Motor controls
162 MotorSettings mMotorSettings;
163 EMotorState mMotorState = EMotorState::Off;
164 float mTargetAngularVelocity = 0.0f;
165 float mTargetAngle = 0.0f;
166
167 // RUN TIME PROPERTIES FOLLOW
168
169 // Current rotation around the hinge axis
170 float mTheta = 0.0f;
171
172 // World space hinge axis for body 1
173 Vec3 mA1;
174
175 // The constraint parts
176 PointConstraintPart mPointConstraintPart;
177 HingeRotationConstraintPart mRotationConstraintPart;
178 AngleConstraintPart mRotationLimitsConstraintPart;
179 AngleConstraintPart mMotorConstraintPart;
180};
181
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_EXPORT
Definition: Core.h:227
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#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:17
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(linkage, class_name)
Definition: SerializableObject.h:100
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:62
EMotorState GetMotorState() const
Definition: HingeConstraint.h:102
const SpringSettings & GetLimitsSpringSettings() const
Update the limits spring settings.
Definition: HingeConstraint.h:115
void SetLimitsSpringSettings(const SpringSettings &inLimitsSpringSettings)
Definition: HingeConstraint.h:117
const MotorSettings & GetMotorSettings() const
Definition: HingeConstraint.h:98
SpringSettings & GetLimitsSpringSettings()
Definition: HingeConstraint.h:116
float GetLimitsMax() const
Definition: HingeConstraint.h:111
float GetTotalLambdaRotationLimits() const
Definition: HingeConstraint.h:122
Vector< 2 > GetTotalLambdaRotation() const
Definition: HingeConstraint.h:121
MotorSettings & GetMotorSettings()
Definition: HingeConstraint.h:97
float GetLimitsMin() const
Definition: HingeConstraint.h:110
void SetTargetAngularVelocity(float inAngularVelocity)
rad/s
Definition: HingeConstraint.h:103
float GetTargetAngle() const
Definition: HingeConstraint.h:106
float GetMaxFrictionTorque() const
Definition: HingeConstraint.h:94
void SetMotorState(EMotorState inState)
Definition: HingeConstraint.h:101
void SetTargetAngle(float inAngle)
rad
Definition: HingeConstraint.h:105
virtual EConstraintSubType GetSubType() const override
Get the sub type of a constraint.
Definition: HingeConstraint.h:70
float GetTotalLambdaMotor() const
Definition: HingeConstraint.h:123
float GetTargetAngularVelocity() const
Definition: HingeConstraint.h:104
void SetMaxFrictionTorque(float inFrictionTorque)
Definition: HingeConstraint.h:93
Vec3 GetTotalLambdaPosition() const
Definition: HingeConstraint.h:120
bool HasLimits() const
Definition: HingeConstraint.h:112
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:53
SpringSettings mLimitsSpringSettings
When enabled, this makes the limits soft. When the constraint exceeds the limits, a spring force will...
Definition: HingeConstraint.h:47
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:101
Settings for a linear or angular spring.
Definition: SpringSettings.h:23
Definition: StateRecorder.h:48
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:16
static JPH_INLINE Vec3 sAxisX()
Vectors with the principal axis.
Definition: Vec3.h:52
static JPH_INLINE Vec3 sAxisY()
Definition: Vec3.h:53
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107
Templatized vector class.
Definition: Vector.h:12