CharacterPlanetTest.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/Character/CharacterVirtual.h>
  7. #include <Jolt/Physics/PhysicsStepListener.h>
  8. // Demonstrates how to do custom gravity to simulate a character walking on a planet
  9. class CharacterPlanetTest : public Test, public PhysicsStepListener, public CharacterContactListener
  10. {
  11. public:
  12. JPH_DECLARE_RTTI_VIRTUAL(JPH_NO_EXPORT, CharacterPlanetTest)
  13. // Initialize the test
  14. virtual void Initialize() override;
  15. // Process input
  16. virtual void ProcessInput(const ProcessInputParams &inParams) override;
  17. // Update the test, called before the physics update
  18. virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) override;
  19. // Override to specify the initial camera state (local to GetCameraPivot)
  20. virtual void GetInitialCamera(CameraState &ioState) const override;
  21. // Override to specify a camera pivot point and orientation (world space)
  22. virtual RMat44 GetCameraPivot(float inCameraHeading, float inCameraPitch) const override;
  23. // Saving / restoring state for replay
  24. virtual void SaveState(StateRecorder &inStream) const override;
  25. virtual void RestoreState(StateRecorder &inStream) override;
  26. // Saving / restoring controller input state for replay
  27. virtual void SaveInputState(StateRecorder &inStream) const override;
  28. virtual void RestoreInputState(StateRecorder &inStream) override;
  29. // See: PhysicsStepListener
  30. virtual void OnStep(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override;
  31. // See: CharacterContactListener
  32. virtual void OnContactAdded(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) override;
  33. private:
  34. // Planet size
  35. static constexpr float cPlanetRadius = 20.0f;
  36. // Character size
  37. static constexpr float cCharacterHeightStanding = 1.35f;
  38. static constexpr float cCharacterRadiusStanding = 0.3f;
  39. static constexpr float cCharacterSpeed = 6.0f;
  40. static constexpr float cJumpSpeed = 4.0f;
  41. // The 'player' character
  42. Ref<CharacterVirtual> mCharacter;
  43. // Player input
  44. Vec3 mDesiredVelocity = Vec3::sZero();
  45. Vec3 mDesiredVelocityWS = Vec3::sZero();
  46. bool mJump = false;
  47. bool mWasJump = false;
  48. };