VehicleConstraintTest.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/Vehicle/VehicleConstraintTest.h>
  5. #include <Physics/Collision/Shape/BoxShape.h>
  6. #include <Physics/Collision/Shape/OffsetCenterOfMassShape.h>
  7. #include <Physics/Vehicle/WheeledVehicleController.h>
  8. #include <Physics/Body/BodyCreationSettings.h>
  9. #include <Application/DebugUI.h>
  10. #include <Layers.h>
  11. #include <Renderer/DebugRendererImp.h>
  12. JPH_IMPLEMENT_RTTI_VIRTUAL(VehicleConstraintTest)
  13. {
  14. JPH_ADD_BASE_CLASS(VehicleConstraintTest, VehicleTest)
  15. }
  16. int VehicleConstraintTest::sCollisionMode = 1;
  17. VehicleConstraintTest::~VehicleConstraintTest()
  18. {
  19. mPhysicsSystem->RemoveStepListener(mVehicleConstraint);
  20. }
  21. void VehicleConstraintTest::Initialize()
  22. {
  23. VehicleTest::Initialize();
  24. const float wheel_radius = 0.3f;
  25. const float wheel_width = 0.1f;
  26. const float half_vehicle_length = 2.0f;
  27. const float half_vehicle_width = 0.9f;
  28. const float half_vehicle_height = 0.2f;
  29. const float suspension_min_length = 0.3f;
  30. const float suspension_max_length = 0.5f;
  31. const float max_steering_angle = DegreesToRadians(30);
  32. // Create collision testers
  33. mTesters[0] = new VehicleCollisionTesterRay(Layers::MOVING);
  34. mTesters[1] = new VehicleCollisionTesterCastSphere(Layers::MOVING, 0.5f * wheel_width);
  35. // Create vehicle body
  36. Vec3 position(0, 2, 0);
  37. RefConst<Shape> car_shape = OffsetCenterOfMassShapeSettings(Vec3(0, -half_vehicle_height, 0), new BoxShape(Vec3(half_vehicle_width, half_vehicle_height, half_vehicle_length))).Create().Get();
  38. BodyCreationSettings car_body_settings(car_shape, position, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  39. car_body_settings.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  40. car_body_settings.mMassPropertiesOverride.mMass = 1500.0f;
  41. mCarBody = mBodyInterface->CreateBody(car_body_settings);
  42. mBodyInterface->AddBody(mCarBody->GetID(), EActivation::Activate);
  43. // Create vehicle constraint
  44. VehicleConstraintSettings vehicle;
  45. vehicle.mDrawConstraintSize = 0.1f;
  46. vehicle.mMaxPitchRollAngle = DegreesToRadians(60.0f);
  47. // Wheels
  48. WheelSettingsWV *w1 = new WheelSettingsWV;
  49. w1->mPosition = Vec3(-half_vehicle_width, -0.9f * half_vehicle_height, half_vehicle_length - 2.0f * wheel_radius);
  50. w1->mMaxSteerAngle = max_steering_angle;
  51. w1->mMaxHandBrakeTorque = 0.0f; // Front wheel doesn't have hand brake
  52. WheelSettingsWV *w2 = new WheelSettingsWV;
  53. w2->mPosition = Vec3(half_vehicle_width, -0.9f * half_vehicle_height, half_vehicle_length - 2.0f * wheel_radius);
  54. w2->mMaxSteerAngle = max_steering_angle;
  55. w2->mMaxHandBrakeTorque = 0.0f; // Front wheel doesn't have hand brake
  56. WheelSettingsWV *w3 = new WheelSettingsWV;
  57. w3->mPosition = Vec3(-half_vehicle_width, -0.9f * half_vehicle_height, -half_vehicle_length + 2.0f * wheel_radius);
  58. w3->mMaxSteerAngle = 0.0f;
  59. WheelSettingsWV *w4 = new WheelSettingsWV;
  60. w4->mPosition = Vec3(half_vehicle_width, -0.9f * half_vehicle_height, -half_vehicle_length + 2.0f * wheel_radius);
  61. w4->mMaxSteerAngle = 0.0f;
  62. vehicle.mWheels = { w1, w2, w3, w4 };
  63. for (WheelSettings *w : vehicle.mWheels)
  64. {
  65. w->mRadius = wheel_radius;
  66. w->mWidth = wheel_width;
  67. w->mSuspensionMinLength = suspension_min_length;
  68. w->mSuspensionMaxLength = suspension_max_length;
  69. }
  70. WheeledVehicleControllerSettings *controller = new WheeledVehicleControllerSettings;
  71. vehicle.mController = controller;
  72. // Differential
  73. controller->mDifferentials.resize(1);
  74. controller->mDifferentials[0].mLeftWheel = 0;
  75. controller->mDifferentials[0].mRightWheel = 1;
  76. // Anti rollbars
  77. vehicle.mAntiRollBars.resize(2);
  78. vehicle.mAntiRollBars[0].mLeftWheel = 0;
  79. vehicle.mAntiRollBars[0].mRightWheel = 1;
  80. vehicle.mAntiRollBars[1].mLeftWheel = 2;
  81. vehicle.mAntiRollBars[1].mRightWheel = 3;
  82. mVehicleConstraint = new VehicleConstraint(*mCarBody, vehicle);
  83. mPhysicsSystem->AddConstraint(mVehicleConstraint);
  84. mPhysicsSystem->AddStepListener(mVehicleConstraint);
  85. }
  86. void VehicleConstraintTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  87. {
  88. // Determine acceleration and brake
  89. float forward = 0.0f, right = 0.0f, brake = 0.0f, hand_brake = 0.0f;
  90. if (inParams.mKeyboard->IsKeyPressed(DIK_UP))
  91. forward = 1.0f;
  92. else if (inParams.mKeyboard->IsKeyPressed(DIK_DOWN))
  93. forward = -1.0f;
  94. // Check if we're reversing direction
  95. if (mPreviousForward * forward < 0.0f)
  96. {
  97. // Get vehicle velocity in local space to the body of the vehicle
  98. float velocity = (mCarBody->GetRotation().Conjugated() * mCarBody->GetLinearVelocity()).GetZ();
  99. if ((forward > 0.0f && velocity < -0.1f) || (forward < 0.0f && velocity > 0.1f))
  100. {
  101. // Brake while we've not stopped yet
  102. forward = 0.0f;
  103. brake = 1.0f;
  104. }
  105. else
  106. {
  107. // When we've come to a stop, accept the new direction
  108. mPreviousForward = forward;
  109. }
  110. }
  111. // Hand brake will cancel gas pedal
  112. if (inParams.mKeyboard->IsKeyPressed(DIK_Z))
  113. {
  114. forward = 0.0f;
  115. hand_brake = 1.0f;
  116. }
  117. // Steering
  118. if (inParams.mKeyboard->IsKeyPressed(DIK_LEFT))
  119. right = -1.0f;
  120. else if (inParams.mKeyboard->IsKeyPressed(DIK_RIGHT))
  121. right = 1.0f;
  122. // On user input, assure that the car is active
  123. if (right != 0.0f || forward != 0.0f || brake != 0.0f || hand_brake != 0.0f)
  124. mBodyInterface->ActivateBody(mCarBody->GetID());
  125. // Pass the input on to the constraint
  126. static_cast<WheeledVehicleController *>(mVehicleConstraint->GetController())->SetDriverInput(forward, right, brake, hand_brake);
  127. // Set the collision tester
  128. mVehicleConstraint->SetVehicleCollisionTester(mTesters[sCollisionMode]);
  129. // Draw our wheels (this needs to be done in the pre update since we draw the bodies too in the state before the step)
  130. for (uint w = 0; w < 4; ++w)
  131. {
  132. const WheelSettings *settings = mVehicleConstraint->GetWheels()[w]->GetSettings();
  133. Mat44 wheel_transform = mVehicleConstraint->GetWheelWorldTransform(w, Vec3::sAxisY(), Vec3::sAxisX()); // The cyclinder we draw is aligned with Y so we specify that as rotational axis
  134. mDebugRenderer->DrawCylinder(wheel_transform, 0.5f * settings->mWidth, settings->mRadius, Color::sGreen);
  135. }
  136. }
  137. void VehicleConstraintTest::GetInitialCamera(CameraState &ioState) const
  138. {
  139. // Position camera behind car
  140. Vec3 cam_tgt = Vec3(0, 0, 5);
  141. ioState.mPos = Vec3(0, 2.5f, -5);
  142. ioState.mForward = (cam_tgt - ioState.mPos).Normalized();
  143. }
  144. Mat44 VehicleConstraintTest::GetCameraPivot(float inCameraHeading, float inCameraPitch) const
  145. {
  146. // Pivot is center of car and rotates with car around Y axis only
  147. Vec3 fwd = mCarBody->GetRotation().RotateAxisZ();
  148. fwd.SetY(0.0f);
  149. float len = fwd.Length();
  150. if (len != 0.0f)
  151. fwd /= len;
  152. else
  153. fwd = Vec3::sAxisZ();
  154. Vec3 up = Vec3::sAxisY();
  155. Vec3 right = up.Cross(fwd);
  156. return Mat44(Vec4(right, 0), Vec4(up, 0), Vec4(fwd, 0), Vec4(mCarBody->GetPosition(), 1.0f));
  157. }
  158. void VehicleConstraintTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  159. {
  160. VehicleTest::CreateSettingsMenu(inUI, inSubMenu);
  161. inUI->CreateComboBox(inSubMenu, "Collision Mode", { "Ray", "Cast Sphere" }, sCollisionMode, [](int inItem) { sCollisionMode = inItem; });
  162. }