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