| 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/RackAndPinionConstraint.h> |
| 8 | #include <Jolt/Physics/Constraints/HingeConstraint.h> |
| 9 | #include <Jolt/Physics/Constraints/SliderConstraint.h> |
| 10 | #include <Jolt/Physics/Body/Body.h> |
| 11 | #include <Jolt/ObjectStream/TypeDeclarations.h> |
| 12 | #include <Jolt/Core/StreamIn.h> |
| 13 | #include <Jolt/Core/StreamOut.h> |
| 14 | #ifdef JPH_DEBUG_RENDERER |
| 15 | #include <Jolt/Renderer/DebugRenderer.h> |
| 16 | #endif // JPH_DEBUG_RENDERER |
| 17 | |
| 18 | JPH_NAMESPACE_BEGIN |
| 19 | |
| 20 | JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(RackAndPinionConstraintSettings) |
| 21 | { |
| 22 | JPH_ADD_BASE_CLASS(RackAndPinionConstraintSettings, TwoBodyConstraintSettings) |
| 23 | |
| 24 | JPH_ADD_ENUM_ATTRIBUTE(RackAndPinionConstraintSettings, mSpace) |
| 25 | JPH_ADD_ATTRIBUTE(RackAndPinionConstraintSettings, mHingeAxis) |
| 26 | JPH_ADD_ATTRIBUTE(RackAndPinionConstraintSettings, mSliderAxis) |
| 27 | JPH_ADD_ATTRIBUTE(RackAndPinionConstraintSettings, mRatio) |
| 28 | } |
| 29 | |
| 30 | void RackAndPinionConstraintSettings::SaveBinaryState(StreamOut &inStream) const |
| 31 | { |
| 32 | ConstraintSettings::SaveBinaryState(inStream); |
| 33 | |
| 34 | inStream.Write(mSpace); |
| 35 | inStream.Write(mHingeAxis); |
| 36 | inStream.Write(mSliderAxis); |
| 37 | inStream.Write(mRatio); |
| 38 | } |
| 39 | |
| 40 | void RackAndPinionConstraintSettings::RestoreBinaryState(StreamIn &inStream) |
| 41 | { |
| 42 | ConstraintSettings::RestoreBinaryState(inStream); |
| 43 | |
| 44 | inStream.Read(mSpace); |
| 45 | inStream.Read(mHingeAxis); |
| 46 | inStream.Read(mSliderAxis); |
| 47 | inStream.Read(mRatio); |
| 48 | } |
| 49 | |
| 50 | TwoBodyConstraint *RackAndPinionConstraintSettings::Create(Body &inBody1, Body &inBody2) const |
| 51 | { |
| 52 | return new RackAndPinionConstraint(inBody1, inBody2, *this); |
| 53 | } |
| 54 | |
| 55 | RackAndPinionConstraint::RackAndPinionConstraint(Body &inBody1, Body &inBody2, const RackAndPinionConstraintSettings &inSettings) : |
| 56 | TwoBodyConstraint(inBody1, inBody2, inSettings), |
| 57 | mLocalSpaceHingeAxis(inSettings.mHingeAxis), |
| 58 | mLocalSpaceSliderAxis(inSettings.mSliderAxis), |
| 59 | mRatio(inSettings.mRatio) |
| 60 | { |
| 61 | if (inSettings.mSpace == EConstraintSpace::WorldSpace) |
| 62 | { |
| 63 | // If all properties were specified in world space, take them to local space now |
| 64 | mLocalSpaceHingeAxis = inBody1.GetInverseCenterOfMassTransform().Multiply3x3(mLocalSpaceHingeAxis).Normalized(); |
| 65 | mLocalSpaceSliderAxis = inBody2.GetInverseCenterOfMassTransform().Multiply3x3(mLocalSpaceSliderAxis).Normalized(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void RackAndPinionConstraint::CalculateConstraintProperties(Mat44Arg inRotation1, Mat44Arg inRotation2) |
| 70 | { |
| 71 | // Calculate world space normals |
| 72 | mWorldSpaceHingeAxis = inRotation1 * mLocalSpaceHingeAxis; |
| 73 | mWorldSpaceSliderAxis = inRotation2 * mLocalSpaceSliderAxis; |
| 74 | |
| 75 | mRackAndPinionConstraintPart.CalculateConstraintProperties(*mBody1, mWorldSpaceHingeAxis, *mBody2, mWorldSpaceSliderAxis, mRatio); |
| 76 | } |
| 77 | |
| 78 | void RackAndPinionConstraint::SetupVelocityConstraint(float inDeltaTime) |
| 79 | { |
| 80 | // Calculate constraint properties that are constant while bodies don't move |
| 81 | Mat44 rotation1 = Mat44::sRotation(mBody1->GetRotation()); |
| 82 | Mat44 rotation2 = Mat44::sRotation(mBody2->GetRotation()); |
| 83 | CalculateConstraintProperties(rotation1, rotation2); |
| 84 | } |
| 85 | |
| 86 | void RackAndPinionConstraint::ResetWarmStart() |
| 87 | { |
| 88 | mRackAndPinionConstraintPart.Deactivate(); |
| 89 | } |
| 90 | |
| 91 | void RackAndPinionConstraint::WarmStartVelocityConstraint(float inWarmStartImpulseRatio) |
| 92 | { |
| 93 | // Warm starting: Apply previous frame impulse |
| 94 | mRackAndPinionConstraintPart.WarmStart(*mBody1, *mBody2, inWarmStartImpulseRatio); |
| 95 | } |
| 96 | |
| 97 | bool RackAndPinionConstraint::SolveVelocityConstraint(float inDeltaTime) |
| 98 | { |
| 99 | return mRackAndPinionConstraintPart.SolveVelocityConstraint(*mBody1, mWorldSpaceHingeAxis, *mBody2, mWorldSpaceSliderAxis, mRatio); |
| 100 | } |
| 101 | |
| 102 | bool RackAndPinionConstraint::SolvePositionConstraint(float inDeltaTime, float inBaumgarte) |
| 103 | { |
| 104 | if (mRackConstraint == nullptr || mPinionConstraint == nullptr) |
| 105 | return false; |
| 106 | |
| 107 | float rotation; |
| 108 | if (mPinionConstraint->GetSubType() == EConstraintSubType::Hinge) |
| 109 | { |
| 110 | rotation = StaticCast<HingeConstraint>(mPinionConstraint)->GetCurrentAngle(); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | JPH_ASSERT(false, "Unsupported"); |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | float translation; |
| 119 | if (mRackConstraint->GetSubType() == EConstraintSubType::Slider) |
| 120 | { |
| 121 | translation = StaticCast<SliderConstraint>(mRackConstraint)->GetCurrentPosition(); |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | JPH_ASSERT(false, "Unsupported"); |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | float error = CenterAngleAroundZero(fmod(rotation - mRatio * translation, 2.0f * JPH_PI)); |
| 130 | if (error == 0.0f) |
| 131 | return false; |
| 132 | |
| 133 | Mat44 rotation1 = Mat44::sRotation(mBody1->GetRotation()); |
| 134 | Mat44 rotation2 = Mat44::sRotation(mBody2->GetRotation()); |
| 135 | CalculateConstraintProperties(rotation1, rotation2); |
| 136 | return mRackAndPinionConstraintPart.SolvePositionConstraint(*mBody1, *mBody2, error, inBaumgarte); |
| 137 | } |
| 138 | |
| 139 | #ifdef JPH_DEBUG_RENDERER |
| 140 | void RackAndPinionConstraint::DrawConstraint(DebugRenderer *inRenderer) const |
| 141 | { |
| 142 | RMat44 transform1 = mBody1->GetCenterOfMassTransform(); |
| 143 | RMat44 transform2 = mBody2->GetCenterOfMassTransform(); |
| 144 | |
| 145 | // Draw constraint axis |
| 146 | inRenderer->DrawArrow(transform1.GetTranslation(), transform1 * mLocalSpaceHingeAxis, Color::sGreen, 0.01f); |
| 147 | inRenderer->DrawArrow(transform2.GetTranslation(), transform2 * mLocalSpaceSliderAxis, Color::sBlue, 0.01f); |
| 148 | } |
| 149 | |
| 150 | #endif // JPH_DEBUG_RENDERER |
| 151 | |
| 152 | void RackAndPinionConstraint::SaveState(StateRecorder &inStream) const |
| 153 | { |
| 154 | TwoBodyConstraint::SaveState(inStream); |
| 155 | |
| 156 | mRackAndPinionConstraintPart.SaveState(inStream); |
| 157 | } |
| 158 | |
| 159 | void RackAndPinionConstraint::RestoreState(StateRecorder &inStream) |
| 160 | { |
| 161 | TwoBodyConstraint::RestoreState(inStream); |
| 162 | |
| 163 | mRackAndPinionConstraintPart.RestoreState(inStream); |
| 164 | } |
| 165 | |
| 166 | Ref<ConstraintSettings> RackAndPinionConstraint::GetConstraintSettings() const |
| 167 | { |
| 168 | RackAndPinionConstraintSettings *settings = new RackAndPinionConstraintSettings; |
| 169 | ToConstraintSettings(*settings); |
| 170 | settings->mSpace = EConstraintSpace::LocalToBodyCOM; |
| 171 | settings->mHingeAxis = mLocalSpaceHingeAxis; |
| 172 | settings->mSliderAxis = mLocalSpaceSliderAxis; |
| 173 | settings->mRatio = mRatio; |
| 174 | return settings; |
| 175 | } |
| 176 | |
| 177 | Mat44 RackAndPinionConstraint::GetConstraintToBody1Matrix() const |
| 178 | { |
| 179 | Vec3 perp = mLocalSpaceHingeAxis.GetNormalizedPerpendicular(); |
| 180 | return Mat44(Vec4(mLocalSpaceHingeAxis, 0), Vec4(perp, 0), Vec4(mLocalSpaceHingeAxis.Cross(perp), 0), Vec4(0, 0, 0, 1)); |
| 181 | } |
| 182 | |
| 183 | Mat44 RackAndPinionConstraint::GetConstraintToBody2Matrix() const |
| 184 | { |
| 185 | Vec3 perp = mLocalSpaceSliderAxis.GetNormalizedPerpendicular(); |
| 186 | return Mat44(Vec4(mLocalSpaceSliderAxis, 0), Vec4(perp, 0), Vec4(mLocalSpaceSliderAxis.Cross(perp), 0), Vec4(0, 0, 0, 1)); |
| 187 | } |
| 188 | |
| 189 | JPH_NAMESPACE_END |