CharacterSpaceShipTest.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // A test that demonstrates how a character may walk around a fast moving/accelerating sci-fi space ship that is equipped with inertial dampeners.
  8. // Note that this is 'game physics' and not real physics, inertial dampeners only exist in the movies.
  9. // You can walk off the ship and remain attached to the ship. A proper implementation would detect this and detach the character.
  10. class CharacterSpaceShipTest : public Test, public CharacterContactListener
  11. {
  12. public:
  13. JPH_DECLARE_RTTI_VIRTUAL(JPH_NO_EXPORT, CharacterSpaceShipTest)
  14. // Initialize the test
  15. virtual void Initialize() override;
  16. // Process input
  17. virtual void ProcessInput(const ProcessInputParams &inParams) override;
  18. // Update the test, called before the physics update
  19. virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) override;
  20. // Override to specify the initial camera state (local to GetCameraPivot)
  21. virtual void GetInitialCamera(CameraState &ioState) const override;
  22. // Override to specify a camera pivot point and orientation (world space)
  23. virtual RMat44 GetCameraPivot(float inCameraHeading, float inCameraPitch) const override;
  24. // Saving / restoring state for replay
  25. virtual void SaveState(StateRecorder &inStream) const override;
  26. virtual void RestoreState(StateRecorder &inStream) override;
  27. // Saving / restoring controller input state for replay
  28. virtual void SaveInputState(StateRecorder &inStream) const override;
  29. virtual void RestoreInputState(StateRecorder &inStream) override;
  30. private:
  31. // Calculate new ship velocity
  32. void UpdateShipVelocity();
  33. /// 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.
  34. virtual void OnAdjustBodyVelocity(const CharacterVirtual *inCharacter, const Body &inBody2, Vec3 &ioLinearVelocity, Vec3 &ioAngularVelocity) override;
  35. // Character size
  36. static constexpr float cCharacterHeightStanding = 1.35f;
  37. static constexpr float cCharacterRadiusStanding = 0.3f;
  38. static constexpr float cCharacterSpeed = 6.0f;
  39. static constexpr float cJumpSpeed = 4.0f;
  40. // The 'player' character
  41. Ref<CharacterVirtual> mCharacter;
  42. // The space ship
  43. BodyID mSpaceShip;
  44. // Previous frame space ship transform
  45. RMat44 mSpaceShipPrevTransform;
  46. // Space ship velocity
  47. Vec3 mSpaceShipLinearVelocity;
  48. Vec3 mSpaceShipAngularVelocity;
  49. // Global time
  50. float mTime = 0.0f;
  51. // Player input
  52. Vec3 mDesiredVelocity = Vec3::sZero();
  53. bool mJump = false;
  54. bool mWasJump = false;
  55. };