Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/PulleyConstraint.h
9912 views
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
7
#include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
8
#include <Jolt/Physics/Constraints/ConstraintPart/IndependentAxisConstraintPart.h>
9
10
JPH_NAMESPACE_BEGIN
11
12
/// Pulley constraint settings, used to create a pulley constraint.
13
/// A pulley connects two bodies via two fixed world points to each other similar to a distance constraint.
14
/// We define Length1 = |BodyPoint1 - FixedPoint1| where Body1 is a point on body 1 in world space and FixedPoint1 a fixed point in world space
15
/// Length2 = |BodyPoint2 - FixedPoint2|
16
/// The constraint keeps the two line segments constrained so that
17
/// MinDistance <= Length1 + Ratio * Length2 <= MaxDistance
18
class JPH_EXPORT PulleyConstraintSettings final : public TwoBodyConstraintSettings
19
{
20
JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, PulleyConstraintSettings)
21
22
public:
23
// See: ConstraintSettings::SaveBinaryState
24
virtual void SaveBinaryState(StreamOut &inStream) const override;
25
26
/// Create an instance of this constraint
27
virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
28
29
/// This determines in which space the constraint is setup, specified properties below should be in the specified space
30
EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
31
32
/// Body 1 constraint attachment point (space determined by mSpace).
33
RVec3 mBodyPoint1 = RVec3::sZero();
34
35
/// Fixed world point to which body 1 is connected (always world space)
36
RVec3 mFixedPoint1 = RVec3::sZero();
37
38
/// Body 2 constraint attachment point (space determined by mSpace)
39
RVec3 mBodyPoint2 = RVec3::sZero();
40
41
/// Fixed world point to which body 2 is connected (always world space)
42
RVec3 mFixedPoint2 = RVec3::sZero();
43
44
/// Ratio between the two line segments (see formula above), can be used to create a block and tackle
45
float mRatio = 1.0f;
46
47
/// The minimum length of the line segments (see formula above), use -1 to calculate the length based on the positions of the objects when the constraint is created.
48
float mMinLength = 0.0f;
49
50
/// The maximum length of the line segments (see formula above), use -1 to calculate the length based on the positions of the objects when the constraint is created.
51
float mMaxLength = -1.0f;
52
53
protected:
54
// See: ConstraintSettings::RestoreBinaryState
55
virtual void RestoreBinaryState(StreamIn &inStream) override;
56
};
57
58
/// A pulley constraint.
59
class JPH_EXPORT PulleyConstraint final : public TwoBodyConstraint
60
{
61
public:
62
JPH_OVERRIDE_NEW_DELETE
63
64
/// Construct distance constraint
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
86
/// Update the minimum and maximum length for the constraint
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
91
/// Get the current length of both segments (multiplied by the ratio for segment 2)
92
float GetCurrentLength() const { return Vec3(mWorldSpacePosition1 - mFixedPosition1).Length() + mRatio * Vec3(mWorldSpacePosition2 - mFixedPosition2).Length(); }
93
94
///@name Get Lagrange multiplier from last physics update (the linear impulse applied to satisfy the constraint)
95
inline float GetTotalLambdaPosition() const { return mIndependentAxisConstraintPart.GetTotalLambda(); }
96
97
private:
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
114
/// Ratio between the two line segments
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
137
JPH_NAMESPACE_END
138
139