Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
SwingTwistConstraint.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
21{
22public:
24
25 // See: ConstraintSettings::SaveBinaryState
26 virtual void SaveBinaryState(StreamOut &inStream) const override;
27
29 virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
30
32 EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
33
35 RVec3 mPosition1 = RVec3::sZero();
36 Vec3 mTwistAxis1 = Vec3::sAxisX();
37 Vec3 mPlaneAxis1 = Vec3::sAxisY();
38
40 RVec3 mPosition2 = RVec3::sZero();
41 Vec3 mTwistAxis2 = Vec3::sAxisX();
42 Vec3 mPlaneAxis2 = Vec3::sAxisY();
43
45 ESwingType mSwingType = ESwingType::Cone;
46
48 float mNormalHalfConeAngle = 0.0f;
49 float mPlaneHalfConeAngle = 0.0f;
50
52 float mTwistMinAngle = 0.0f;
53 float mTwistMaxAngle = 0.0f;
54
56 float mMaxFrictionTorque = 0.0f;
57
61
62protected:
63 // See: ConstraintSettings::RestoreBinaryState
64 virtual void RestoreBinaryState(StreamIn &inStream) override;
65};
66
71{
72public:
74
76 SwingTwistConstraint(Body &inBody1, Body &inBody2, const SwingTwistConstraintSettings &inSettings);
77
79 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::SwingTwist; }
80 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
81 virtual void SetupVelocityConstraint(float inDeltaTime) override;
82 virtual void ResetWarmStart() override;
83 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
84 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
85 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
86#ifdef JPH_DEBUG_RENDERER
87 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
88 virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
89#endif // JPH_DEBUG_RENDERER
90 virtual void SaveState(StateRecorder &inStream) const override;
91 virtual void RestoreState(StateRecorder &inStream) override;
92 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
93
94 // See: TwoBodyConstraint
95 virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sRotationTranslation(mConstraintToBody1, mLocalSpacePosition1); }
96 virtual Mat44 GetConstraintToBody2Matrix() const override { return Mat44::sRotationTranslation(mConstraintToBody2, mLocalSpacePosition2); }
97
99 inline Vec3 GetLocalSpacePosition1() const { return mLocalSpacePosition1; }
100 inline Vec3 GetLocalSpacePosition2() const { return mLocalSpacePosition2; }
101 inline Quat GetConstraintToBody1() const { return mConstraintToBody1; }
102 inline Quat GetConstraintToBody2() const { return mConstraintToBody2; }
103
105 inline float GetNormalHalfConeAngle() const { return mNormalHalfConeAngle; }
106 inline void SetNormalHalfConeAngle(float inAngle) { mNormalHalfConeAngle = inAngle; UpdateLimits(); }
107 inline float GetPlaneHalfConeAngle() const { return mPlaneHalfConeAngle; }
108 inline void SetPlaneHalfConeAngle(float inAngle) { mPlaneHalfConeAngle = inAngle; UpdateLimits(); }
109 inline float GetTwistMinAngle() const { return mTwistMinAngle; }
110 inline void SetTwistMinAngle(float inAngle) { mTwistMinAngle = inAngle; UpdateLimits(); }
111 inline float GetTwistMaxAngle() const { return mTwistMaxAngle; }
112 inline void SetTwistMaxAngle(float inAngle) { mTwistMaxAngle = inAngle; UpdateLimits(); }
113
115 const MotorSettings & GetSwingMotorSettings() const { return mSwingMotorSettings; }
116 MotorSettings & GetSwingMotorSettings() { return mSwingMotorSettings; }
117 const MotorSettings & GetTwistMotorSettings() const { return mTwistMotorSettings; }
118 MotorSettings & GetTwistMotorSettings() { return mTwistMotorSettings; }
119
121 void SetMaxFrictionTorque(float inFrictionTorque) { mMaxFrictionTorque = inFrictionTorque; }
122 float GetMaxFrictionTorque() const { return mMaxFrictionTorque; }
123
125
127 void SetSwingMotorState(EMotorState inState);
128 EMotorState GetSwingMotorState() const { return mSwingMotorState; }
129 void SetTwistMotorState(EMotorState inState);
130 EMotorState GetTwistMotorState() const { return mTwistMotorState; }
131
133 void SetTargetAngularVelocityCS(Vec3Arg inAngularVelocity) { mTargetAngularVelocity = inAngularVelocity; }
134 Vec3 GetTargetAngularVelocityCS() const { return mTargetAngularVelocity; }
135
137 void SetTargetOrientationCS(QuatArg inOrientation);
138 Quat GetTargetOrientationCS() const { return mTargetOrientation; }
139
142 void SetTargetOrientationBS(QuatArg inOrientation) { SetTargetOrientationCS(mConstraintToBody1.Conjugated() * inOrientation * mConstraintToBody2); }
143
146 Quat GetRotationInConstraintSpace() const;
147
149 inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
150 inline float GetTotalLambdaTwist() const { return mSwingTwistConstraintPart.GetTotalTwistLambda(); }
151 inline float GetTotalLambdaSwingY() const { return mSwingTwistConstraintPart.GetTotalSwingYLambda(); }
152 inline float GetTotalLambdaSwingZ() const { return mSwingTwistConstraintPart.GetTotalSwingZLambda(); }
153 inline Vec3 GetTotalLambdaMotor() const { return Vec3(mMotorConstraintPart[0].GetTotalLambda(), mMotorConstraintPart[1].GetTotalLambda(), mMotorConstraintPart[2].GetTotalLambda()); }
154
155private:
156 // Update the limits in the swing twist constraint part
157 void UpdateLimits();
158
159 // CONFIGURATION PROPERTIES FOLLOW
160
161 // Local space constraint positions
162 Vec3 mLocalSpacePosition1;
163 Vec3 mLocalSpacePosition2;
164
165 // Transforms from constraint space to body space
166 Quat mConstraintToBody1;
167 Quat mConstraintToBody2;
168
169 // Limits
170 float mNormalHalfConeAngle;
171 float mPlaneHalfConeAngle;
172 float mTwistMinAngle;
173 float mTwistMaxAngle;
174
175 // Friction
176 float mMaxFrictionTorque;
177
178 // Motor controls
179 MotorSettings mSwingMotorSettings;
180 MotorSettings mTwistMotorSettings;
181 EMotorState mSwingMotorState = EMotorState::Off;
182 EMotorState mTwistMotorState = EMotorState::Off;
183 Vec3 mTargetAngularVelocity = Vec3::sZero();
184 Quat mTargetOrientation = Quat::sIdentity();
185
186 // RUN TIME PROPERTIES FOLLOW
187
188 // Rotation axis for motor constraint parts
189 Vec3 mWorldSpaceMotorAxis[3];
190
191 // The constraint parts
192 PointConstraintPart mPointConstraintPart;
193 SwingTwistConstraintPart mSwingTwistConstraintPart;
194 AngleConstraintPart mMotorConstraintPart[3];
195};
196
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_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
ESwingType
How the swing limit behaves.
Definition: SwingTwistConstraintPart.h:15
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
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
static JPH_INLINE Mat44 sRotationTranslation(QuatArg inR, Vec3Arg inT)
Get matrix that rotates and translates.
Definition: Mat44.inl:149
Definition: MotorSettings.h:26
Definition: PointConstraintPart.h:41
Definition: Quat.h:33
static JPH_INLINE Quat sIdentity()
Definition: Quat.h:103
Definition: Reference.h:101
Definition: StateRecorder.h:48
Simple binary input stream.
Definition: StreamIn.h:13
Simple binary output stream.
Definition: StreamOut.h:13
Definition: SwingTwistConstraint.h:71
void SetPlaneHalfConeAngle(float inAngle)
Definition: SwingTwistConstraint.h:108
float GetNormalHalfConeAngle() const
Definition: SwingTwistConstraint.h:105
void SetTwistMinAngle(float inAngle)
Definition: SwingTwistConstraint.h:110
float GetTotalLambdaSwingY() const
Definition: SwingTwistConstraint.h:151
float GetTwistMaxAngle() const
Definition: SwingTwistConstraint.h:111
virtual EConstraintSubType GetSubType() const override
Get the sub type of a constraint.
Definition: SwingTwistConstraint.h:79
virtual Mat44 GetConstraintToBody2Matrix() const override
Calculates the transform that transforms from constraint space to body 2 space. The first column of t...
Definition: SwingTwistConstraint.h:96
float GetTotalLambdaSwingZ() const
Definition: SwingTwistConstraint.h:152
void SetTargetAngularVelocityCS(Vec3Arg inAngularVelocity)
Set the target angular velocity of body 2 in constraint space of body 2.
Definition: SwingTwistConstraint.h:133
Vec3 GetTotalLambdaMotor() const
Definition: SwingTwistConstraint.h:153
EMotorState GetTwistMotorState() const
Definition: SwingTwistConstraint.h:130
Quat GetConstraintToBody1() const
Definition: SwingTwistConstraint.h:101
void SetTwistMaxAngle(float inAngle)
Definition: SwingTwistConstraint.h:112
void SetNormalHalfConeAngle(float inAngle)
Definition: SwingTwistConstraint.h:106
Vec3 GetLocalSpacePosition1() const
Definition: SwingTwistConstraint.h:99
const MotorSettings & GetSwingMotorSettings() const
Definition: SwingTwistConstraint.h:115
float GetPlaneHalfConeAngle() const
Definition: SwingTwistConstraint.h:107
float GetMaxFrictionTorque() const
Definition: SwingTwistConstraint.h:122
Quat GetConstraintToBody2() const
Definition: SwingTwistConstraint.h:102
Vec3 GetTargetAngularVelocityCS() const
Definition: SwingTwistConstraint.h:134
Vec3 GetTotalLambdaPosition() const
Definition: SwingTwistConstraint.h:149
void SetTargetOrientationBS(QuatArg inOrientation)
Definition: SwingTwistConstraint.h:142
Vec3 GetLocalSpacePosition2() const
Definition: SwingTwistConstraint.h:100
Quat GetTargetOrientationCS() const
Definition: SwingTwistConstraint.h:138
virtual Mat44 GetConstraintToBody1Matrix() const override
Calculates the transform that transforms from constraint space to body 1 space. The first column of t...
Definition: SwingTwistConstraint.h:95
void SetMaxFrictionTorque(float inFrictionTorque)
Definition: SwingTwistConstraint.h:121
MotorSettings & GetSwingMotorSettings()
Definition: SwingTwistConstraint.h:116
float GetTwistMinAngle() const
Definition: SwingTwistConstraint.h:109
const MotorSettings & GetTwistMotorSettings() const
Definition: SwingTwistConstraint.h:117
MotorSettings & GetTwistMotorSettings()
Definition: SwingTwistConstraint.h:118
float GetTotalLambdaTwist() const
Definition: SwingTwistConstraint.h:150
EMotorState GetSwingMotorState() const
Definition: SwingTwistConstraint.h:128
Definition: SwingTwistConstraintPart.h:33
Definition: SwingTwistConstraint.h:21
MotorSettings mSwingMotorSettings
Definition: SwingTwistConstraint.h:59
MotorSettings mTwistMotorSettings
Definition: SwingTwistConstraint.h:60
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