Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
SliderConstraint.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
26 virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
27
29 void SetSliderAxis(Vec3Arg inSliderAxis);
30
33
35 bool mAutoDetectPoint = false;
36
39 RVec3 mPoint1 = RVec3::sZero();
40 Vec3 mSliderAxis1 = Vec3::sAxisX();
41 Vec3 mNormalAxis1 = Vec3::sAxisY();
42
44 RVec3 mPoint2 = RVec3::sZero();
45 Vec3 mSliderAxis2 = Vec3::sAxisX();
46 Vec3 mNormalAxis2 = Vec3::sAxisY();
47
49 float mLimitsMin = -FLT_MAX;
50 float mLimitsMax = FLT_MAX;
51
54
56 float mMaxFrictionForce = 0.0f;
57
60
61protected:
62 // See: ConstraintSettings::RestoreBinaryState
63 virtual void RestoreBinaryState(StreamIn &inStream) override;
64};
65
68{
69public:
71
73 SliderConstraint(Body &inBody1, Body &inBody2, const SliderConstraintSettings &inSettings);
74
75 // Generic interface of a constraint
76 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Slider; }
77 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
78 virtual void SetupVelocityConstraint(float inDeltaTime) override;
79 virtual void ResetWarmStart() override;
80 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
81 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
82 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
83#ifdef JPH_DEBUG_RENDERER
84 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
85 virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
86#endif // JPH_DEBUG_RENDERER
87 virtual void SaveState(StateRecorder &inStream) const override;
88 virtual void RestoreState(StateRecorder &inStream) override;
89 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
90
91 // See: TwoBodyConstraint
92 virtual Mat44 GetConstraintToBody1Matrix() const override;
93 virtual Mat44 GetConstraintToBody2Matrix() const override;
94
96 float GetCurrentPosition() const;
97
99 void SetMaxFrictionForce(float inFrictionForce) { mMaxFrictionForce = inFrictionForce; }
100 float GetMaxFrictionForce() const { return mMaxFrictionForce; }
101
103 MotorSettings & GetMotorSettings() { return mMotorSettings; }
104 const MotorSettings & GetMotorSettings() const { return mMotorSettings; }
105
106 // Motor controls
107 void SetMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mMotorSettings.IsValid()); mMotorState = inState; }
108 EMotorState GetMotorState() const { return mMotorState; }
109 void SetTargetVelocity(float inVelocity) { mTargetVelocity = inVelocity; }
110 float GetTargetVelocity() const { return mTargetVelocity; }
111 void SetTargetPosition(float inPosition) { mTargetPosition = mHasLimits? Clamp(inPosition, mLimitsMin, mLimitsMax) : inPosition; }
112 float GetTargetPosition() const { return mTargetPosition; }
113
115 void SetLimits(float inLimitsMin, float inLimitsMax);
116 float GetLimitsMin() const { return mLimitsMin; }
117 float GetLimitsMax() const { return mLimitsMax; }
118 bool HasLimits() const { return mHasLimits; }
119
121 const SpringSettings & GetLimitsSpringSettings() const { return mLimitsSpringSettings; }
122 SpringSettings & GetLimitsSpringSettings() { return mLimitsSpringSettings; }
123 void SetLimitsSpringSettings(const SpringSettings &inLimitsSpringSettings) { mLimitsSpringSettings = inLimitsSpringSettings; }
124
126 inline Vector<2> GetTotalLambdaPosition() const { return mPositionConstraintPart.GetTotalLambda(); }
127 inline float GetTotalLambdaPositionLimits() const { return mPositionLimitsConstraintPart.GetTotalLambda(); }
128 inline Vec3 GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
129 inline float GetTotalLambdaMotor() const { return mMotorConstraintPart.GetTotalLambda(); }
130
131private:
132 // Internal helper function to calculate the values below
133 void CalculateR1R2U(Mat44Arg inRotation1, Mat44Arg inRotation2);
134 void CalculateSlidingAxisAndPosition(Mat44Arg inRotation1);
135 void CalculatePositionConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);
136 void CalculatePositionLimitsConstraintProperties(float inDeltaTime);
137 void CalculateMotorConstraintProperties(float inDeltaTime);
138
139 // CONFIGURATION PROPERTIES FOLLOW
140
141 // Local space constraint positions
142 Vec3 mLocalSpacePosition1;
143 Vec3 mLocalSpacePosition2;
144
145 // Local space sliding direction
146 Vec3 mLocalSpaceSliderAxis1;
147
148 // Local space normals to the sliding direction (in body 1 space)
149 Vec3 mLocalSpaceNormal1;
150 Vec3 mLocalSpaceNormal2;
151
152 // Inverse of initial rotation from body 1 to body 2 in body 1 space
153 Quat mInvInitialOrientation;
154
155 // Slider limits
156 bool mHasLimits;
157 float mLimitsMin;
158 float mLimitsMax;
159
160 // Soft constraint limits
161 SpringSettings mLimitsSpringSettings;
162
163 // Friction
164 float mMaxFrictionForce;
165
166 // Motor controls
167 MotorSettings mMotorSettings;
168 EMotorState mMotorState = EMotorState::Off;
169 float mTargetVelocity = 0.0f;
170 float mTargetPosition = 0.0f;
171
172 // RUN TIME PROPERTIES FOLLOW
173
174 // Positions where the point constraint acts on (middle point between center of masses)
175 Vec3 mR1;
176 Vec3 mR2;
177
178 // X2 + R2 - X1 - R1
179 Vec3 mU;
180
181 // World space sliding direction
182 Vec3 mWorldSpaceSliderAxis;
183
184 // Normals to the slider axis
185 Vec3 mN1;
186 Vec3 mN2;
187
188 // Distance along the slide axis
189 float mD = 0.0f;
190
191 // The constraint parts
192 DualAxisConstraintPart mPositionConstraintPart;
193 RotationEulerConstraintPart mRotationConstraintPart;
194 AxisConstraintPart mPositionLimitsConstraintPart;
195 AxisConstraintPart mMotorConstraintPart;
196};
197
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 AxisConstraintPart.h:43
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
Definition DualAxisConstraintPart.h:48
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 Quat.h:33
Definition Reference.h:107
Definition RotationEulerConstraintPart.h:36
A slider constraint allows movement in only 1 axis (and no rotation). Also known as a prismatic const...
Definition SliderConstraint.h:68
void SetMaxFrictionForce(float inFrictionForce)
Friction control.
Definition SliderConstraint.h:99
Vec3 GetTotalLambdaRotation() const
Definition SliderConstraint.h:128
float GetLimitsMin() const
Definition SliderConstraint.h:116
const MotorSettings & GetMotorSettings() const
Definition SliderConstraint.h:104
virtual EConstraintSubType GetSubType() const override
Get the sub type of a constraint.
Definition SliderConstraint.h:76
float GetTotalLambdaPositionLimits() const
Definition SliderConstraint.h:127
bool HasLimits() const
Definition SliderConstraint.h:118
Vector< 2 > GetTotalLambdaPosition() const
Definition SliderConstraint.h:126
SpringSettings & GetLimitsSpringSettings()
Definition SliderConstraint.h:122
float GetTotalLambdaMotor() const
Definition SliderConstraint.h:129
float GetLimitsMax() const
Definition SliderConstraint.h:117
void SetMotorState(EMotorState inState)
Definition SliderConstraint.h:107
float GetTargetPosition() const
Definition SliderConstraint.h:112
void SetLimitsSpringSettings(const SpringSettings &inLimitsSpringSettings)
Definition SliderConstraint.h:123
const SpringSettings & GetLimitsSpringSettings() const
Update the limits spring settings.
Definition SliderConstraint.h:121
void SetTargetVelocity(float inVelocity)
Definition SliderConstraint.h:109
float GetTargetVelocity() const
Definition SliderConstraint.h:110
MotorSettings & GetMotorSettings()
Motor settings.
Definition SliderConstraint.h:103
float GetMaxFrictionForce() const
Definition SliderConstraint.h:100
void SetTargetPosition(float inPosition)
Definition SliderConstraint.h:111
EMotorState GetMotorState() const
Definition SliderConstraint.h:108
Slider constraint settings, used to create a slider constraint.
Definition SliderConstraint.h:17
MotorSettings mMotorSettings
In case the constraint is powered, this determines the motor settings around the sliding axis.
Definition SliderConstraint.h:59
SpringSettings mLimitsSpringSettings
When enabled, this makes the limits soft. When the constraint exceeds the limits, a spring force will...
Definition SliderConstraint.h:53
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