CharacterVirtualTest.h 5.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 persists colliding with a body.
  23. virtual void OnContactPersisted(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) override;
  24. // Called whenever the character loses contact with a body.
  25. virtual void OnContactRemoved(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2) override;
  26. // Called whenever the character collides with a virtual character.
  27. virtual void OnCharacterContactAdded(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) override;
  28. // Called whenever the character persists colliding with a virtual character.
  29. virtual void OnCharacterContactPersisted(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) override;
  30. // Called whenever the character loses contact with a virtual character.
  31. virtual void OnCharacterContactRemoved(const CharacterVirtual *inCharacter, const CharacterID &inOtherCharacterID, const SubShapeID &inSubShapeID2) override;
  32. // 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).
  33. 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;
  34. protected:
  35. // Common function to be called when contacts are added/persisted
  36. void OnContactCommon(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings);
  37. void OnCharacterContactCommon(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings);
  38. // Get position of the character
  39. virtual RVec3 GetCharacterPosition() const override { return mCharacter->GetPosition(); }
  40. // Handle user input to the character
  41. virtual void HandleInput(Vec3Arg inMovementDirection, bool inJump, bool inSwitchStance, float inDeltaTime) override;
  42. // Add character movement settings
  43. virtual void AddCharacterMovementSettings(DebugUI* inUI, UIElement* inSubMenu) override;
  44. // Add test configuration settings
  45. virtual void AddConfigurationSettings(DebugUI *inUI, UIElement *inSubMenu) override;
  46. private:
  47. // Character movement settings
  48. static inline bool sEnableCharacterInertia = true;
  49. // Test configuration settings
  50. static inline EBackFaceMode sBackFaceMode = EBackFaceMode::CollideWithBackFaces;
  51. static inline float sUpRotationX = 0;
  52. static inline float sUpRotationZ = 0;
  53. static inline float sMaxSlopeAngle = DegreesToRadians(45.0f);
  54. static inline float sMaxStrength = 100.0f;
  55. static inline float sCharacterPadding = 0.02f;
  56. static inline float sPenetrationRecoverySpeed = 1.0f;
  57. static inline float sPredictiveContactDistance = 0.1f;
  58. static inline bool sEnableWalkStairs = true;
  59. static inline bool sEnableStickToFloor = true;
  60. static inline bool sEnhancedInternalEdgeRemoval = false;
  61. static inline bool sCreateInnerBody = false;
  62. static inline bool sPlayerCanPushOtherCharacters = true;
  63. static inline bool sOtherCharactersCanPushPlayer = true;
  64. // The 'player' character
  65. Ref<CharacterVirtual> mCharacter;
  66. // Smoothed value of the player input
  67. Vec3 mDesiredVelocity = Vec3::sZero();
  68. // True when the player is pressing movement controls
  69. bool mAllowSliding = false;
  70. // Track active contacts for debugging purposes
  71. using ContactSet = Array<CharacterVirtual::ContactKey>;
  72. ContactSet mActiveContacts;
  73. };