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
33 RVec3 mBodyPoint1 = RVec3::sZero();
34
36 RVec3 mFixedPoint1 = RVec3::sZero();
37
39 RVec3 mBodyPoint2 = RVec3::sZero();
40
42 RVec3 mFixedPoint2 = RVec3::sZero();
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 ResetWarmStart() override;
72 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
73 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
74 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
75#ifdef JPH_DEBUG_RENDERER
76 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
77#endif // JPH_DEBUG_RENDERER
78 virtual void SaveState(StateRecorder &inStream) const override;
79 virtual void RestoreState(StateRecorder &inStream) override;
80 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
81
82 // See: TwoBodyConstraint
83 virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition1); }
84 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.
85
87 void SetLength(float inMinLength, float inMaxLength) { JPH_ASSERT(inMinLength >= 0.0f && inMinLength <= inMaxLength); mMinLength = inMinLength; mMaxLength = inMaxLength; }
88 float GetMinLength() const { return mMinLength; }
89 float GetMaxLength() const { return mMaxLength; }
90
92 float GetCurrentLength() const { return Vec3(mWorldSpacePosition1 - mFixedPosition1).Length() + mRatio * Vec3(mWorldSpacePosition2 - mFixedPosition2).Length(); }
93
95 inline float GetTotalLambdaPosition() const { return mIndependentAxisConstraintPart.GetTotalLambda(); }
96
97private:
98 // Calculates world positions and normals and returns current length
99 float CalculatePositionsNormalsAndLength();
100
101 // Internal helper function to calculate the values below
102 void CalculateConstraintProperties();
103
104 // CONFIGURATION PROPERTIES FOLLOW
105
106 // Local space constraint positions on the bodies
107 Vec3 mLocalSpacePosition1;
108 Vec3 mLocalSpacePosition2;
109
110 // World space fixed positions
111 RVec3 mFixedPosition1;
112 RVec3 mFixedPosition2;
113
115 float mRatio;
116
117 // The minimum/maximum length of the line segments
118 float mMinLength;
119 float mMaxLength;
120
121 // RUN TIME PROPERTIES FOLLOW
122
123 // World space positions and normal
124 RVec3 mWorldSpacePosition1;
125 RVec3 mWorldSpacePosition2;
126 Vec3 mWorldSpaceNormal1;
127 Vec3 mWorldSpaceNormal2;
128
129 // Depending on if the length < min or length > max we can apply forces to prevent further violations
130 float mMinLambda;
131 float mMaxLambda;
132
133 // The constraint part
134 IndependentAxisConstraintPart mIndependentAxisConstraintPart;
135};
136
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_EXPORT
Definition: Core.h:227
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#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(linkage, class_name)
Definition: SerializableObject.h:100
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: IndependentAxisConstraintPart.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
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
float GetCurrentLength() const
Get the current length of both segments (multiplied by the ratio for segment 2)
Definition: PulleyConstraint.h:92
void SetLength(float inMinLength, float inMaxLength)
Update the minimum and maximum length for the constraint.
Definition: PulleyConstraint.h:87
float GetMinLength() const
Definition: PulleyConstraint.h:88
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:83
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:84
float GetMaxLength() const
Definition: PulleyConstraint.h:89
float GetTotalLambdaPosition() const
Definition: PulleyConstraint.h:95
Definition: PulleyConstraint.h:19
Definition: Reference.h:101
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
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: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