CharacterBase.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <Jolt/Core/Reference.h>
  6. #include <Jolt/Core/NonCopyable.h>
  7. #include <Jolt/Physics/Body/BodyID.h>
  8. #include <Jolt/Physics/Collision/Shape/Shape.h>
  9. #include <Jolt/Physics/Collision/Shape/SubShapeID.h>
  10. #include <Jolt/Physics/Collision/PhysicsMaterial.h>
  11. JPH_NAMESPACE_BEGIN
  12. class PhysicsSystem;
  13. class StateRecorder;
  14. /// Base class for configuration of a character
  15. class JPH_EXPORT CharacterBaseSettings : public RefTarget<CharacterBaseSettings>
  16. {
  17. public:
  18. JPH_OVERRIDE_NEW_DELETE
  19. /// Constructor
  20. CharacterBaseSettings() = default;
  21. CharacterBaseSettings(const CharacterBaseSettings &) = default;
  22. CharacterBaseSettings & operator = (const CharacterBaseSettings &) = default;
  23. /// Virtual destructor
  24. virtual ~CharacterBaseSettings() = default;
  25. /// Vector indicating the up direction of the character
  26. Vec3 mUp = Vec3::sAxisY();
  27. /// Plane, defined in local space relative to the character. Every contact behind this plane can support the
  28. /// character, every contact in front of this plane is treated as only colliding with the player.
  29. /// Default: Accept any contact.
  30. Plane mSupportingVolume { Vec3::sAxisY(), -1.0e10f };
  31. /// Maximum angle of slope that character can still walk on (radians).
  32. float mMaxSlopeAngle = DegreesToRadians(50.0f);
  33. /// Set to indicate that extra effort should be made to try to remove ghost contacts (collisions with internal edges of a mesh). This is more expensive but makes bodies move smoother over a mesh with convex edges.
  34. bool mEnhancedInternalEdgeRemoval = false;
  35. /// Initial shape that represents the character's volume.
  36. /// Usually this is a capsule, make sure the shape is made so that the bottom of the shape is at (0, 0, 0).
  37. RefConst<Shape> mShape;
  38. };
  39. /// Base class for character class
  40. class JPH_EXPORT CharacterBase : public RefTarget<CharacterBase>, public NonCopyable
  41. {
  42. public:
  43. JPH_OVERRIDE_NEW_DELETE
  44. /// Constructor
  45. CharacterBase(const CharacterBaseSettings *inSettings, PhysicsSystem *inSystem);
  46. /// Destructor
  47. virtual ~CharacterBase() = default;
  48. /// Set the maximum angle of slope that character can still walk on (radians)
  49. void SetMaxSlopeAngle(float inMaxSlopeAngle) { mCosMaxSlopeAngle = Cos(inMaxSlopeAngle); }
  50. float GetCosMaxSlopeAngle() const { return mCosMaxSlopeAngle; }
  51. /// Set the up vector for the character
  52. void SetUp(Vec3Arg inUp) { mUp = inUp; }
  53. Vec3 GetUp() const { return mUp; }
  54. /// Check if the normal of the ground surface is too steep to walk on
  55. bool IsSlopeTooSteep(Vec3Arg inNormal) const
  56. {
  57. // If cos max slope angle is close to one the system is turned off,
  58. // otherwise check the angle between the up and normal vector
  59. return mCosMaxSlopeAngle < cNoMaxSlopeAngle && inNormal.Dot(mUp) < mCosMaxSlopeAngle;
  60. }
  61. /// Get the current shape that the character is using.
  62. const Shape * GetShape() const { return mShape; }
  63. enum class EGroundState
  64. {
  65. OnGround, ///< Character is on the ground and can move freely.
  66. OnSteepGround, ///< Character is on a slope that is too steep and can't climb up any further. The caller should start applying downward velocity if sliding from the slope is desired.
  67. NotSupported, ///< Character is touching an object, but is not supported by it and should fall. The GetGroundXXX functions will return information about the touched object.
  68. InAir, ///< Character is in the air and is not touching anything.
  69. };
  70. /// Debug function to convert enum values to string
  71. static const char * sToString(EGroundState inState);
  72. ///@name Properties of the ground this character is standing on
  73. /// Current ground state
  74. EGroundState GetGroundState() const { return mGroundState; }
  75. /// Returns true if the player is supported by normal or steep ground
  76. bool IsSupported() const { return mGroundState == EGroundState::OnGround || mGroundState == EGroundState::OnSteepGround; }
  77. /// Get the contact point with the ground
  78. RVec3 GetGroundPosition() const { return mGroundPosition; }
  79. /// Get the contact normal with the ground
  80. Vec3 GetGroundNormal() const { return mGroundNormal; }
  81. /// Velocity in world space of ground
  82. Vec3 GetGroundVelocity() const { return mGroundVelocity; }
  83. /// Material that the character is standing on
  84. const PhysicsMaterial * GetGroundMaterial() const { return mGroundMaterial; }
  85. /// BodyID of the object the character is standing on. Note may have been removed!
  86. BodyID GetGroundBodyID() const { return mGroundBodyID; }
  87. /// Sub part of the body that we're standing on.
  88. SubShapeID GetGroundSubShapeID() const { return mGroundBodySubShapeID; }
  89. /// User data value of the body that we're standing on
  90. uint64 GetGroundUserData() const { return mGroundUserData; }
  91. // Saving / restoring state for replay
  92. virtual void SaveState(StateRecorder &inStream) const;
  93. virtual void RestoreState(StateRecorder &inStream);
  94. protected:
  95. // Cached physics system
  96. PhysicsSystem * mSystem;
  97. // The shape that the body currently has
  98. RefConst<Shape> mShape;
  99. // The character's world space up axis
  100. Vec3 mUp;
  101. // Every contact behind this plane can support the character
  102. Plane mSupportingVolume;
  103. // Beyond this value there is no max slope
  104. static constexpr float cNoMaxSlopeAngle = 0.9999f;
  105. // Cosine of the maximum angle of slope that character can still walk on
  106. float mCosMaxSlopeAngle;
  107. // Ground properties
  108. EGroundState mGroundState = EGroundState::InAir;
  109. BodyID mGroundBodyID;
  110. SubShapeID mGroundBodySubShapeID;
  111. RVec3 mGroundPosition = RVec3::sZero();
  112. Vec3 mGroundNormal = Vec3::sZero();
  113. Vec3 mGroundVelocity = Vec3::sZero();
  114. RefConst<PhysicsMaterial> mGroundMaterial = PhysicsMaterial::sDefault;
  115. uint64 mGroundUserData = 0;
  116. };
  117. JPH_NAMESPACE_END