CharacterVirtualTest.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. class CharacterVirtualTest : public CharacterBaseTest, public CharacterContactListener
  7. {
  8. public:
  9. JPH_DECLARE_RTTI_VIRTUAL(JPH_NO_EXPORT, CharacterVirtualTest)
  10. // Description of the test
  11. virtual const char * GetDescription() const override
  12. {
  13. return "Shows the CharacterVirtual class. Move around with the arrow keys, Shift for crouch and Ctrl for jump.";
  14. }
  15. // Initialize the test
  16. virtual void Initialize() override;
  17. // Update the test, called before the physics update
  18. virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) override;
  19. // Saving / restoring state for replay
  20. virtual void SaveState(StateRecorder &inStream) const override;
  21. virtual void RestoreState(StateRecorder &inStream) override;
  22. /// 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.
  23. virtual void OnAdjustBodyVelocity(const CharacterVirtual *inCharacter, const Body &inBody2, Vec3 &ioLinearVelocity, Vec3 &ioAngularVelocity) override;
  24. // Called whenever the character collides with a body.
  25. virtual void OnContactAdded(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) override;
  26. // Called whenever the character persists colliding with a body.
  27. virtual void OnContactPersisted(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) override;
  28. // Called whenever the character loses contact with a body.
  29. virtual void OnContactRemoved(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2) override;
  30. // Called whenever the character collides with a virtual character.
  31. virtual void OnCharacterContactAdded(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) override;
  32. // Called whenever the character persists colliding with a virtual character.
  33. virtual void OnCharacterContactPersisted(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings) override;
  34. // Called whenever the character loses contact with a virtual character.
  35. virtual void OnCharacterContactRemoved(const CharacterVirtual *inCharacter, const CharacterID &inOtherCharacterID, const SubShapeID &inSubShapeID2) override;
  36. // 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).
  37. 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;
  38. protected:
  39. // Common function to be called when contacts are added/persisted
  40. void OnContactCommon(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings);
  41. void OnCharacterContactCommon(const CharacterVirtual *inCharacter, const CharacterVirtual *inOtherCharacter, const SubShapeID &inSubShapeID2, RVec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings);
  42. // Get position of the character
  43. virtual RVec3 GetCharacterPosition() const override { return mCharacter->GetPosition(); }
  44. // Handle user input to the character
  45. virtual void HandleInput(Vec3Arg inMovementDirection, bool inJump, bool inSwitchStance, float inDeltaTime) override;
  46. // Add character movement settings
  47. virtual void AddCharacterMovementSettings(DebugUI* inUI, UIElement* inSubMenu) override;
  48. // Add test configuration settings
  49. virtual void AddConfigurationSettings(DebugUI *inUI, UIElement *inSubMenu) override;
  50. private:
  51. // Character movement settings
  52. static inline bool sEnableCharacterInertia = true;
  53. // Test configuration settings
  54. static inline EBackFaceMode sBackFaceMode = EBackFaceMode::CollideWithBackFaces;
  55. static inline float sUpRotationX = 0;
  56. static inline float sUpRotationZ = 0;
  57. static inline float sMaxSlopeAngle = DegreesToRadians(45.0f);
  58. static inline float sMaxStrength = 100.0f;
  59. static inline float sCharacterPadding = 0.02f;
  60. static inline float sPenetrationRecoverySpeed = 1.0f;
  61. static inline float sPredictiveContactDistance = 0.1f;
  62. static inline bool sEnableWalkStairs = true;
  63. static inline bool sEnableStickToFloor = true;
  64. static inline bool sEnhancedInternalEdgeRemoval = false;
  65. static inline bool sCreateInnerBody = false;
  66. static inline bool sPlayerCanPushOtherCharacters = true;
  67. static inline bool sOtherCharactersCanPushPlayer = true;
  68. // The 'player' character
  69. Ref<CharacterVirtual> mCharacter;
  70. // Smoothed value of the player input
  71. Vec3 mDesiredVelocity = Vec3::sZero();
  72. // True when the player is pressing movement controls
  73. bool mAllowSliding = false;
  74. // Track active contacts for debugging purposes
  75. using ContactSet = Array<CharacterVirtual::ContactKey>;
  76. ContactSet mActiveContacts;
  77. };