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
19{
20 Free,
26};
27
30{
31public:
33
34 // See: ConstraintSettings::SaveBinaryState
35 virtual void SaveBinaryState(StreamOut &inStream) const override;
36
38 virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
39
42
45
48
50 float mPathFraction = 0.0f;
51
53 float mMaxFrictionForce = 0.0f;
54
57
59 EPathRotationConstraintType mRotationConstraintType = EPathRotationConstraintType::Free;
60
61protected:
62 // See: ConstraintSettings::RestoreBinaryState
63 virtual void RestoreBinaryState(StreamIn &inStream) override;
64};
65
68{
69public:
71
73 PathConstraint(Body &inBody1, Body &inBody2, const PathConstraintSettings &inSettings);
74
75 // Generic interface of a constraint
76 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Path; }
77 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
78 virtual void SetupVelocityConstraint(float inDeltaTime) override;
79 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
80 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
81 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
82#ifdef JPH_DEBUG_RENDERER
83 virtual void DrawConstraint(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 bool IsActive() const override { return TwoBodyConstraint::IsActive() && mPath != nullptr; }
88 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
89
90 // See: TwoBodyConstraint
91 virtual Mat44 GetConstraintToBody1Matrix() const override { return mPathToBody1; }
92 virtual Mat44 GetConstraintToBody2Matrix() const override { return mPathToBody2; }
93
95 void SetPath(const PathConstraintPath *inPath, float inPathFraction);
96
98 const PathConstraintPath * GetPath() const { return mPath; }
99
101 float GetPathFraction() const { return mPathFraction; }
102
104 void SetMaxFrictionForce(float inFrictionForce) { mMaxFrictionForce = inFrictionForce; }
105 float GetMaxFrictionForce() const { return mMaxFrictionForce; }
106
108 MotorSettings & GetPositionMotorSettings() { return mPositionMotorSettings; }
109 const MotorSettings & GetPositionMotorSettings() const { return mPositionMotorSettings; }
110
111 // Position motor controls (drives body 2 along the path)
112 void SetPositionMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mPositionMotorSettings.IsValid()); mPositionMotorState = inState; }
113 EMotorState GetPositionMotorState() const { return mPositionMotorState; }
114 void SetTargetVelocity(float inVelocity) { mTargetVelocity = inVelocity; }
115 float GetTargetVelocity() const { return mTargetVelocity; }
116 void SetTargetPathFraction(float inFraction) { JPH_ASSERT(mPath->IsLooping() || (inFraction >= 0.0f && inFraction <= mPath->GetPathMaxFraction())); mTargetPathFraction = inFraction; }
117 float GetTargetPathFraction() const { return mTargetPathFraction; }
118
120 inline Vector<2> GetTotalLambdaPosition() const { return mPositionConstraintPart.GetTotalLambda(); }
121 inline float GetTotalLambdaPositionLimits() const { return mPositionLimitsConstraintPart.GetTotalLambda(); }
122 inline float GetTotalLambdaMotor() const { return mPositionMotorConstraintPart.GetTotalLambda(); }
123 inline Vector<2> GetTotalLambdaRotationHinge() const { return mHingeConstraintPart.GetTotalLambda(); }
124 inline Vec3 GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
125
126private:
127 // Internal helper function to calculate the values below
128 void CalculateConstraintProperties(float inDeltaTime);
129
130 // CONFIGURATION PROPERTIES FOLLOW
131
133 Mat44 mPathToBody1;
134 Mat44 mPathToBody2;
135 EPathRotationConstraintType mRotationConstraintType;
136
137 // Friction
138 float mMaxFrictionForce;
139
140 // Motor controls
141 MotorSettings mPositionMotorSettings;
142 EMotorState mPositionMotorState = EMotorState::Off;
143 float mTargetVelocity = 0.0f;
144 float mTargetPathFraction = 0.0f;
145
146 // RUN TIME PROPERTIES FOLLOW
147
148 // Positions where the point constraint acts on in world space
149 Vec3 mR1;
150 Vec3 mR2;
151
152 // X2 + R2 - X1 - R1
153 Vec3 mU;
154
155 // World space path tangent
156 Vec3 mPathTangent;
157
158 // Normals to the path tangent
159 Vec3 mPathNormal;
160 Vec3 mPathBinormal;
161
162 // Inverse of initial rotation from body 1 to body 2 in body 1 space (only used when rotation constraint type is FullyConstrained)
163 Quat mInvInitialOrientation;
164
165 // Current fraction along the path where body 2 is attached
166 float mPathFraction = 0.0f;
167
168 // Translation constraint parts
169 DualAxisConstraintPart mPositionConstraintPart;
170 AxisConstraintPart mPositionLimitsConstraintPart;
171 AxisConstraintPart mPositionMotorConstraintPart;
172
173 // Rotation constraint parts
174 HingeRotationConstraintPart mHingeConstraintPart;
175 RotationEulerConstraintPart mRotationConstraintPart;
176};
177
EConstraintSubType
Enum to identify constraint sub type.
Definition: Constraint.h:34
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
EMotorState
Definition: MotorSettings.h:16
EPathRotationConstraintType
How to constrain the rotation of the body to a PathConstraint.
Definition: PathConstraint.h:19
@ FullyConstrained
Fully constrain the rotation of the body 2 to the rotation of body 1.
@ ConstaintToPath
Fully constrain the rotation of body 2 to the path (follwing the tangent and normal of the path)
@ 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(class_name)
Definition: SerializableObject.h:100
Definition: AxisConstraintPart.h:42
float GetTotalLambda() const
Return lagrange multiplier.
Definition: AxisConstraintPart.h:378
Definition: Body.h:33
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition: BodyID.h:13
Simple triangle renderer for debugging purposes.
Definition: DebugRenderer.h:25
Definition: DualAxisConstraintPart.h:46
const Vec2 & GetTotalLambda() const
Return lagrange multiplier.
Definition: DualAxisConstraintPart.h:247
Definition: HingeRotationConstraintPart.h:42
const Vec2 & GetTotalLambda() const
Return lagrange multiplier.
Definition: HingeRotationConstraintPart.h:194
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:25
bool IsValid() const
Check if settings are valid.
Definition: MotorSettings.h:48
Path constraint, used to constrain the degrees of freedom between two bodies to a path.
Definition: PathConstraint.h:68
float GetMaxFrictionForce() const
Definition: PathConstraint.h:105
void SetTargetVelocity(float inVelocity)
Definition: PathConstraint.h:114
void SetTargetPathFraction(float inFraction)
Definition: PathConstraint.h:116
EMotorState GetPositionMotorState() const
Definition: PathConstraint.h:113
virtual void SetupVelocityConstraint(float inDeltaTime) override
Definition: PathConstraint.cpp:220
virtual void SaveState(StateRecorder &inStream) const override
Saving state for replay.
Definition: PathConstraint.cpp:404
float GetTargetPathFraction() const
Definition: PathConstraint.h:117
virtual bool SolveVelocityConstraint(float inDeltaTime) override
Definition: PathConstraint.cpp:251
virtual void DrawConstraint(DebugRenderer *inRenderer) const override
Definition: PathConstraint.cpp:360
virtual bool IsActive() const override
Solver interface.
Definition: PathConstraint.h:87
void SetPositionMotorState(EMotorState inState)
Definition: PathConstraint.h:112
virtual EConstraintSubType GetSubType() const override
Get the sub type of a constraint.
Definition: PathConstraint.h:76
Vec3 GetTotalLambdaRotation() const
Definition: PathConstraint.h:124
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:91
virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override
Definition: PathConstraint.cpp:225
virtual Ref< ConstraintSettings > GetConstraintSettings() const override
Debug function to convert a constraint to its settings, note that this will not save to which bodies ...
Definition: PathConstraint.cpp:440
virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override
Definition: PathConstraint.cpp:315
float GetTargetVelocity() const
Definition: PathConstraint.h:115
Vector< 2 > GetTotalLambdaPosition() const
Definition: PathConstraint.h:120
MotorSettings & GetPositionMotorSettings()
Position motor settings.
Definition: PathConstraint.h:108
const PathConstraintPath * GetPath() const
Access to the current path.
Definition: PathConstraint.h:98
Vector< 2 > GetTotalLambdaRotationHinge() const
Definition: PathConstraint.h:123
void SetPath(const PathConstraintPath *inPath, float inPathFraction)
Update the path for this constraint.
Definition: PathConstraint.cpp:85
void SetMaxFrictionForce(float inFrictionForce)
Friction control.
Definition: PathConstraint.h:104
float GetTotalLambdaMotor() const
Definition: PathConstraint.h:122
virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override
Definition: PathConstraint.cpp:77
virtual void RestoreState(StateRecorder &inStream) override
Restoring state for replay.
Definition: PathConstraint.cpp:422
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:92
float GetPathFraction() const
Access to the current fraction along the path e [0, GetPath()->GetMaxPathFraction()].
Definition: PathConstraint.h:101
const MotorSettings & GetPositionMotorSettings() const
Definition: PathConstraint.h:109
float GetTotalLambdaPositionLimits() const
Definition: PathConstraint.h:121
The path for a path constraint. It allows attaching two bodies to each other while giving the second ...
Definition: PathConstraintPath.h:21
bool IsLooping() const
Definition: PathConstraintPath.h:48
Path constraint settings, used to constrain the degrees of freedom between two bodies to a path.
Definition: PathConstraint.h:30
virtual void RestoreBinaryState(StreamIn &inStream) override
This function should not be called directly, it is used by sRestoreFromBinaryState.
Definition: PathConstraint.cpp:45
RefConst< PathConstraintPath > mPath
The path that constrains the two bodies.
Definition: PathConstraint.h:41
MotorSettings mPositionMotorSettings
In case the constraint is powered, this determines the motor settings along the path.
Definition: PathConstraint.h:56
Quat mPathRotation
The rotation of the path start relative to world transform of body 1.
Definition: PathConstraint.h:47
virtual void SaveBinaryState(StreamOut &inStream) const override
Saves the contents of the constraint settings in binary form to inStream.
Definition: PathConstraint.cpp:32
virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override
Create an an instance of this constraint.
Definition: PathConstraint.cpp:60
Vec3 mPathPosition
The position of the path start relative to world transform of body 1.
Definition: PathConstraint.h:44
float mMaxFrictionForce
Maximum amount of friction force to apply (N) when not driven by a motor.
Definition: PathConstraint.h:53
float mPathFraction
The fraction along the path that corresponds to the initial position of body 2. Usually this is 0,...
Definition: PathConstraint.h:50
EPathRotationConstraintType mRotationConstraintType
How to constrain the rotation of the body to the path.
Definition: PathConstraint.h:59
Definition: Quat.h:33
static JPH_INLINE Quat sIdentity()
Definition: Quat.h:93
Definition: Reference.h:154
Definition: Reference.h:101
Definition: RotationEulerConstraintPart.h:36
Vec3 GetTotalLambda() const
Return lagrange multiplier.
Definition: RotationEulerConstraintPart.h:245
Definition: StateRecorder.h:15
Simple binary input stream.
Definition: StreamIn.h:11
Simple binary output stream.
Definition: StreamOut.h:11
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
Definition: Vec3.h:16
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107
Templatized vector class.
Definition: Vector.h:12