Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
PathConstraint.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
14
16
17JPH_SUPPRESS_WARNING_PUSH
18JPH_GCC_SUPPRESS_WARNING("-Wshadow") // GCC complains about the 'Free' value conflicting with the 'Free' method
19
20
30
31JPH_SUPPRESS_WARNING_POP
32
42{
44
45public:
46 // See: ConstraintSettings::SaveBinaryState
47 virtual void SaveBinaryState(StreamOut &inStream) const override;
48
50 virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
51
54
56 Vec3 mPathPosition = Vec3::sZero();
57
59 Quat mPathRotation = Quat::sIdentity();
60
62 float mPathFraction = 0.0f;
63
65 float mMaxFrictionForce = 0.0f;
66
69
72
73protected:
74 // See: ConstraintSettings::RestoreBinaryState
75 virtual void RestoreBinaryState(StreamIn &inStream) override;
76};
77
80{
81public:
83
85 PathConstraint(Body &inBody1, Body &inBody2, const PathConstraintSettings &inSettings);
86
87 // Generic interface of a constraint
88 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Path; }
89 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
90 virtual void SetupVelocityConstraint(float inDeltaTime) override;
91 virtual void ResetWarmStart() override;
92 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
93 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
94 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
95#ifdef JPH_DEBUG_RENDERER
96 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
97#endif // JPH_DEBUG_RENDERER
98 virtual void SaveState(StateRecorder &inStream) const override;
99 virtual void RestoreState(StateRecorder &inStream) override;
100 virtual bool IsActive() const override { return TwoBodyConstraint::IsActive() && mPath != nullptr; }
101 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
102
103 // See: TwoBodyConstraint
104 virtual Mat44 GetConstraintToBody1Matrix() const override { return mPathToBody1; }
105 virtual Mat44 GetConstraintToBody2Matrix() const override { return mPathToBody2; }
106
108 void SetPath(const PathConstraintPath *inPath, float inPathFraction);
109
111 const PathConstraintPath * GetPath() const { return mPath; }
112
114 float GetPathFraction() const { return mPathFraction; }
115
117 void SetMaxFrictionForce(float inFrictionForce) { mMaxFrictionForce = inFrictionForce; }
118 float GetMaxFrictionForce() const { return mMaxFrictionForce; }
119
121 MotorSettings & GetPositionMotorSettings() { return mPositionMotorSettings; }
122 const MotorSettings & GetPositionMotorSettings() const { return mPositionMotorSettings; }
123
124 // Position motor controls (drives body 2 along the path)
125 void SetPositionMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mPositionMotorSettings.IsValid()); mPositionMotorState = inState; }
126 EMotorState GetPositionMotorState() const { return mPositionMotorState; }
127 void SetTargetVelocity(float inVelocity) { mTargetVelocity = inVelocity; }
128 float GetTargetVelocity() const { return mTargetVelocity; }
129 void SetTargetPathFraction(float inFraction) { JPH_ASSERT(mPath->IsLooping() || (inFraction >= 0.0f && inFraction <= mPath->GetPathMaxFraction())); mTargetPathFraction = inFraction; }
130 float GetTargetPathFraction() const { return mTargetPathFraction; }
131
133 inline Vector<2> GetTotalLambdaPosition() const { return mPositionConstraintPart.GetTotalLambda(); }
134 inline float GetTotalLambdaPositionLimits() const { return mPositionLimitsConstraintPart.GetTotalLambda(); }
135 inline float GetTotalLambdaMotor() const { return mPositionMotorConstraintPart.GetTotalLambda(); }
136 inline Vector<2> GetTotalLambdaRotationHinge() const { return mHingeConstraintPart.GetTotalLambda(); }
137 inline Vec3 GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
138
139private:
140 // Internal helper function to calculate the values below
141 void CalculateConstraintProperties(float inDeltaTime);
142
143 // CONFIGURATION PROPERTIES FOLLOW
144
146 Mat44 mPathToBody1;
147 Mat44 mPathToBody2;
148 EPathRotationConstraintType mRotationConstraintType;
149
150 // Friction
151 float mMaxFrictionForce;
152
153 // Motor controls
154 MotorSettings mPositionMotorSettings;
155 EMotorState mPositionMotorState = EMotorState::Off;
156 float mTargetVelocity = 0.0f;
157 float mTargetPathFraction = 0.0f;
158
159 // RUN TIME PROPERTIES FOLLOW
160
161 // Positions where the point constraint acts on in world space
162 Vec3 mR1;
163 Vec3 mR2;
164
165 // X2 + R2 - X1 - R1
166 Vec3 mU;
167
168 // World space path tangent
169 Vec3 mPathTangent;
170
171 // Normals to the path tangent
172 Vec3 mPathNormal;
173 Vec3 mPathBinormal;
174
175 // Inverse of initial rotation from body 1 to body 2 in body 1 space (only used when rotation constraint type is FullyConstrained)
176 Quat mInvInitialOrientation;
177
178 // Current fraction along the path where body 2 is attached
179 float mPathFraction = 0.0f;
180
181 // Translation constraint parts
182 DualAxisConstraintPart mPositionConstraintPart;
183 AxisConstraintPart mPositionLimitsConstraintPart;
184 AxisConstraintPart mPositionMotorConstraintPart;
185
186 // Rotation constraint parts
187 HingeRotationConstraintPart mHingeConstraintPart;
188 RotationEulerConstraintPart mRotationConstraintPart;
189};
190
EConstraintSubType
Enum to identify constraint sub type.
Definition Constraint.h:34
#define JPH_EXPORT
Definition Core.h:271
#define JPH_GCC_SUPPRESS_WARNING(w)
Definition Core.h:308
#define JPH_NAMESPACE_END
Definition Core.h:414
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
#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.
EPathRotationConstraintType
How to constrain the rotation of the body to a PathConstraint.
Definition PathConstraint.h:22
@ ConstrainToPath
Fully constrain the rotation of body 2 to the path (following the tangent and normal of the path)
@ FullyConstrained
Fully constrain the rotation of the body 2 to the rotation of body 1.
@ Free
Do not constrain the rotation of the body at all.
@ ConstrainAroundNormal
Only allow rotation around the normal vector (perpendicular to the path)
@ ConstrainAroundBinormal
Only allow rotation around the binormal vector (perpendicular to the path)
@ ConstrainAroundTangent
Only allow rotation around the tangent vector (following the path)
#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
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
Path constraint, used to constrain the degrees of freedom between two bodies to a path.
Definition PathConstraint.h:80
float GetMaxFrictionForce() const
Definition PathConstraint.h:118
void SetTargetVelocity(float inVelocity)
Definition PathConstraint.h:127
void SetTargetPathFraction(float inFraction)
Definition PathConstraint.h:129
EMotorState GetPositionMotorState() const
Definition PathConstraint.h:126
float GetTargetPathFraction() const
Definition PathConstraint.h:130
virtual bool IsActive() const override
Solver interface.
Definition PathConstraint.h:100
void SetPositionMotorState(EMotorState inState)
Definition PathConstraint.h:125
virtual EConstraintSubType GetSubType() const override
Get the sub type of a constraint.
Definition PathConstraint.h:88
Vec3 GetTotalLambdaRotation() const
Definition PathConstraint.h:137
virtual Mat44 GetConstraintToBody1Matrix() const override
Calculates the transform that transforms from constraint space to body 1 space. The first column of t...
Definition PathConstraint.h:104
float GetTargetVelocity() const
Definition PathConstraint.h:128
Vector< 2 > GetTotalLambdaPosition() const
Definition PathConstraint.h:133
MotorSettings & GetPositionMotorSettings()
Position motor settings.
Definition PathConstraint.h:121
const PathConstraintPath * GetPath() const
Access to the current path.
Definition PathConstraint.h:111
Vector< 2 > GetTotalLambdaRotationHinge() const
Definition PathConstraint.h:136
void SetMaxFrictionForce(float inFrictionForce)
Friction control.
Definition PathConstraint.h:117
float GetTotalLambdaMotor() const
Definition PathConstraint.h:135
virtual Mat44 GetConstraintToBody2Matrix() const override
Calculates the transform that transforms from constraint space to body 2 space. The first column of t...
Definition PathConstraint.h:105
float GetPathFraction() const
Access to the current fraction along the path e [0, GetPath()->GetMaxPathFraction()].
Definition PathConstraint.h:114
const MotorSettings & GetPositionMotorSettings() const
Definition PathConstraint.h:122
float GetTotalLambdaPositionLimits() const
Definition PathConstraint.h:134
The path for a path constraint. It allows attaching two bodies to each other while giving the second ...
Definition PathConstraintPath.h:21
Definition PathConstraint.h:42
RefConst< PathConstraintPath > mPath
The path that constrains the two bodies.
Definition PathConstraint.h:53
MotorSettings mPositionMotorSettings
In case the constraint is powered, this determines the motor settings along the path.
Definition PathConstraint.h:68
Definition Quat.h:33
static JPH_INLINE Quat sIdentity()
Definition Quat.h:103
Definition Reference.h:163
Definition Reference.h:107
Definition RotationEulerConstraintPart.h:36
Definition StateRecorder.h:110
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
virtual bool IsActive() const override
Solver interface.
Definition TwoBodyConstraint.h:38
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 sZero()
Vector with all zeros.
Definition Vec3.inl:103
Templatized vector class.
Definition Vector.h:12