CharacterVirtualTest.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // 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(JPH_NO_EXPORT, 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. /// 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.
  19. virtual void OnAdjustBodyVelocity(const CharacterVirtual *inCharacter, const Body &inBody2, Vec3 &ioLinearVelocity, Vec3 &ioAngularVelocity) override;
  20. // Called whenever the character collides with a body.
  21. virtual void OnContactAdded(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) override;
  22. // Called whenever the character collides with a virtual character.
  23. virtual void OnCharacterContactAdded(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) override;
  24. // 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).
  25. 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;
  26. protected:
  27. // Get position of the character
  28. virtual RVec3 GetCharacterPosition() const override { return mCharacter->GetPosition(); }
  29. // Handle user input to the character
  30. virtual void HandleInput(Vec3Arg inMovementDirection, bool inJump, bool inSwitchStance, float inDeltaTime) override;
  31. // Add character movement settings
  32. virtual void AddCharacterMovementSettings(DebugUI* inUI, UIElement* inSubMenu) override;
  33. // Add test configuration settings
  34. virtual void AddConfigurationSettings(DebugUI *inUI, UIElement *inSubMenu) override;
  35. private:
  36. // Character movement settings
  37. static inline bool sEnableCharacterInertia = true;
  38. // Test configuration settings
  39. static inline EBackFaceMode sBackFaceMode = EBackFaceMode::CollideWithBackFaces;
  40. static inline float sUpRotationX = 0;
  41. static inline float sUpRotationZ = 0;
  42. static inline float sMaxSlopeAngle = DegreesToRadians(45.0f);
  43. static inline float sMaxStrength = 100.0f;
  44. static inline float sCharacterPadding = 0.02f;
  45. static inline float sPenetrationRecoverySpeed = 1.0f;
  46. static inline float sPredictiveContactDistance = 0.1f;
  47. static inline bool sEnableWalkStairs = true;
  48. static inline bool sEnableStickToFloor = true;
  49. static inline bool sEnhancedInternalEdgeRemoval = false;
  50. static inline bool sCreateInnerBody = false;
  51. static inline bool sPlayerCanPushOtherCharacters = true;
  52. static inline bool sOtherCharactersCanPushPlayer = true;
  53. // The 'player' character
  54. Ref<CharacterVirtual> mCharacter;
  55. // Smoothed value of the player input
  56. Vec3 mDesiredVelocity = Vec3::sZero();
  57. // True when the player is pressing movement controls
  58. bool mAllowSliding = false;
  59. };