Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
PulleyConstraint.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2022 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
9
11
19{
20public:
22
23 // See: ConstraintSettings::SaveBinaryState
24 virtual void SaveBinaryState(StreamOut &inStream) const override;
25
27 virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
28
30 EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
31
34
37
40
43
45 float mRatio = 1.0f;
46
48 float mMinLength = 0.0f;
49
51 float mMaxLength = -1.0f;
52
53protected:
54 // See: ConstraintSettings::RestoreBinaryState
55 virtual void RestoreBinaryState(StreamIn &inStream) override;
56};
57
60{
61public:
63
65 PulleyConstraint(Body &inBody1, Body &inBody2, const PulleyConstraintSettings &inSettings);
66
67 // Generic interface of a constraint
68 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Pulley; }
69 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
70 virtual void SetupVelocityConstraint(float inDeltaTime) override;
71 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
72 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
73 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
74#ifdef JPH_DEBUG_RENDERER
75 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
76#endif // JPH_DEBUG_RENDERER
77 virtual void SaveState(StateRecorder &inStream) const override;
78 virtual void RestoreState(StateRecorder &inStream) override;
79 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
80
81 // See: TwoBodyConstraint
82 virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition1); }
83 virtual Mat44 GetConstraintToBody2Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition2); } // Note: Incorrect rotation as we don't track the original rotation difference, should not matter though as the constraint is not limiting rotation.
84
86 void SetLength(float inMinLength, float inMaxLength) { JPH_ASSERT(inMinLength >= 0.0f && inMinLength <= inMaxLength); mMinLength = inMinLength; mMaxLength = inMaxLength; }
87 float GetMinLength() const { return mMinLength; }
88 float GetMaxLength() const { return mMaxLength; }
89
91 float GetCurrentLength() const { return Vec3(mWorldSpacePosition1 - mFixedPosition1).Length() + mRatio * Vec3(mWorldSpacePosition2 - mFixedPosition2).Length(); }
92
94 inline float GetTotalLambdaPosition() const { return mIndependentAxisConstraintPart.GetTotalLambda(); }
95
96private:
97 // Calculates world positions and normals and returns current length
98 float CalculatePositionsNormalsAndLength();
99
100 // Internal helper function to calculate the values below
101 void CalculateConstraintProperties();
102
103 // CONFIGURATION PROPERTIES FOLLOW
104
105 // Local space constraint positions on the bodies
106 Vec3 mLocalSpacePosition1;
107 Vec3 mLocalSpacePosition2;
108
109 // World space fixed positions
110 RVec3 mFixedPosition1;
111 RVec3 mFixedPosition2;
112
114 float mRatio;
115
116 // The minimum/maximum length of the line segments
117 float mMinLength;
118 float mMaxLength;
119
120 // RUN TIME PROPERTIES FOLLOW
121
122 // World space positions and normal
123 RVec3 mWorldSpacePosition1;
124 RVec3 mWorldSpacePosition2;
125 Vec3 mWorldSpaceNormal1;
126 Vec3 mWorldSpaceNormal2;
127
128 // Depending on if the length < min or length > max we can apply forces to prevent further violations
129 float mMinLambda;
130 float mMaxLambda;
131
132 // The constraint part
133 IndependentAxisConstraintPart mIndependentAxisConstraintPart;
134};
135
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_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
#define JPH_DECLARE_SERIALIZABLE_VIRTUAL(class_name)
Definition: SerializableObject.h:100
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: IndependentAxisConstraintPart.h:48
float GetTotalLambda() const
Return lagrange multiplier.
Definition: IndependentAxisConstraintPart.h:167
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 sTranslation(Vec3Arg inV)
Get matrix that translates.
Definition: Mat44.inl:144
A pulley constraint.
Definition: PulleyConstraint.h:60
virtual EConstraintSubType GetSubType() const override
Get the sub type of a constraint.
Definition: PulleyConstraint.h:68
virtual void RestoreState(StateRecorder &inStream) override
Restoring state for replay.
Definition: PulleyConstraint.cpp:224
float GetCurrentLength() const
Get the current length of both segments (multiplied by the ratio for segment 2)
Definition: PulleyConstraint.h:91
void SetLength(float inMinLength, float inMaxLength)
Update the minimum and maximum length for the constraint.
Definition: PulleyConstraint.h:86
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: PulleyConstraint.cpp:233
virtual void SetupVelocityConstraint(float inDeltaTime) override
Definition: PulleyConstraint.cpp:141
virtual bool SolveVelocityConstraint(float inDeltaTime) override
Definition: PulleyConstraint.cpp:164
float GetMinLength() const
Definition: PulleyConstraint.h:87
virtual void DrawConstraint(DebugRenderer *inRenderer) const override
Definition: PulleyConstraint.cpp:195
virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override
Definition: PulleyConstraint.cpp:172
virtual Mat44 GetConstraintToBody1Matrix() const override
Calculates the transform that transforms from constraint space to body 1 space. The first column of t...
Definition: PulleyConstraint.h:82
virtual Mat44 GetConstraintToBody2Matrix() const override
Calculates the transform that transforms from constraint space to body 2 space. The first column of t...
Definition: PulleyConstraint.h:83
float GetMaxLength() const
Definition: PulleyConstraint.h:88
virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override
Definition: PulleyConstraint.cpp:103
virtual void SaveState(StateRecorder &inStream) const override
Saving state for replay.
Definition: PulleyConstraint.cpp:215
float GetTotalLambdaPosition() const
Definition: PulleyConstraint.h:94
virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override
Definition: PulleyConstraint.cpp:159
Definition: PulleyConstraint.h:19
RVec3 mFixedPoint2
Fixed world point to which body 2 is connected (always world space)
Definition: PulleyConstraint.h:42
virtual void RestoreBinaryState(StreamIn &inStream) override
This function should not be called directly, it is used by sRestoreFromBinaryState.
Definition: PulleyConstraint.cpp:48
float mRatio
Ratio between the two line segments (see formula above), can be used to create a block and tackle.
Definition: PulleyConstraint.h:45
RVec3 mFixedPoint1
Fixed world point to which body 1 is connected (always world space)
Definition: PulleyConstraint.h:36
float mMaxLength
The maximum length of the line segments (see formula above), use -1 to calculate the length based on ...
Definition: PulleyConstraint.h:51
virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override
Create an an instance of this constraint.
Definition: PulleyConstraint.cpp:62
RVec3 mBodyPoint2
Body 2 constraint attachment point (space determined by mSpace)
Definition: PulleyConstraint.h:39
EConstraintSpace mSpace
This determines in which space the constraint is setup, specified properties below should be in the s...
Definition: PulleyConstraint.h:30
virtual void SaveBinaryState(StreamOut &inStream) const override
Saves the contents of the constraint settings in binary form to inStream.
Definition: PulleyConstraint.cpp:34
RVec3 mBodyPoint1
Body 1 constraint attachment point (space determined by mSpace).
Definition: PulleyConstraint.h:33
float mMinLength
The minimum length of the line segments (see formula above), use -1 to calculate the length based on ...
Definition: PulleyConstraint.h:48
Definition: Reference.h:101
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
Base class for settings for all constraints that involve 2 bodies.
Definition: TwoBodyConstraint.h:16
Definition: Vec3.h:16
JPH_INLINE float Length() const
Length of vector.
Definition: Vec3.inl:669
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107