Skip to content
0.2.0-dev Download ZIP
215 lines ·7.5 KB Raw
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#include <Jolt/Jolt.h>
6
7#include <Jolt/Physics/Constraints/FixedConstraint.h>
8#include <Jolt/Physics/Body/Body.h>
9#include <Jolt/ObjectStream/TypeDeclarations.h>
10#ifdef JPH_DEBUG_RENDERER
11 #include <Jolt/Renderer/DebugRenderer.h>
12#endif // JPH_DEBUG_RENDERER
13
14JPH_NAMESPACE_BEGIN
15
16using namespace literals;
17
18JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(FixedConstraintSettings)
19{
20 JPH_ADD_BASE_CLASS(FixedConstraintSettings, TwoBodyConstraintSettings)
21
22 JPH_ADD_ENUM_ATTRIBUTE(FixedConstraintSettings, mSpace)
23 JPH_ADD_ATTRIBUTE(FixedConstraintSettings, mAutoDetectPoint)
24 JPH_ADD_ATTRIBUTE(FixedConstraintSettings, mPoint1)
25 JPH_ADD_ATTRIBUTE(FixedConstraintSettings, mAxisX1)
26 JPH_ADD_ATTRIBUTE(FixedConstraintSettings, mAxisY1)
27 JPH_ADD_ATTRIBUTE(FixedConstraintSettings, mPoint2)
28 JPH_ADD_ATTRIBUTE(FixedConstraintSettings, mAxisX2)
29 JPH_ADD_ATTRIBUTE(FixedConstraintSettings, mAxisY2)
30}
31
32void FixedConstraintSettings::SaveBinaryState(StreamOut &inStream) const
33{
34 ConstraintSettings::SaveBinaryState(inStream);
35
36 inStream.Write(mSpace);
37 inStream.Write(mAutoDetectPoint);
38 inStream.Write(mPoint1);
39 inStream.Write(mAxisX1);
40 inStream.Write(mAxisY1);
41 inStream.Write(mPoint2);
42 inStream.Write(mAxisX2);
43 inStream.Write(mAxisY2);
44}
45
46void FixedConstraintSettings::RestoreBinaryState(StreamIn &inStream)
47{
48 ConstraintSettings::RestoreBinaryState(inStream);
49
50 inStream.Read(mSpace);
51 inStream.Read(mAutoDetectPoint);
52 inStream.Read(mPoint1);
53 inStream.Read(mAxisX1);
54 inStream.Read(mAxisY1);
55 inStream.Read(mPoint2);
56 inStream.Read(mAxisX2);
57 inStream.Read(mAxisY2);
58}
59
60TwoBodyConstraint *FixedConstraintSettings::Create(Body &inBody1, Body &inBody2) const
61{
62 return new FixedConstraint(inBody1, inBody2, *this);
63}
64
65FixedConstraint::FixedConstraint(Body &inBody1, Body &inBody2, const FixedConstraintSettings &inSettings) :
66 TwoBodyConstraint(inBody1, inBody2, inSettings)
67{
68 // Store inverse of initial rotation from body 1 to body 2 in body 1 space
69 mInvInitialOrientation = RotationEulerConstraintPart::sGetInvInitialOrientationXY(inSettings.mAxisX1, inSettings.mAxisY1, inSettings.mAxisX2, inSettings.mAxisY2);
70
71 if (inSettings.mSpace == EConstraintSpace::WorldSpace)
72 {
73 if (inSettings.mAutoDetectPoint)
74 {
75 // Determine anchor point: If any of the bodies can never be dynamic use the other body as anchor point
76 RVec3 anchor;
77 if (!inBody1.CanBeKinematicOrDynamic())
78 anchor = inBody2.GetCenterOfMassPosition();
79 else if (!inBody2.CanBeKinematicOrDynamic())
80 anchor = inBody1.GetCenterOfMassPosition();
81 else
82 {
83 // Otherwise use weighted anchor point towards the lightest body
84 Real inv_m1 = Real(inBody1.GetMotionPropertiesUnchecked()->GetInverseMassUnchecked());
85 Real inv_m2 = Real(inBody2.GetMotionPropertiesUnchecked()->GetInverseMassUnchecked());
86 Real total_inv_mass = inv_m1 + inv_m2;
87 if (total_inv_mass != 0.0_r)
88 anchor = (inv_m1 * inBody1.GetCenterOfMassPosition() + inv_m2 * inBody2.GetCenterOfMassPosition()) / (inv_m1 + inv_m2);
89 else
90 anchor = inBody1.GetCenterOfMassPosition();
91 }
92
93 // Store local positions
94 mLocalSpacePosition1 = Vec3(inBody1.GetInverseCenterOfMassTransform() * anchor);
95 mLocalSpacePosition2 = Vec3(inBody2.GetInverseCenterOfMassTransform() * anchor);
96 }
97 else
98 {
99 // Store local positions
100 mLocalSpacePosition1 = Vec3(inBody1.GetInverseCenterOfMassTransform() * inSettings.mPoint1);
101 mLocalSpacePosition2 = Vec3(inBody2.GetInverseCenterOfMassTransform() * inSettings.mPoint2);
102 }
103
104 // Constraints were specified in world space, so we should have replaced c1 with q10^-1 c1 and c2 with q20^-1 c2
105 // => r0^-1 = (q20^-1 c2) (q10^-1 c1)^1 = q20^-1 (c2 c1^-1) q10
106 mInvInitialOrientation = inBody2.GetRotation().Conjugated() * mInvInitialOrientation * inBody1.GetRotation();
107 }
108 else
109 {
110 // Store local positions
111 mLocalSpacePosition1 = Vec3(inSettings.mPoint1);
112 mLocalSpacePosition2 = Vec3(inSettings.mPoint2);
113 }
114}
115
116void FixedConstraint::NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inDeltaCOM)
117{
118 if (mBody1->GetID() == inBodyID)
119 mLocalSpacePosition1 -= inDeltaCOM;
120 else if (mBody2->GetID() == inBodyID)
121 mLocalSpacePosition2 -= inDeltaCOM;
122}
123
124void FixedConstraint::SetupVelocityConstraint(float inDeltaTime)
125{
126 // Calculate constraint values that don't change when the bodies don't change position
127 Mat44 rotation1 = Mat44::sRotation(mBody1->GetRotation());
128 Mat44 rotation2 = Mat44::sRotation(mBody2->GetRotation());
129 mRotationConstraintPart.CalculateConstraintProperties(*mBody1, rotation1, *mBody2, rotation2);
130 mPointConstraintPart.CalculateConstraintProperties(*mBody1, rotation1, mLocalSpacePosition1, *mBody2, rotation2, mLocalSpacePosition2);
131}
132
133void FixedConstraint::ResetWarmStart()
134{
135 mRotationConstraintPart.Deactivate();
136 mPointConstraintPart.Deactivate();
137}
138
139void FixedConstraint::WarmStartVelocityConstraint(float inWarmStartImpulseRatio)
140{
141 // Warm starting: Apply previous frame impulse
142 mRotationConstraintPart.WarmStart(*mBody1, *mBody2, inWarmStartImpulseRatio);
143 mPointConstraintPart.WarmStart(*mBody1, *mBody2, inWarmStartImpulseRatio);
144}
145
146bool FixedConstraint::SolveVelocityConstraint(float inDeltaTime)
147{
148 // Solve rotation constraint
149 bool rot = mRotationConstraintPart.SolveVelocityConstraint(*mBody1, *mBody2);
150
151 // Solve position constraint
152 bool pos = mPointConstraintPart.SolveVelocityConstraint(*mBody1, *mBody2);
153
154 return rot || pos;
155}
156
157bool FixedConstraint::SolvePositionConstraint(float inDeltaTime, float inBaumgarte)
158{
159 // Solve rotation constraint
160 mRotationConstraintPart.CalculateConstraintProperties(*mBody1, Mat44::sRotation(mBody1->GetRotation()), *mBody2, Mat44::sRotation(mBody2->GetRotation()));
161 bool rot = mRotationConstraintPart.SolvePositionConstraint(*mBody1, *mBody2, mInvInitialOrientation, inBaumgarte);
162
163 // Solve position constraint
164 mPointConstraintPart.CalculateConstraintProperties(*mBody1, Mat44::sRotation(mBody1->GetRotation()), mLocalSpacePosition1, *mBody2, Mat44::sRotation(mBody2->GetRotation()), mLocalSpacePosition2);
165 bool pos = mPointConstraintPart.SolvePositionConstraint(*mBody1, *mBody2, inBaumgarte);
166
167 return rot || pos;
168}
169
170#ifdef JPH_DEBUG_RENDERER
171void FixedConstraint::DrawConstraint(DebugRenderer *inRenderer) const
172{
173 RMat44 com1 = mBody1->GetCenterOfMassTransform();
174 RMat44 com2 = mBody2->GetCenterOfMassTransform();
175
176 RVec3 anchor1 = com1 * mLocalSpacePosition1;
177 RVec3 anchor2 = com2 * mLocalSpacePosition2;
178
179 // Draw constraint
180 inRenderer->DrawLine(com1.GetTranslation(), anchor1, Color::sGreen);
181 inRenderer->DrawLine(com2.GetTranslation(), anchor2, Color::sBlue);
182}
183#endif // JPH_DEBUG_RENDERER
184
185void FixedConstraint::SaveState(StateRecorder &inStream) const
186{
187 TwoBodyConstraint::SaveState(inStream);
188
189 mRotationConstraintPart.SaveState(inStream);
190 mPointConstraintPart.SaveState(inStream);
191}
192
193void FixedConstraint::RestoreState(StateRecorder &inStream)
194{
195 TwoBodyConstraint::RestoreState(inStream);
196
197 mRotationConstraintPart.RestoreState(inStream);
198 mPointConstraintPart.RestoreState(inStream);
199}
200
201Ref<ConstraintSettings> FixedConstraint::GetConstraintSettings() const
202{
203 FixedConstraintSettings *settings = new FixedConstraintSettings;
204 ToConstraintSettings(*settings);
205 settings->mSpace = EConstraintSpace::LocalToBodyCOM;
206 settings->mPoint1 = RVec3(mLocalSpacePosition1);
207 settings->mAxisX1 = Vec3::sAxisX();
208 settings->mAxisY1 = Vec3::sAxisY();
209 settings->mPoint2 = RVec3(mLocalSpacePosition2);
210 settings->mAxisX2 = mInvInitialOrientation.RotateAxisX();
211 settings->mAxisY2 = mInvInitialOrientation.RotateAxisY();
212 return settings;
213}
214
215JPH_NAMESPACE_END