Skip to content
0.2.0-dev Download ZIP
96 lines ·4.1 KB Raw
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/RotationEulerConstraintPart.h>
9#include <Jolt/Physics/Constraints/ConstraintPart/PointConstraintPart.h>
10
11JPH_NAMESPACE_BEGIN
12
13/// Fixed constraint settings, used to create a fixed constraint
14class JPH_EXPORT FixedConstraintSettings final : public TwoBodyConstraintSettings
15{
16 JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, FixedConstraintSettings)
17
18public:
19 // See: ConstraintSettings::SaveBinaryState
20 virtual void SaveBinaryState(StreamOut &inStream) const override;
21
22 /// Create an instance of this constraint
23 virtual TwoBodyConstraint * Create(Body &inBody1, Body &inBody2) const override;
24
25 /// This determines in which space the constraint is setup, all properties below should be in the specified space
26 EConstraintSpace mSpace = EConstraintSpace::WorldSpace;
27
28 /// When mSpace is WorldSpace mPoint1 and mPoint2 can be automatically calculated based on the positions of the bodies when the constraint is created (they will be fixated in their current relative position/orientation). Set this to false if you want to supply the attachment points yourself.
29 bool mAutoDetectPoint = false;
30
31 /// Body 1 constraint reference frame (space determined by mSpace)
32 RVec3 mPoint1 = RVec3::sZero();
33 Vec3 mAxisX1 = Vec3::sAxisX();
34 Vec3 mAxisY1 = Vec3::sAxisY();
35
36 /// Body 2 constraint reference frame (space determined by mSpace)
37 RVec3 mPoint2 = RVec3::sZero();
38 Vec3 mAxisX2 = Vec3::sAxisX();
39 Vec3 mAxisY2 = Vec3::sAxisY();
40
41protected:
42 // See: ConstraintSettings::RestoreBinaryState
43 virtual void RestoreBinaryState(StreamIn &inStream) override;
44};
45
46/// A fixed constraint welds two bodies together removing all degrees of freedom between them.
47/// This variant uses Euler angles for the rotation constraint.
48class JPH_EXPORT FixedConstraint final : public TwoBodyConstraint
49{
50public:
51 JPH_OVERRIDE_NEW_DELETE
52
53 /// Constructor
54 FixedConstraint(Body &inBody1, Body &inBody2, const FixedConstraintSettings &inSettings);
55
56 // Generic interface of a constraint
57 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Fixed; }
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::sRotationTranslation(mInvInitialOrientation, mLocalSpacePosition2); }
74
75 ///@name Get Lagrange multiplier from last physics update (the linear/angular impulse applied to satisfy the constraint)
76 inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
77 inline Vec3 GetTotalLambdaRotation() const { return mRotationConstraintPart.GetTotalLambda(); }
78
79private:
80 // CONFIGURATION PROPERTIES FOLLOW
81
82 // Local space constraint positions
83 Vec3 mLocalSpacePosition1;
84 Vec3 mLocalSpacePosition2;
85
86 // Inverse of initial rotation from body 1 to body 2 in body 1 space
87 Quat mInvInitialOrientation;
88
89 // RUN TIME PROPERTIES FOLLOW
90
91 // The constraint parts
92 RotationEulerConstraintPart mRotationConstraintPart;
93 PointConstraintPart mPointConstraintPart;
94};
95
96JPH_NAMESPACE_END