VehicleSixDOFTest.cpp 7.8 KB

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