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
9912 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
/// Update the rotation limits of the hinge, value in radians (see HingeConstraintSettings)
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
132
/// Update the limits spring settings
133
const SpringSettings & GetLimitsSpringSettings() const { return mLimitsSpringSettings; }
134
SpringSettings & GetLimitsSpringSettings() { return mLimitsSpringSettings; }
135
void SetLimitsSpringSettings(const SpringSettings &inLimitsSpringSettings) { mLimitsSpringSettings = inLimitsSpringSettings; }
136
137
///@name Get Lagrange multiplier from last physics update (the linear/angular impulse applied to satisfy the constraint)
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
143
private:
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
200
JPH_NAMESPACE_END
201
202