BoatTest.h 3.0 KB

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