Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Constraints/PointConstraint.h
9913 views
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
7
#include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
8
#include <Jolt/Physics/Constraints/ConstraintPart/PointConstraintPart.h>
9
10
JPH_NAMESPACE_BEGIN
11
12
/// Point constraint settings, used to create a point constraint
13
class JPH_EXPORT PointConstraintSettings final : public TwoBodyConstraintSettings
14
{
15
JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, PointConstraintSettings)
16
17
public:
18
// See: ConstraintSettings::SaveBinaryState
19
virtual void SaveBinaryState(StreamOut &inStream) const override;
20
21
/// Create an instance of this constraint
22
virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
23
24
/// This determines in which space the constraint is setup, all properties below should be in the specified space
25
EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
26
27
/// Body 1 constraint position (space determined by mSpace).
28
RVec3 mPoint1 = RVec3::sZero();
29
30
/// Body 2 constraint position (space determined by mSpace).
31
/// Note: Normally you would set mPoint1 = mPoint2 if the bodies are already placed how you want to constrain them (if mSpace = world space).
32
RVec3 mPoint2 = RVec3::sZero();
33
34
protected:
35
// See: ConstraintSettings::RestoreBinaryState
36
virtual void RestoreBinaryState(StreamIn &inStream) override;
37
};
38
39
/// A point constraint constrains 2 bodies on a single point (removing 3 degrees of freedom)
40
class JPH_EXPORT PointConstraint final : public TwoBodyConstraint
41
{
42
public:
43
JPH_OVERRIDE_NEW_DELETE
44
45
/// Construct point constraint
46
PointConstraint(Body &inBody1, Body &inBody2, const PointConstraintSettings &inSettings);
47
48
// Generic interface of a constraint
49
virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Point; }
50
virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
51
virtual void SetupVelocityConstraint(float inDeltaTime) override;
52
virtual void ResetWarmStart() override;
53
virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
54
virtual bool SolveVelocityConstraint(float inDeltaTime) override;
55
virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
56
#ifdef JPH_DEBUG_RENDERER
57
virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
58
#endif // JPH_DEBUG_RENDERER
59
virtual void SaveState(StateRecorder &inStream) const override;
60
virtual void RestoreState(StateRecorder &inStream) override;
61
virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
62
63
/// Update the attachment point for body 1
64
void SetPoint1(EConstraintSpace inSpace, RVec3Arg inPoint1);
65
66
/// Update the attachment point for body 2
67
void SetPoint2(EConstraintSpace inSpace, RVec3Arg inPoint2);
68
69
/// Get the attachment point for body 1 relative to body 1 COM (transform by Body::GetCenterOfMassTransform to take to world space)
70
inline Vec3 GetLocalSpacePoint1() const { return mLocalSpacePosition1; }
71
72
/// Get the attachment point for body 2 relative to body 2 COM (transform by Body::GetCenterOfMassTransform to take to world space)
73
inline Vec3 GetLocalSpacePoint2() const { return mLocalSpacePosition2; }
74
75
// See: TwoBodyConstraint
76
virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition1); }
77
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.
78
79
///@name Get Lagrange multiplier from last physics update (the linear impulse applied to satisfy the constraint)
80
inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
81
82
private:
83
// Internal helper function to calculate the values below
84
void CalculateConstraintProperties();
85
86
// Local space constraint positions
87
Vec3 mLocalSpacePosition1;
88
Vec3 mLocalSpacePosition2;
89
90
// The constraint part
91
PointConstraintPart mPointConstraintPart;
92
};
93
94
JPH_NAMESPACE_END
95
96