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
37{
38public:
40
41 // See: ConstraintSettings::SaveBinaryState
42 virtual void SaveBinaryState(StreamOut &inStream) const override;
43
45 virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
46
49
51 Vec3 mPathPosition = Vec3::sZero();
52
54 Quat mPathRotation = Quat::sIdentity();
55
57 float mPathFraction = 0.0f;
58
60 float mMaxFrictionForce = 0.0f;
61
64
66 EPathRotationConstraintType mRotationConstraintType = EPathRotationConstraintType::Free;
67
68protected:
69 // See: ConstraintSettings::RestoreBinaryState
70 virtual void RestoreBinaryState(StreamIn &inStream) override;
71};
72
75{
76public:
78
80 PathConstraint(Body &inBody1, Body &inBody2, const PathConstraintSettings &inSettings);
81
82 // Generic interface of a constraint
83 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Path; }
84 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
85 virtual void SetupVelocityConstraint(float inDeltaTime) override;
86 virtual void ResetWarmStart() override;
87 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
88 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
89 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
90#ifdef JPH_DEBUG_RENDERER
91 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
92#endif // JPH_DEBUG_RENDERER
93 virtual void SaveState(StateRecorder &inStream) const override;
94 virtual void RestoreState(StateRecorder &inStream) override;
95 virtual bool IsActive() const override { return TwoBodyConstraint::IsActive() && mPath != nullptr; }
96 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
97
98 // See: TwoBodyConstraint
99 virtual Mat44 GetConstraintToBody1Matrix() const override { return mPathToBody1; }
100 virtual Mat44 GetConstraintToBody2Matrix() const override { return mPathToBody2; }
101
103 void SetPath(const PathConstraintPath *inPath, float inPathFraction);
104
106 const PathConstraintPath * GetPath() const { return mPath; }
107
109 float GetPathFraction() const { return mPathFraction; }
110
112 void SetMaxFrictionForce(float inFrictionForce) { mMaxFrictionForce = inFrictionForce; }
113 float GetMaxFrictionForce() const { return mMaxFrictionForce; }
114
116 MotorSettings & GetPositionMotorSettings() { return mPositionMotorSettings; }
117 const MotorSettings & GetPositionMotorSettings() const { return mPositionMotorSettings; }
118
119 // Position motor controls (drives body 2 along the path)
120 void SetPositionMotorState(EMotorState inState) { JPH_ASSERT(inState == EMotorState::Off || mPositionMotorSettings.IsValid()); mPositionMotorState = inState; }
121 EMotorState GetPositionMotorState() const { return mPositionMotorState; }
122 void SetTargetVelocity(float inVelocity) { mTargetVelocity = inVelocity; }
123 float GetTargetVelocity() const { return mTargetVelocity; }
124 void SetTargetPathFraction(float inFraction) { JPH_ASSERT(mPath->IsLooping() || (inFraction >= 0.0f && inFraction <= mPath->GetPathMaxFraction())); mTargetPathFraction = inFraction; }
125 float GetTargetPathFraction() const { return mTargetPathFraction; }
126
128 inline Vector<2> GetTotalLambdaPosition() const { return mPositionConstraintPart.GetTotalLambda(); }
129 inline float GetTotalLambdaPositionLimits() const { return mPositionLimitsConstraintPart.GetTotalLambda(); }
130 inline float GetTotalLambdaMotor() const { return mPositionMotorConstraintPart.GetTotalLambda(); }
131 inline Vector<2> GetTotalLambdaRotationHinge() const { return mHingeConstraintPart.GetTotalLambda(); }
132 inline Vec3 GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
133
134private:
135 // Internal helper function to calculate the values below
136 void CalculateConstraintProperties(float inDeltaTime);
137
138 // CONFIGURATION PROPERTIES FOLLOW
139
141 Mat44 mPathToBody1;
142 Mat44 mPathToBody2;
143 EPathRotationConstraintType mRotationConstraintType;
144
145 // Friction
146 float mMaxFrictionForce;
147
148 // Motor controls
149 MotorSettings mPositionMotorSettings;
150 EMotorState mPositionMotorState = EMotorState::Off;
151 float mTargetVelocity = 0.0f;
152 float mTargetPathFraction = 0.0f;
153
154 // RUN TIME PROPERTIES FOLLOW
155
156 // Positions where the point constraint acts on in world space
157 Vec3 mR1;
158 Vec3 mR2;
159
160 // X2 + R2 - X1 - R1
161 Vec3 mU;
162
163 // World space path tangent
164 Vec3 mPathTangent;
165
166 // Normals to the path tangent
167 Vec3 mPathNormal;
168 Vec3 mPathBinormal;
169
170 // Inverse of initial rotation from body 1 to body 2 in body 1 space (only used when rotation constraint type is FullyConstrained)
171 Quat mInvInitialOrientation;
172
173 // Current fraction along the path where body 2 is attached
174 float mPathFraction = 0.0f;
175
176 // Translation constraint parts
177 DualAxisConstraintPart mPositionConstraintPart;
178 AxisConstraintPart mPositionLimitsConstraintPart;
179 AxisConstraintPart mPositionMotorConstraintPart;
180
181 // Rotation constraint parts
182 HingeRotationConstraintPart mHingeConstraintPart;
183 RotationEulerConstraintPart mRotationConstraintPart;
184};
185
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:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
#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
EPathRotationConstraintType
How to constrain the rotation of the body to a PathConstraint.
Definition: PathConstraint.h:19
@ 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:75
float GetMaxFrictionForce() const
Definition: PathConstraint.h:113
void SetTargetVelocity(float inVelocity)
Definition: PathConstraint.h:122
void SetTargetPathFraction(float inFraction)
Definition: PathConstraint.h:124
EMotorState GetPositionMotorState() const
Definition: PathConstraint.h:121
float GetTargetPathFraction() const
Definition: PathConstraint.h:125
virtual bool IsActive() const override
Solver interface.
Definition: PathConstraint.h:95
void SetPositionMotorState(EMotorState inState)
Definition: PathConstraint.h:120
virtual EConstraintSubType GetSubType() const override
Get the sub type of a constraint.
Definition: PathConstraint.h:83
Vec3 GetTotalLambdaRotation() const
Definition: PathConstraint.h:132
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:99
float GetTargetVelocity() const
Definition: PathConstraint.h:123
Vector< 2 > GetTotalLambdaPosition() const
Definition: PathConstraint.h:128
MotorSettings & GetPositionMotorSettings()
Position motor settings.
Definition: PathConstraint.h:116
const PathConstraintPath * GetPath() const
Access to the current path.
Definition: PathConstraint.h:106
Vector< 2 > GetTotalLambdaRotationHinge() const
Definition: PathConstraint.h:131
void SetMaxFrictionForce(float inFrictionForce)
Friction control.
Definition: PathConstraint.h:112
float GetTotalLambdaMotor() const
Definition: PathConstraint.h:130
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:100
float GetPathFraction() const
Access to the current fraction along the path e [0, GetPath()->GetMaxPathFraction()].
Definition: PathConstraint.h:109
const MotorSettings & GetPositionMotorSettings() const
Definition: PathConstraint.h:117
float GetTotalLambdaPositionLimits() const
Definition: PathConstraint.h:129
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:37
RefConst< PathConstraintPath > mPath
The path that constrains the two bodies.
Definition: PathConstraint.h:48
MotorSettings mPositionMotorSettings
In case the constraint is powered, this determines the motor settings along the path.
Definition: PathConstraint.h:63
Definition: Quat.h:33
static JPH_INLINE Quat sIdentity()
Definition: Quat.h:103
Definition: Reference.h:151
Definition: Reference.h:101
Definition: RotationEulerConstraintPart.h:36
Definition: StateRecorder.h:48
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:107
Templatized vector class.
Definition: Vector.h:12