CharacterSpaceShipTest.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // Update the test, called before the physics update
  17. virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) override;
  18. // Override to specify the initial camera state (local to GetCameraPivot)
  19. virtual void GetInitialCamera(CameraState &ioState) const override;
  20. // Override to specify a camera pivot point and orientation (world space)
  21. virtual RMat44 GetCameraPivot(float inCameraHeading, float inCameraPitch) const override;
  22. // Saving / restoring state for replay
  23. virtual void SaveState(StateRecorder &inStream) const override;
  24. virtual void RestoreState(StateRecorder &inStream) override;
  25. private:
  26. // Calculate new ship velocity
  27. void UpdateShipVelocity();
  28. /// 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.
  29. virtual void OnAdjustBodyVelocity(const CharacterVirtual *inCharacter, const Body &inBody2, Vec3 &ioLinearVelocity, Vec3 &ioAngularVelocity) override;
  30. // Character size
  31. static constexpr float cCharacterHeightStanding = 1.35f;
  32. static constexpr float cCharacterRadiusStanding = 0.3f;
  33. static constexpr float cCharacterSpeed = 6.0f;
  34. static constexpr float cJumpSpeed = 4.0f;
  35. // The 'player' character
  36. Ref<CharacterVirtual> mCharacter;
  37. // The space ship
  38. BodyID mSpaceShip;
  39. // Previous frame space ship transform
  40. RMat44 mSpaceShipPrevTransform;
  41. // Space ship velocity
  42. Vec3 mSpaceShipLinearVelocity;
  43. Vec3 mSpaceShipAngularVelocity;
  44. // Global time
  45. float mTime = 0.0f;
  46. // Smoothed value of the player input
  47. Vec3 mDesiredVelocity = Vec3::sZero();
  48. };