Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
DistanceConstraint.h
Go to the documentation of this file.
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
9
11
14{
15public:
17
18 // See: ConstraintSettings::SaveBinaryState
19 virtual void SaveBinaryState(StreamOut &inStream) const override;
20
22 virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
23
25 EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
26
30 RVec3 mPoint1 = RVec3::sZero();
31
33 RVec3 mPoint2 = RVec3::sZero();
34
36 float mMinDistance = -1.0f;
37 float mMaxDistance = -1.0f;
38
41
42protected:
43 // See: ConstraintSettings::RestoreBinaryState
44 virtual void RestoreBinaryState(StreamIn &inStream) override;
45};
46
49{
50public:
52
54 DistanceConstraint(Body &inBody1, Body &inBody2, const DistanceConstraintSettings &inSettings);
55
56 // Generic interface of a constraint
57 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Distance; }
58 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
59 virtual void SetupVelocityConstraint(float inDeltaTime) override;
60 virtual void ResetWarmStart() override;
61 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
62 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
63 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
64#ifdef JPH_DEBUG_RENDERER
65 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
66#endif // JPH_DEBUG_RENDERER
67 virtual void SaveState(StateRecorder &inStream) const override;
68 virtual void RestoreState(StateRecorder &inStream) override;
69 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
70
71 // See: TwoBodyConstraint
72 virtual Mat44 GetConstraintToBody1Matrix() const override { return Mat44::sTranslation(mLocalSpacePosition1); }
73 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.
74
76 void SetDistance(float inMinDistance, float inMaxDistance) { JPH_ASSERT(inMinDistance <= inMaxDistance); mMinDistance = inMinDistance; mMaxDistance = inMaxDistance; }
77 float GetMinDistance() const { return mMinDistance; }
78 float GetMaxDistance() const { return mMaxDistance; }
79
81 const SpringSettings & GetLimitsSpringSettings() const { return mLimitsSpringSettings; }
82 SpringSettings & GetLimitsSpringSettings() { return mLimitsSpringSettings; }
83 void SetLimitsSpringSettings(const SpringSettings &inLimitsSpringSettings) { mLimitsSpringSettings = inLimitsSpringSettings; }
84
86 inline float GetTotalLambdaPosition() const { return mAxisConstraint.GetTotalLambda(); }
87
88private:
89 // Internal helper function to calculate the values below
90 void CalculateConstraintProperties(float inDeltaTime);
91
92 // CONFIGURATION PROPERTIES FOLLOW
93
94 // Local space constraint positions
95 Vec3 mLocalSpacePosition1;
96 Vec3 mLocalSpacePosition2;
97
98 // Min/max distance that must be kept between the world space points
99 float mMinDistance;
100 float mMaxDistance;
101
102 // Soft constraint limits
103 SpringSettings mLimitsSpringSettings;
104
105 // RUN TIME PROPERTIES FOLLOW
106
107 // World space positions and normal
108 RVec3 mWorldSpacePosition1;
109 RVec3 mWorldSpacePosition2;
110 Vec3 mWorldSpaceNormal;
111
112 // Depending on if the distance < min or distance > max we can apply forces to prevent further violations
113 float mMinLambda;
114 float mMaxLambda;
115
116 // The constraint part
117 AxisConstraintPart mAxisConstraint;
118};
119
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: AxisConstraintPart.h:43
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
This constraint is a stiff spring that holds 2 points at a fixed distance from each other.
Definition: DistanceConstraint.h:49
void SetLimitsSpringSettings(const SpringSettings &inLimitsSpringSettings)
Definition: DistanceConstraint.h:83
const SpringSettings & GetLimitsSpringSettings() const
Update the limits spring settings.
Definition: DistanceConstraint.h:81
virtual Mat44 GetConstraintToBody2Matrix() const override
Calculates the transform that transforms from constraint space to body 2 space. The first column of t...
Definition: DistanceConstraint.h:73
float GetMinDistance() const
Definition: DistanceConstraint.h:77
float GetMaxDistance() const
Definition: DistanceConstraint.h:78
float GetTotalLambdaPosition() const
Definition: DistanceConstraint.h:86
virtual EConstraintSubType GetSubType() const override
Get the sub type of a constraint.
Definition: DistanceConstraint.h:57
void SetDistance(float inMinDistance, float inMaxDistance)
Update the minimum and maximum distance for the constraint.
Definition: DistanceConstraint.h:76
SpringSettings & GetLimitsSpringSettings()
Definition: DistanceConstraint.h:82
virtual Mat44 GetConstraintToBody1Matrix() const override
Calculates the transform that transforms from constraint space to body 1 space. The first column of t...
Definition: DistanceConstraint.h:72
Distance constraint settings, used to create a distance constraint.
Definition: DistanceConstraint.h:14
SpringSettings mLimitsSpringSettings
When enabled, this makes the limits soft. When the constraint exceeds the limits, a spring force will...
Definition: DistanceConstraint.h:40
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
Definition: Reference.h:101
Settings for a linear or angular spring.
Definition: SpringSettings.h:23
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
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107