VehicleSixDOFTest.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/Vehicle/VehicleSixDOFTest.h>
  5. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  6. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  7. #include <Jolt/Physics/Collision/GroupFilterTable.h>
  8. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  9. #include <Application/DebugUI.h>
  10. #include <Layers.h>
  11. JPH_IMPLEMENT_RTTI_VIRTUAL(VehicleSixDOFTest)
  12. {
  13. JPH_ADD_BASE_CLASS(VehicleSixDOFTest, VehicleTest)
  14. }
  15. void VehicleSixDOFTest::Initialize()
  16. {
  17. VehicleTest::Initialize();
  18. const float half_vehicle_length = 2.0f;
  19. const float half_vehicle_width = 0.9f;
  20. const float half_vehicle_height = 0.2f;
  21. const float half_wheel_height = 0.3f;
  22. const float half_wheel_width = 0.05f;
  23. const float half_wheel_travel = 0.5f;
  24. Vec3 wheel_position[] =
  25. {
  26. Vec3(-half_vehicle_width, -half_vehicle_height, half_vehicle_length - 2.0f * half_wheel_height),
  27. Vec3(half_vehicle_width, -half_vehicle_height, half_vehicle_length - 2.0f * half_wheel_height),
  28. Vec3(-half_vehicle_width, -half_vehicle_height, -half_vehicle_length + 2.0f * half_wheel_height),
  29. Vec3(half_vehicle_width, -half_vehicle_height, -half_vehicle_length + 2.0f * half_wheel_height),
  30. };
  31. Vec3 position(0, 2, 0);
  32. RefConst<Shape> body_shape = new BoxShape(Vec3(half_vehicle_width, half_vehicle_height, half_vehicle_length));
  33. Ref<CylinderShape> wheel_shape = new CylinderShape(half_wheel_width, half_wheel_height);
  34. wheel_shape->SetDensity(1.0e4f);
  35. // Create group filter
  36. Ref<GroupFilterTable> group_filter = new GroupFilterTable;
  37. // Create vehicle body
  38. mCarBody = mBodyInterface->CreateBody(BodyCreationSettings(body_shape, position, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  39. mCarBody->SetCollisionGroup(CollisionGroup(group_filter, 0, 0));
  40. mBodyInterface->AddBody(mCarBody->GetID(), EActivation::Activate);
  41. // Create wheels
  42. for (int i = 0; i < (int)EWheel::Num; ++i)
  43. {
  44. bool is_front = sIsFrontWheel((EWheel)i);
  45. bool is_left = sIsLeftWheel((EWheel)i);
  46. Vec3 wheel_pos1 = position + wheel_position[i];
  47. Vec3 wheel_pos2 = wheel_pos1 - Vec3(0, half_wheel_travel, 0);
  48. // Create body
  49. Body &wheel = *mBodyInterface->CreateBody(BodyCreationSettings(wheel_shape, wheel_pos2, Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  50. wheel.SetFriction(1.0f);
  51. wheel.SetCollisionGroup(CollisionGroup(group_filter, 0, 0));
  52. mBodyInterface->AddBody(wheel.GetID(), EActivation::Activate);
  53. // Create constraint
  54. SixDOFConstraintSettings settings;
  55. settings.mPosition1 = wheel_pos1;
  56. settings.mPosition2 = wheel_pos2;
  57. settings.mAxisX1 = settings.mAxisX2 = is_left? -Vec3::sAxisX() : Vec3::sAxisX();
  58. settings.mAxisY1 = settings.mAxisY2 = Vec3::sAxisY();
  59. // The suspension works in the Y translation axis only
  60. settings.MakeFixedAxis(EAxis::TranslationX);
  61. settings.SetLimitedAxis(EAxis::TranslationY, -half_wheel_travel, half_wheel_travel);
  62. settings.MakeFixedAxis(EAxis::TranslationZ);
  63. settings.mMotorSettings[EAxis::TranslationY] = MotorSettings(2.0f, 1.0f, 1.0e5f, 0.0f);
  64. // Front wheel can rotate around the Y axis
  65. if (is_front)
  66. settings.SetLimitedAxis(EAxis::RotationY, -cMaxSteeringAngle, cMaxSteeringAngle);
  67. else
  68. settings.MakeFixedAxis(EAxis::RotationY);
  69. // The Z axis is static
  70. settings.MakeFixedAxis(EAxis::RotationZ);
  71. // The main engine drives the X axis
  72. settings.MakeFreeAxis(EAxis::RotationX);
  73. settings.mMotorSettings[EAxis::RotationX] = MotorSettings(2.0f, 1.0f, 0.0f, 0.5e4f);
  74. // The front wheel needs to be able to steer around the Y axis
  75. // However the motors work in the constraint space of the wheel, and since this rotates around the
  76. // X axis we need to drive both the Y and Z to steer
  77. if (is_front)
  78. settings.mMotorSettings[EAxis::RotationY] = settings.mMotorSettings[EAxis::RotationZ] = MotorSettings(10.0f, 1.0f, 0.0f, 1.0e6f);
  79. SixDOFConstraint *wheel_constraint = static_cast<SixDOFConstraint *>(settings.Create(*mCarBody, wheel));
  80. mPhysicsSystem->AddConstraint(wheel_constraint);
  81. mWheels[i] = wheel_constraint;
  82. // Drive the suspension
  83. wheel_constraint->SetTargetPositionCS(Vec3(0, -half_wheel_travel, 0));
  84. wheel_constraint->SetMotorState(EAxis::TranslationY, EMotorState::Position);
  85. // The front wheels steer around the Y axis, but in constraint space of the wheel this means we need to drive
  86. // both Y and Z (see comment above)
  87. if (is_front)
  88. {
  89. wheel_constraint->SetTargetOrientationCS(Quat::sIdentity());
  90. wheel_constraint->SetMotorState(EAxis::RotationY, EMotorState::Position);
  91. wheel_constraint->SetMotorState(EAxis::RotationZ, EMotorState::Position);
  92. }
  93. }
  94. }
  95. void VehicleSixDOFTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  96. {
  97. const float max_rotation_speed = 10.0f * JPH_PI;
  98. // Determine steering and speed
  99. float steering_angle = 0.0f, speed = 0.0f;
  100. if (inParams.mKeyboard->IsKeyPressed(DIK_LEFT)) steering_angle = cMaxSteeringAngle;
  101. if (inParams.mKeyboard->IsKeyPressed(DIK_RIGHT)) steering_angle = -cMaxSteeringAngle;
  102. if (inParams.mKeyboard->IsKeyPressed(DIK_UP)) speed = max_rotation_speed;
  103. if (inParams.mKeyboard->IsKeyPressed(DIK_DOWN)) speed = -max_rotation_speed;
  104. // On user input, assure that the car is active
  105. if (steering_angle != 0.0f || speed != 0.0f)
  106. mBodyInterface->ActivateBody(mCarBody->GetID());
  107. // Brake if current velocity is in the opposite direction of the desired velocity
  108. float car_speed = mCarBody->GetLinearVelocity().Dot(mCarBody->GetRotation().RotateAxisZ());
  109. bool brake = speed != 0.0f && car_speed != 0.0f && Sign(speed) != Sign(car_speed);
  110. // Front wheels
  111. const EWheel front_wheels[] = { EWheel::LeftFront, EWheel::RightFront };
  112. for (EWheel w : front_wheels)
  113. {
  114. SixDOFConstraint *wheel_constraint = mWheels[(int)w];
  115. if (wheel_constraint == nullptr)
  116. continue;
  117. // Steer front wheels
  118. Quat steering_rotation = Quat::sRotation(Vec3::sAxisY(), steering_angle);
  119. wheel_constraint->SetTargetOrientationCS(steering_rotation);
  120. if (brake)
  121. {
  122. // Brake on front wheels
  123. wheel_constraint->SetTargetAngularVelocityCS(Vec3::sZero());
  124. wheel_constraint->SetMotorState(EAxis::RotationX, EMotorState::Velocity);
  125. }
  126. else if (speed != 0.0f)
  127. {
  128. // Front wheel drive, since the motors are applied in the constraint space of the wheel
  129. // it is always applied on the X axis
  130. wheel_constraint->SetTargetAngularVelocityCS(Vec3(sIsLeftWheel(w)? -speed : speed, 0, 0));
  131. wheel_constraint->SetMotorState(EAxis::RotationX, EMotorState::Velocity);
  132. }
  133. else
  134. {
  135. // Free spin
  136. wheel_constraint->SetMotorState(EAxis::RotationX, EMotorState::Off);
  137. }
  138. }
  139. // Rear wheels
  140. const EWheel rear_wheels[] = { EWheel::LeftRear, EWheel::RightRear };
  141. for (EWheel w : rear_wheels)
  142. {
  143. SixDOFConstraint *wheel_constraint = mWheels[(int)w];
  144. if (wheel_constraint == nullptr)
  145. continue;
  146. if (brake)
  147. {
  148. // Brake on rear wheels
  149. wheel_constraint->SetTargetAngularVelocityCS(Vec3::sZero());
  150. wheel_constraint->SetMotorState(EAxis::RotationX, EMotorState::Velocity);
  151. }
  152. else
  153. {
  154. // Free spin
  155. wheel_constraint->SetMotorState(EAxis::RotationX, EMotorState::Off);
  156. }
  157. }
  158. }
  159. void VehicleSixDOFTest::GetInitialCamera(CameraState &ioState) const
  160. {
  161. // Position camera behind car
  162. Vec3 cam_tgt = Vec3(0, 0, 5);
  163. ioState.mPos = Vec3(0, 2.5f, -5);
  164. ioState.mForward = (cam_tgt - ioState.mPos).Normalized();
  165. }
  166. Mat44 VehicleSixDOFTest::GetCameraPivot(float inCameraHeading, float inCameraPitch) const
  167. {
  168. // Pivot is center of car and rotates with car around Y axis only
  169. Vec3 fwd = mCarBody->GetRotation().RotateAxisZ();
  170. fwd.SetY(0.0f);
  171. float len = fwd.Length();
  172. if (len != 0.0f)
  173. fwd /= len;
  174. else
  175. fwd = Vec3::sAxisZ();
  176. Vec3 up = Vec3::sAxisY();
  177. Vec3 right = up.Cross(fwd);
  178. return Mat44(Vec4(right, 0), Vec4(up, 0), Vec4(fwd, 0), Vec4(mCarBody->GetPosition(), 1.0f));
  179. }