BoatTest.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Tests/Test.h>
  6. #include <Jolt/Physics/Collision/ContactListener.h>
  7. class BoatTest : public Test, public ContactListener
  8. {
  9. public:
  10. JPH_DECLARE_RTTI_VIRTUAL(JPH_NO_EXPORT, BoatTest)
  11. // Description of the test
  12. virtual const char * GetDescription() const override
  13. {
  14. return "Shows how a boat could be constructed and how waves could be simulated.";
  15. }
  16. // If this test implements a contact listener, it should be returned here
  17. virtual ContactListener * GetContactListener() override { return this; }
  18. // See: Test
  19. virtual void Initialize() override;
  20. virtual void ProcessInput(const ProcessInputParams &inParams) override;
  21. virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) override;
  22. virtual void SaveInputState(StateRecorder &inStream) const override;
  23. virtual void RestoreInputState(StateRecorder &inStream) override;
  24. virtual void SaveState(StateRecorder &inStream) const override;
  25. virtual void RestoreState(StateRecorder &inStream) override;
  26. virtual void GetInitialCamera(CameraState &ioState) const override;
  27. virtual RMat44 GetCameraPivot(float inCameraHeading, float inCameraPitch) const override { return mCameraPivot; }
  28. // See: ContactListener
  29. virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override;
  30. virtual void OnContactRemoved(const SubShapeIDPair &inSubShapePair) override;
  31. private:
  32. void UpdateCameraPivot();
  33. // Determines the water surface position at a given XZ position
  34. RVec3 GetWaterSurfacePosition(RVec3Arg inXZPosition) const;
  35. // Configuration
  36. static constexpr float cMaxWaterHeight = 5.0f;
  37. static constexpr float cMinWaterHeight = 3.0f;
  38. static constexpr float cWaterWidth = 100.0f;
  39. static constexpr float cHalfBoatLength = 4.0f;
  40. static constexpr float cHalfBoatTopWidth = 1.5f;
  41. static constexpr float cHalfBoatBottomWidth = 1.2f;
  42. static constexpr float cBoatBowLength = 2.0f;
  43. static constexpr float cHalfBoatHeight = 0.75f;
  44. static constexpr float cBoatMass = 1000.0f;
  45. static constexpr float cBoatBuoyancy = 3.0f;
  46. static constexpr float cBoatLinearDrag = 0.5f;
  47. static constexpr float cBoatAngularDrag = 0.7f;
  48. static constexpr float cBarrelMass = 50.0f;
  49. static constexpr float cBarrelBuoyancy = 1.5f;
  50. static constexpr float cBarrelLinearDrag = 0.5f;
  51. static constexpr float cBarrelAngularDrag = 0.1f;
  52. static constexpr float cForwardAcceleration = 15.0f;
  53. static constexpr float cSteerAcceleration = 1.5f;
  54. // The boat
  55. Body * mBoatBody;
  56. // The sensor that detects objects in the water
  57. BodyID mWaterSensor;
  58. // The camera pivot, recorded before the physics update to align with the drawn world
  59. RMat44 mCameraPivot = RMat44::sIdentity();
  60. // Keeping track of which bodies are in the water
  61. Mutex mBodiesInWaterMutex;
  62. BodyIDVector mBodiesInWater;
  63. // Time
  64. float mTime = 0.0f;
  65. // Player input
  66. float mForward = 0.0f;
  67. float mRight = 0.0f;
  68. };