CharacterSpaceShipTest.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Tests/Test.h>
  6. #include <Jolt/Physics/Character/CharacterVirtual.h>
  7. class CharacterSpaceShipTest : public Test, public CharacterContactListener
  8. {
  9. public:
  10. JPH_DECLARE_RTTI_VIRTUAL(JPH_NO_EXPORT, CharacterSpaceShipTest)
  11. // Description of the test
  12. virtual const char * GetDescription() const override
  13. {
  14. return "Demonstrates how a character may walk around a fast moving/accelerating sci-fi space ship that is equipped with inertial dampeners.\n"
  15. "Note that this is 'game physics' and not real physics, inertial dampeners only exist in the movies.\n"
  16. "You can walk off the ship and remain attached to the ship. A proper implementation would detect this and detach the character.";
  17. }
  18. // Initialize the test
  19. virtual void Initialize() override;
  20. // Process input
  21. virtual void ProcessInput(const ProcessInputParams &inParams) override;
  22. // Update the test, called before the physics update
  23. virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) override;
  24. // Override to specify the initial camera state (local to GetCameraPivot)
  25. virtual void GetInitialCamera(CameraState &ioState) const override;
  26. // Override to specify a camera pivot point and orientation (world space)
  27. virtual RMat44 GetCameraPivot(float inCameraHeading, float inCameraPitch) const override;
  28. // Saving / restoring state for replay
  29. virtual void SaveState(StateRecorder &inStream) const override;
  30. virtual void RestoreState(StateRecorder &inStream) override;
  31. // Saving / restoring controller input state for replay
  32. virtual void SaveInputState(StateRecorder &inStream) const override;
  33. virtual void RestoreInputState(StateRecorder &inStream) override;
  34. private:
  35. // Calculate new ship velocity
  36. void UpdateShipVelocity();
  37. /// Callback to adjust the velocity of a body as seen by the character. Can be adjusted to e.g. implement a conveyor belt or an inertial dampener system of a sci-fi space ship.
  38. virtual void OnAdjustBodyVelocity(const CharacterVirtual *inCharacter, const Body &inBody2, Vec3 &ioLinearVelocity, Vec3 &ioAngularVelocity) override;
  39. // Character size
  40. static constexpr float cCharacterHeightStanding = 1.35f;
  41. static constexpr float cCharacterRadiusStanding = 0.3f;
  42. static constexpr float cCharacterSpeed = 6.0f;
  43. static constexpr float cJumpSpeed = 4.0f;
  44. // The 'player' character
  45. Ref<CharacterVirtual> mCharacter;
  46. // The space ship
  47. BodyID mSpaceShip;
  48. // Previous frame space ship transform
  49. RMat44 mSpaceShipPrevTransform;
  50. // Space ship velocity
  51. Vec3 mSpaceShipLinearVelocity;
  52. Vec3 mSpaceShipAngularVelocity;
  53. // Global time
  54. float mTime = 0.0f;
  55. // Player input
  56. Vec3 mDesiredVelocity = Vec3::sZero();
  57. bool mJump = false;
  58. bool mWasJump = false;
  59. };