CharacterVirtualTest.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Tests/Character/CharacterBaseTest.h>
  5. #include <Jolt/Physics/Character/CharacterVirtual.h>
  6. // Simple test that test the CharacterVirtual class. Allows the user to move around with the arrow keys and jump with the J button.
  7. class CharacterVirtualTest : public CharacterBaseTest, public CharacterContactListener
  8. {
  9. public:
  10. JPH_DECLARE_RTTI_VIRTUAL(CharacterVirtualTest)
  11. // Initialize the test
  12. virtual void Initialize() override;
  13. // Update the test, called before the physics update
  14. virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) override;
  15. // Saving / restoring state for replay
  16. virtual void SaveState(StateRecorder &inStream) const override;
  17. virtual void RestoreState(StateRecorder &inStream) override;
  18. // Called whenever the character collides with a body. Returns true if the contact can push the character.
  19. virtual void OnContactAdded(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, Vec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) override;
  20. // Called whenever the character movement is solved and a constraint is hit. Allows the listener to override the resulting character velocity (e.g. by preventing sliding along certain surfaces).
  21. virtual void OnContactSolve(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, Vec3Arg inContactPosition, Vec3Arg inContactNormal, Vec3Arg inContactVelocity, const PhysicsMaterial *inContactMaterial, Vec3Arg inCharacterVelocity, Vec3 &ioNewCharacterVelocity) override;
  22. protected:
  23. // Get position of the character
  24. virtual Vec3 GetCharacterPosition() const override { return mCharacter->GetPosition(); }
  25. // Handle user input to the character
  26. virtual void HandleInput(Vec3Arg inMovementDirection, bool inJump, bool inSwitchStance, float inDeltaTime) override;
  27. // Add test configuration settings
  28. virtual void AddConfigurationSettings(DebugUI *inUI, UIElement *inSubMenu) override;
  29. private:
  30. // Test settings
  31. static inline float sMaxSlopeAngle = DegreesToRadians(45.0f);
  32. static inline float sMaxStrength = 100.0f;
  33. static inline float sCharacterPadding = 0.02f;
  34. static inline float sPenetrationRecoverySpeed = 1.0f;
  35. static inline float sPredictiveContactDistance = 0.1f;
  36. static inline bool sEnableWalkStairs = true;
  37. static inline bool sEnableStickToFloor = true;
  38. // The 'player' character
  39. Ref<CharacterVirtual> mCharacter;
  40. // Smoothed value of the player input
  41. Vec3 mDesiredVelocity = Vec3::sZero();
  42. // True when the player is pressing movement controls
  43. bool mAllowSliding = false;
  44. };