Debian Codegraph @

thirdparty/jolt_physics/Jolt/Physics/Constraints/ConeConstraint.h

raw symbols SCIP
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 #include <Jolt/Physics/Constraints/ConstraintPart/AngleConstraintPart.h>
10
11 JPH_NAMESPACE_BEGIN
12
13 /// Cone constraint settings, used to create a cone constraint
14 class JPH_EXPORT ConeConstraintSettings final : public TwoBodyConstraintSettings
15 {
16 JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, ConeConstraintSettings)
17
18 public:
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 /// Body 1 constraint reference frame (space determined by mSpace)
29 RVec3 mPoint1 = RVec3::sZero();
30 Vec3 mTwistAxis1 = Vec3::sAxisX();
31
32 /// Body 2 constraint reference frame (space determined by mSpace)
33 RVec3 mPoint2 = RVec3::sZero();
34 Vec3 mTwistAxis2 = Vec3::sAxisX();
35
36 /// Half of maximum angle between twist axis of body 1 and 2
37 float mHalfConeAngle = 0.0f;
38
39 protected:
40 // See: ConstraintSettings::RestoreBinaryState
41 virtual void RestoreBinaryState(StreamIn &inStream) override;
42 };
43
44 /// A cone constraint constraints 2 bodies to a single point and limits the swing between the twist axis within a cone:
45 ///
46 /// t1 . t2 <= cos(theta)
47 ///
48 /// Where:
49 ///
50 /// t1 = twist axis of body 1.
51 /// t2 = twist axis of body 2.
52 /// theta = half cone angle (angle from the principal axis of the cone to the edge).
53 ///
54 /// Calculating the Jacobian:
55 ///
56 /// Constraint equation:
57 ///
58 /// C = t1 . t2 - cos(theta)
59 ///
60 /// Derivative:
61 ///
62 /// d/dt C = d/dt (t1 . t2) = (d/dt t1) . t2 + t1 . (d/dt t2) = (w1 x t1) . t2 + t1 . (w2 x t2) = (t1 x t2) . w1 + (t2 x t1) . w2
63 ///
64 /// d/dt C = J v = [0, -t2 x t1, 0, t2 x t1] [v1, w1, v2, w2]
65 ///
66 /// Where J is the Jacobian.
67 ///
68 /// Note that this is the exact same equation as used in AngleConstraintPart if we use t2 x t1 as the world space axis
69 class JPH_EXPORT ConeConstraint final : public TwoBodyConstraint
70 {
71 public:
72 JPH_OVERRIDE_NEW_DELETE
73
74 /// Construct cone constraint
75 ConeConstraint(Body &inBody1, Body &inBody2, const ConeConstraintSettings &inSettings);
76
77 // Generic interface of a constraint
78 virtual EConstraintSubType GetSubType() const override { return EConstraintSubType::Cone; }
79 virtual void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM) override;
80 virtual void SetupVelocityConstraint(float inDeltaTime) override;
81 virtual void ResetWarmStart() override;
82 virtual void WarmStartVelocityConstraint(float inWarmStartImpulseRatio) override;
83 virtual bool SolveVelocityConstraint(float inDeltaTime) override;
84 virtual bool SolvePositionConstraint(float inDeltaTime, float inBaumgarte) override;
85 #ifdef JPH_DEBUG_RENDERER
86 virtual void DrawConstraint(DebugRenderer *inRenderer) const override;
87 virtual void DrawConstraintLimits(DebugRenderer *inRenderer) const override;
88 #endif // JPH_DEBUG_RENDERER
89 virtual void SaveState(StateRecorder &inStream) const override;
90 virtual void RestoreState(StateRecorder &inStream) override;
91 virtual Ref<ConstraintSettings> GetConstraintSettings() const override;
92
93 // See: TwoBodyConstraint
94 virtual Mat44 GetConstraintToBody1Matrix() const override;
95 virtual Mat44 GetConstraintToBody2Matrix() const override;
96
97 /// Update maximum angle between body 1 and 2 (see ConeConstraintSettings)
98 void SetHalfConeAngle(float inHalfConeAngle) { JPH_ASSERT(inHalfConeAngle >= 0.0f && inHalfConeAngle <= JPH_PI); mCosHalfConeAngle = Cos(inHalfConeAngle); }
99 float GetCosHalfConeAngle() const { return mCosHalfConeAngle; }
100
101 ///@name Get Lagrange multiplier from last physics update (the linear/angular impulse applied to satisfy the constraint)
102 inline Vec3 GetTotalLambdaPosition() const { return mPointConstraintPart.GetTotalLambda(); }
103 inline float GetTotalLambdaRotation() const { return mAngleConstraintPart.GetTotalLambda(); }
104
105 private:
106 // Internal helper function to calculate the values below
107 void CalculateRotationConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2);
108
109 // CONFIGURATION PROPERTIES FOLLOW
110
111 // Local space constraint positions
112 Vec3 mLocalSpacePosition1;
113 Vec3 mLocalSpacePosition2;
114
115 // Local space constraint axis
116 Vec3 mLocalSpaceTwistAxis1;
117 Vec3 mLocalSpaceTwistAxis2;
118
119 // Angular limits
120 float mCosHalfConeAngle;
121
122 // RUN TIME PROPERTIES FOLLOW
123
124 // Axis and angle of rotation between the two bodies
125 Vec3 mWorldSpaceRotationAxis;
126 float mCosTheta;
127
128 // The constraint parts
129 PointConstraintPart mPointConstraintPart;
130 AngleConstraintPart mAngleConstraintPart;
131 };
132
133 JPH_NAMESPACE_END