Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/HingeConstraint.h
22173 views
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
7
#include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
8
#include <Jolt/Physics/Constraints/MotorSettings.h>
9
#include <Jolt/Physics/Constraints/ConstraintPart/PointConstraintPart.h>
10
#include <Jolt/Physics/Constraints/ConstraintPart/HingeRotationConstraintPart.h>
11
#include <Jolt/Physics/Constraints/ConstraintPart/AngleConstraintPart.h>
12
13
JPH_NAMESPACE_BEGIN
14
15
/// Hinge constraint settings, used to create a hinge constraint
16
class JPH_EXPORT HingeConstraintSettings final : public TwoBodyConstraintSettings
17
{
18
JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, HingeConstraintSettings)
19
20
public:
21
// See: ConstraintSettings::SaveBinaryState
22
virtual void SaveBinaryState(StreamOut &inStream) const override;
23
24
/// Create an instance of this constraint
25
virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
26
27
/// This determines in which space the constraint is setup, all properties below should be in the specified space
28
EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
29
30
/// Body 1 constraint reference frame (space determined by mSpace).
31
/// Hinge axis is the axis where rotation is allowed.
32
/// When the normal axis of both bodies align in world space, the hinge angle is defined to be 0.
33
/// mHingeAxis1 and mNormalAxis1 should be perpendicular. mHingeAxis2 and mNormalAxis2 should also be perpendicular.
34
/// If you configure the joint in world space and create both bodies with a relative rotation you want to be defined as zero,
35
/// you can simply set mHingeAxis1 = mHingeAxis2 and mNormalAxis1 = mNormalAxis2.
36
RVec3 mPoint1 = RVec3::sZero();
37
Vec3 mHingeAxis1 = Vec3::sAxisY();
38
Vec3 mNormalAxis1 = Vec3::sAxisX();
39
40
/// Body 2 constraint reference frame (space determined by mSpace)
41
RVec3 mPoint2 = RVec3::sZero();
42
Vec3 mHingeAxis2 = Vec3::sAxisY();
43
Vec3 mNormalAxis2 = Vec3::sAxisX();
44
45
/// Rotation around the hinge axis will be limited between [mLimitsMin, mLimitsMax] where mLimitsMin e [-pi, 0] and mLimitsMax e [0, pi].
46
/// Both angles are in radians.
47
float mLimitsMin = -JPH_PI;
48
float mLimitsMax = JPH_PI;
49
50
/// When enabled, this makes the limits soft. When the constraint exceeds the limits, a spring force will pull it back.
51
SpringSettings mLimitsSpringSettings;
52
53
/// Maximum amount of torque (N m) to apply as friction when the constraint is not powered by a motor
54
float mMaxFrictionTorque = 0.0f;
55
56
/// In case the constraint is powered, this determines the motor settings around the hinge axis
57
MotorSettings mMotorSettings;
58
59
protected:
60
// See: ConstraintSettings::RestoreBinaryState
61
virtual void RestoreBinaryState(StreamIn &inStream) override;
62
};
63
64
/// A hinge constraint constrains 2 bodies on a single point and allows only a single axis of rotation
65
class JPH_EXPORT HingeConstraint final : public TwoBodyConstraint
66
{
67
public:
68
JPH_OVERRIDE_NEW_DELETE
69
70
/// Construct hinge constraint
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
93
/// Get the attachment point for body 1 relative to body 1 COM (transform by Body::GetCenterOfMassTransform to take to world space)
94
inline Vec3 GetLocalSpacePoint1() const { return mLocalSpacePosition1; }
95
96
/// Get the attachment point for body 2 relative to body 2 COM (transform by Body::GetCenterOfMassTransform to take to world space)
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
107
/// Get the current rotation angle from the rest position
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; } ///< rad/s
122
float GetTargetAngularVelocity() const { return mTargetAngularVelocity; }
123
void SetTargetAngle(float inAngle) { mTargetAngle = mHasLimits? Clamp(inAngle, mLimitsMin, mLimitsMax) : inAngle; } ///< rad
124
float GetTargetAngle() const { return mTargetAngle; }
125
126
/// Set the target orientation in body space (R2 = R1 * inOrientation, where R1 and R2 are the world space rotations for body 1 and 2).
127
/// Calculates the local space target angle and calls SetTargetAngle. Motor state must be EMotorState::Position for this to have any effect.
128
/// May set the wrong angle if inOrientation contains large rotations around other axis than the hinge axis.
129
void SetTargetOrientationBS(QuatArg inOrientation);
130
131
/// Update the rotation limits of the hinge, value in radians (see HingeConstraintSettings)
132
void SetLimits(float inLimitsMin, float inLimitsMax);
133
float GetLimitsMin() const { return mLimitsMin; }
134
float GetLimitsMax() const { return mLimitsMax; }
135
bool HasLimits() const { return mHasLimits; }
136
137
/// Update the limits spring settings
138
const SpringSettings & GetLimitsSpringSettings() const { return mLimitsSpringSettings; }
139
SpringSettings & GetLimitsSpringSettings() { return mLimitsSpringSettings; }
140
void SetLimitsSpringSettings(const SpringSettings &inLimitsSpringSettings) { mLimitsSpringSettings = inLimitsSpringSettings; }
141
142
///@name Get Lagrange multiplier from last physics update (the linear/angular impulse applied to satisfy the constraint)
143
inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
144
inline Vector<2> GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
145
inline float GetTotalLambdaRotationLimits() const { return mRotationLimitsConstraintPart.GetTotalLambda(); }
146
inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }
147
148
private:
149
// Internal helper function to calculate the values below
150
void CalculateA1AndTheta();
151
void CalculateRotationLimitsConstraintProperties(float inDeltaTime);
152
void CalculateMotorConstraintProperties(float inDeltaTime);
153
inline float GetSmallestAngleToLimit() const;
154
inline bool IsMinLimitClosest() const;
155
156
// CONFIGURATION PROPERTIES FOLLOW
157
158
// Local space constraint positions
159
Vec3 mLocalSpacePosition1;
160
Vec3 mLocalSpacePosition2;
161
162
// Local space hinge directions
163
Vec3 mLocalSpaceHingeAxis1;
164
Vec3 mLocalSpaceHingeAxis2;
165
166
// Local space normal direction (direction relative to which to draw constraint limits)
167
Vec3 mLocalSpaceNormalAxis1;
168
Vec3 mLocalSpaceNormalAxis2;
169
170
// Inverse of initial relative orientation between bodies (which defines hinge angle = 0)
171
Quat mInvInitialOrientation;
172
173
// Hinge limits
174
bool mHasLimits;
175
float mLimitsMin;
176
float mLimitsMax;
177
178
// Soft constraint limits
179
SpringSettings mLimitsSpringSettings;
180
181
// Friction
182
float mMaxFrictionTorque;
183
184
// Motor controls
185
MotorSettings mMotorSettings;
186
EMotorState mMotorState = EMotorState::Off;
187
float mTargetAngularVelocity = 0.0f;
188
float mTargetAngle = 0.0f;
189
190
// RUN TIME PROPERTIES FOLLOW
191
192
// Current rotation around the hinge axis
193
float mTheta = 0.0f;
194
195
// World space hinge axis for body 1
196
Vec3 mA1;
197
198
// The constraint parts
199
PointConstraintPart mPointConstraintPart;
200
HingeRotationConstraintPart mRotationConstraintPart;
201
AngleConstraintPart mRotationLimitsConstraintPart;
202
AngleConstraintPart mMotorConstraintPart;
203
};
204
205
JPH_NAMESPACE_END
206
207