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 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_EXPORT
Definition: Core.h:214
#define JPH_NAMESPACE_END
Definition: Core.h:354
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:348
#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
Simple triangle renderer for debugging purposes.
Definition: DebugRenderer.h:30
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:91
void SetLength(float inMinLength, float inMaxLength)
Update the minimum and maximum length for the constraint.
Definition: PulleyConstraint.h:86
float GetMinLength() const
Definition: PulleyConstraint.h:87
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
float GetTotalLambdaPosition() const
Definition: PulleyConstraint.h:94
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