CharacterVirtualTest.h 3.5 KB

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