TankTest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/TankTest.h>
  6. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  7. #include <Jolt/Physics/Collision/RayCast.h>
  8. #include <Jolt/Physics/Collision/CastResult.h>
  9. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  10. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  11. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  12. #include <Jolt/Physics/Collision/Shape/OffsetCenterOfMassShape.h>
  13. #include <Jolt/Physics/Vehicle/TrackedVehicleController.h>
  14. #include <Jolt/Physics/Collision/GroupFilterTable.h>
  15. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  16. #include <Application/DebugUI.h>
  17. #include <Layers.h>
  18. #include <Renderer/DebugRendererImp.h>
  19. JPH_IMPLEMENT_RTTI_VIRTUAL(TankTest)
  20. {
  21. JPH_ADD_BASE_CLASS(TankTest, VehicleTest)
  22. }
  23. TankTest::~TankTest()
  24. {
  25. mPhysicsSystem->RemoveStepListener(mVehicleConstraint);
  26. }
  27. void TankTest::Initialize()
  28. {
  29. VehicleTest::Initialize();
  30. const float wheel_radius = 0.3f;
  31. const float wheel_width = 0.1f;
  32. const float half_vehicle_length = 3.2f;
  33. const float half_vehicle_width = 1.7f;
  34. const float half_vehicle_height = 0.5f;
  35. const float suspension_min_length = 0.3f;
  36. const float suspension_max_length = 0.5f;
  37. const float suspension_frequency = 1.0f;
  38. const float half_turret_width = 1.4f;
  39. const float half_turret_length = 2.0f;
  40. const float half_turret_height = 0.4f;
  41. const float half_barrel_length = 1.5f;
  42. const float barrel_radius = 0.1f;
  43. const float barrel_rotation_offset = 0.2f;
  44. static Vec3 wheel_pos[] = {
  45. Vec3(0.0f, -0.0f, 2.95f),
  46. Vec3(0.0f, -0.3f, 2.1f),
  47. Vec3(0.0f, -0.3f, 1.4f),
  48. Vec3(0.0f, -0.3f, 0.7f),
  49. Vec3(0.0f, -0.3f, 0.0f),
  50. Vec3(0.0f, -0.3f, -0.7f),
  51. Vec3(0.0f, -0.3f, -1.4f),
  52. Vec3(0.0f, -0.3f, -2.1f),
  53. Vec3(0.0f, -0.0f, -2.75f),
  54. };
  55. // Create filter to prevent body, turret and barrel from colliding
  56. GroupFilter *filter = new GroupFilterTable;
  57. // Create tank body
  58. RVec3 body_position(0, 2, 0);
  59. RefConst<Shape> tank_body_shape = OffsetCenterOfMassShapeSettings(Vec3(0, -half_vehicle_height, 0), new BoxShape(Vec3(half_vehicle_width, half_vehicle_height, half_vehicle_length))).Create().Get();
  60. BodyCreationSettings tank_body_settings(tank_body_shape, body_position, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  61. tank_body_settings.mCollisionGroup.SetGroupFilter(filter);
  62. tank_body_settings.mCollisionGroup.SetGroupID(0);
  63. tank_body_settings.mCollisionGroup.SetSubGroupID(0);
  64. tank_body_settings.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  65. tank_body_settings.mMassPropertiesOverride.mMass = 4000.0f;
  66. mTankBody = mBodyInterface->CreateBody(tank_body_settings);
  67. mBodyInterface->AddBody(mTankBody->GetID(), EActivation::Activate);
  68. // Create vehicle constraint
  69. VehicleConstraintSettings vehicle;
  70. vehicle.mDrawConstraintSize = 0.1f;
  71. vehicle.mMaxPitchRollAngle = DegreesToRadians(60.0f);
  72. TrackedVehicleControllerSettings *controller = new TrackedVehicleControllerSettings;
  73. vehicle.mController = controller;
  74. for (int t = 0; t < 2; ++t)
  75. {
  76. VehicleTrackSettings &track = controller->mTracks[t];
  77. // Last wheel is driven wheel
  78. track.mDrivenWheel = (uint)(vehicle.mWheels.size() + size(wheel_pos) - 1);
  79. for (uint wheel = 0; wheel < size(wheel_pos); ++wheel)
  80. {
  81. WheelSettingsTV *w = new WheelSettingsTV;
  82. w->mPosition = wheel_pos[wheel];
  83. w->mPosition.SetX(t == 0? half_vehicle_width : -half_vehicle_width);
  84. w->mRadius = wheel_radius;
  85. w->mWidth = wheel_width;
  86. w->mSuspensionMinLength = suspension_min_length;
  87. w->mSuspensionMaxLength = wheel == 0 || wheel == size(wheel_pos) - 1? suspension_min_length : suspension_max_length;
  88. w->mSuspensionSpring.mFrequency = suspension_frequency;
  89. // Add the wheel to the vehicle
  90. track.mWheels.push_back((uint)vehicle.mWheels.size());
  91. vehicle.mWheels.push_back(w);
  92. }
  93. }
  94. mVehicleConstraint = new VehicleConstraint(*mTankBody, vehicle);
  95. mVehicleConstraint->SetVehicleCollisionTester(new VehicleCollisionTesterRay(Layers::MOVING));
  96. #ifdef JPH_DEBUG_RENDERER
  97. static_cast<TrackedVehicleController *>(mVehicleConstraint->GetController())->SetRPMMeter(Vec3(0, 2, 0), 0.5f);
  98. #endif // JPH_DEBUG_RENDERER
  99. mPhysicsSystem->AddConstraint(mVehicleConstraint);
  100. mPhysicsSystem->AddStepListener(mVehicleConstraint);
  101. // Create turret
  102. RVec3 turret_position = body_position + Vec3(0, half_vehicle_height + half_turret_height, 0);
  103. BodyCreationSettings turret_body_setings(new BoxShape(Vec3(half_turret_width, half_turret_height, half_turret_length)), turret_position, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  104. turret_body_setings.mCollisionGroup.SetGroupFilter(filter);
  105. turret_body_setings.mCollisionGroup.SetGroupID(0);
  106. turret_body_setings.mCollisionGroup.SetSubGroupID(0);
  107. turret_body_setings.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  108. turret_body_setings.mMassPropertiesOverride.mMass = 2000.0f;
  109. mTurretBody = mBodyInterface->CreateBody(turret_body_setings);
  110. mBodyInterface->AddBody(mTurretBody->GetID(), EActivation::Activate);
  111. // Attach turret to body
  112. HingeConstraintSettings turret_hinge;
  113. turret_hinge.mPoint1 = turret_hinge.mPoint2 = body_position + Vec3(0, half_vehicle_height, 0);
  114. turret_hinge.mHingeAxis1 = turret_hinge.mHingeAxis2 = Vec3::sAxisY();
  115. turret_hinge.mNormalAxis1 = turret_hinge.mNormalAxis2 = Vec3::sAxisZ();
  116. turret_hinge.mMotorSettings = MotorSettings(0.5f, 1.0f);
  117. mTurretHinge = static_cast<HingeConstraint *>(turret_hinge.Create(*mTankBody, *mTurretBody));
  118. mTurretHinge->SetMotorState(EMotorState::Position);
  119. mPhysicsSystem->AddConstraint(mTurretHinge);
  120. // Create barrel
  121. RVec3 barrel_position = turret_position + Vec3(0, 0, half_turret_length + half_barrel_length - barrel_rotation_offset);
  122. BodyCreationSettings barrel_body_setings(new CylinderShape(half_barrel_length, barrel_radius), barrel_position, Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING);
  123. barrel_body_setings.mCollisionGroup.SetGroupFilter(filter);
  124. barrel_body_setings.mCollisionGroup.SetGroupID(0);
  125. barrel_body_setings.mCollisionGroup.SetSubGroupID(0);
  126. barrel_body_setings.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  127. barrel_body_setings.mMassPropertiesOverride.mMass = 200.0f;
  128. mBarrelBody = mBodyInterface->CreateBody(barrel_body_setings);
  129. mBodyInterface->AddBody(mBarrelBody->GetID(), EActivation::Activate);
  130. // Attach barrel to turret
  131. HingeConstraintSettings barrel_hinge;
  132. barrel_hinge.mPoint1 = barrel_hinge.mPoint2 = barrel_position - Vec3(0, 0, half_barrel_length);
  133. barrel_hinge.mHingeAxis1 = barrel_hinge.mHingeAxis2 = -Vec3::sAxisX();
  134. barrel_hinge.mNormalAxis1 = barrel_hinge.mNormalAxis2 = Vec3::sAxisZ();
  135. barrel_hinge.mLimitsMin = DegreesToRadians(-10.0f);
  136. barrel_hinge.mLimitsMax = DegreesToRadians(40.0f);
  137. barrel_hinge.mMotorSettings = MotorSettings(10.0f, 1.0f);
  138. mBarrelHinge = static_cast<HingeConstraint *>(barrel_hinge.Create(*mTurretBody, *mBarrelBody));
  139. mBarrelHinge->SetMotorState(EMotorState::Position);
  140. mPhysicsSystem->AddConstraint(mBarrelHinge);
  141. }
  142. void TankTest::ProcessInput(const ProcessInputParams &inParams)
  143. {
  144. const float min_velocity_pivot_turn = 1.0f;
  145. // Determine acceleration and brake
  146. mForward = 0.0f;
  147. mBrake = 0.0f;
  148. if (inParams.mKeyboard->IsKeyPressed(DIK_RSHIFT))
  149. mBrake = 1.0f;
  150. else if (inParams.mKeyboard->IsKeyPressed(DIK_UP))
  151. mForward = 1.0f;
  152. else if (inParams.mKeyboard->IsKeyPressed(DIK_DOWN))
  153. mForward = -1.0f;
  154. // Steering
  155. mLeftRatio = 1.0f;
  156. mRightRatio = 1.0f;
  157. float velocity = (mTankBody->GetRotation().Conjugated() * mTankBody->GetLinearVelocity()).GetZ();
  158. if (inParams.mKeyboard->IsKeyPressed(DIK_LEFT))
  159. {
  160. if (mBrake == 0.0f && mForward == 0.0f && abs(velocity) < min_velocity_pivot_turn)
  161. {
  162. // Pivot turn
  163. mLeftRatio = -1.0f;
  164. mForward = 1.0f;
  165. }
  166. else
  167. mLeftRatio = 0.6f;
  168. }
  169. else if (inParams.mKeyboard->IsKeyPressed(DIK_RIGHT))
  170. {
  171. if (mBrake == 0.0f && mForward == 0.0f && abs(velocity) < min_velocity_pivot_turn)
  172. {
  173. // Pivot turn
  174. mRightRatio = -1.0f;
  175. mForward = 1.0f;
  176. }
  177. else
  178. mRightRatio = 0.6f;
  179. }
  180. // Check if we're reversing direction
  181. if (mPreviousForward * mForward < 0.0f)
  182. {
  183. // Get vehicle velocity in local space to the body of the vehicle
  184. if ((mForward > 0.0f && velocity < -0.1f) || (mForward < 0.0f && velocity > 0.1f))
  185. {
  186. // Brake while we've not stopped yet
  187. mForward = 0.0f;
  188. mBrake = 1.0f;
  189. }
  190. else
  191. {
  192. // When we've come to a stop, accept the new direction
  193. mPreviousForward = mForward;
  194. }
  195. }
  196. // Cast ray to find target
  197. RRayCast ray { inParams.mCameraState.mPos, 1000.0f * inParams.mCameraState.mForward };
  198. RayCastSettings ray_settings;
  199. ClosestHitCollisionCollector<CastRayCollector> collector;
  200. IgnoreMultipleBodiesFilter body_filter;
  201. body_filter.Reserve(3);
  202. body_filter.IgnoreBody(mTankBody->GetID());
  203. body_filter.IgnoreBody(mTurretBody->GetID());
  204. body_filter.IgnoreBody(mBarrelBody->GetID());
  205. mPhysicsSystem->GetNarrowPhaseQuery().CastRay(ray, ray_settings, collector, {}, {}, body_filter);
  206. RVec3 hit_pos = collector.HadHit()? ray.GetPointOnRay(collector.mHit.mFraction) : ray.mOrigin + ray.mDirection;
  207. mDebugRenderer->DrawMarker(hit_pos, Color::sGreen, 1.0f);
  208. // Orient the turret towards the hit position
  209. RMat44 turret_to_world = mTankBody->GetCenterOfMassTransform() * mTurretHinge->GetConstraintToBody1Matrix();
  210. Vec3 hit_pos_in_turret = Vec3(turret_to_world.InversedRotationTranslation() * hit_pos);
  211. mTurretHeading = ATan2(hit_pos_in_turret.GetZ(), hit_pos_in_turret.GetY());
  212. // Orient barrel towards the hit position
  213. RMat44 barrel_to_world = mTurretBody->GetCenterOfMassTransform() * mBarrelHinge->GetConstraintToBody1Matrix();
  214. Vec3 hit_pos_in_barrel = Vec3(barrel_to_world.InversedRotationTranslation() * hit_pos);
  215. mBarrelPitch = ATan2(hit_pos_in_barrel.GetZ(), hit_pos_in_barrel.GetY());
  216. // If user wants to fire
  217. mFire = inParams.mKeyboard->IsKeyPressed(DIK_RETURN);
  218. }
  219. void TankTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  220. {
  221. VehicleTest::PrePhysicsUpdate(inParams);
  222. const float bullet_radius = 0.061f; // 120 mm
  223. const Vec3 bullet_pos = Vec3(0, 1.6f, 0);
  224. const Vec3 bullet_velocity = Vec3(0, 400.0f, 0); // Normal exit velocities are around 1100-1700 m/s, use a lower variable as we have a limit to max velocity (See: https://tanks-encyclopedia.com/coldwar-usa-120mm-gun-tank-m1e1-abrams/)
  225. const float bullet_mass = 40.0f; // Normal projectile weight is around 7 kg, use an increased value so the momentum is more realistic (with the lower exit velocity)
  226. const float bullet_reload_time = 2.0f;
  227. // Assure the tank stays active as we're controlling the turret with the mouse
  228. mBodyInterface->ActivateBody(mTankBody->GetID());
  229. // Pass the input on to the constraint
  230. static_cast<TrackedVehicleController *>(mVehicleConstraint->GetController())->SetDriverInput(mForward, mLeftRatio, mRightRatio, mBrake);
  231. mTurretHinge->SetTargetAngle(mTurretHeading);
  232. mBarrelHinge->SetTargetAngle(mBarrelPitch);
  233. // Update reload time
  234. mReloadTime = max(0.0f, mReloadTime - inParams.mDeltaTime);
  235. // Shoot bullet
  236. if (mReloadTime == 0.0f && mFire)
  237. {
  238. // Create bullet
  239. BodyCreationSettings bullet_creation_settings(new SphereShape(bullet_radius), mBarrelBody->GetCenterOfMassTransform() * bullet_pos, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  240. bullet_creation_settings.mMotionQuality = EMotionQuality::LinearCast;
  241. bullet_creation_settings.mFriction = 1.0f;
  242. bullet_creation_settings.mRestitution = 0.0f;
  243. bullet_creation_settings.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  244. bullet_creation_settings.mMassPropertiesOverride.mMass = bullet_mass;
  245. Body *bullet = mBodyInterface->CreateBody(bullet_creation_settings);
  246. bullet->SetLinearVelocity(mBarrelBody->GetRotation() * bullet_velocity);
  247. mBodyInterface->AddBody(bullet->GetID(), EActivation::Activate);
  248. // Start reloading
  249. mReloadTime = bullet_reload_time;
  250. // Apply opposite impulse to turret body
  251. mBodyInterface->AddImpulse(mTurretBody->GetID(), -bullet->GetLinearVelocity() * bullet_mass);
  252. }
  253. // Draw our wheels (this needs to be done in the pre update since we draw the bodies too in the state before the step)
  254. for (uint w = 0; w < mVehicleConstraint->GetWheels().size(); ++w)
  255. {
  256. const WheelSettings *settings = mVehicleConstraint->GetWheels()[w]->GetSettings();
  257. RMat44 wheel_transform = mVehicleConstraint->GetWheelWorldTransform(w, Vec3::sAxisY(), Vec3::sAxisX()); // The cylinder we draw is aligned with Y so we specify that as rotational axis
  258. mDebugRenderer->DrawCylinder(wheel_transform, 0.5f * settings->mWidth, settings->mRadius, Color::sGreen);
  259. }
  260. }
  261. void TankTest::SaveState(StateRecorder &inStream) const
  262. {
  263. VehicleTest::SaveState(inStream);
  264. inStream.Write(mReloadTime);
  265. }
  266. void TankTest::RestoreState(StateRecorder &inStream)
  267. {
  268. VehicleTest::RestoreState(inStream);
  269. inStream.Read(mReloadTime);
  270. }
  271. void TankTest::SaveInputState(StateRecorder &inStream) const
  272. {
  273. inStream.Write(mForward);
  274. inStream.Write(mPreviousForward);
  275. inStream.Write(mLeftRatio);
  276. inStream.Write(mRightRatio);
  277. inStream.Write(mBrake);
  278. inStream.Write(mTurretHeading);
  279. inStream.Write(mBarrelPitch);
  280. inStream.Write(mFire);
  281. }
  282. void TankTest::RestoreInputState(StateRecorder &inStream)
  283. {
  284. inStream.Read(mForward);
  285. inStream.Read(mPreviousForward);
  286. inStream.Read(mLeftRatio);
  287. inStream.Read(mRightRatio);
  288. inStream.Read(mBrake);
  289. inStream.Read(mTurretHeading);
  290. inStream.Read(mBarrelPitch);
  291. inStream.Read(mFire);
  292. }
  293. void TankTest::GetInitialCamera(CameraState &ioState) const
  294. {
  295. // Position camera behind tank
  296. ioState.mPos = RVec3(0, 4.0f, 0);
  297. ioState.mForward = Vec3(0, -2.0f, 10.0f).Normalized();
  298. }
  299. RMat44 TankTest::GetCameraPivot(float inCameraHeading, float inCameraPitch) const
  300. {
  301. // Pivot is center of tank + a distance away from the tank based on the heading and pitch of the camera
  302. Vec3 fwd = Vec3(Cos(inCameraPitch) * Cos(inCameraHeading), Sin(inCameraPitch), Cos(inCameraPitch) * Sin(inCameraHeading));
  303. return RMat44::sTranslation(mTankBody->GetPosition() - 10.0f * fwd);
  304. }