Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Character/CharacterBase.h
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Core/Reference.h>7#include <Jolt/Core/NonCopyable.h>8#include <Jolt/Physics/Body/BodyID.h>9#include <Jolt/Physics/Collision/Shape/Shape.h>10#include <Jolt/Physics/Collision/Shape/SubShapeID.h>11#include <Jolt/Physics/Collision/PhysicsMaterial.h>1213JPH_NAMESPACE_BEGIN1415class PhysicsSystem;16class StateRecorder;1718/// Base class for configuration of a character19class JPH_EXPORT CharacterBaseSettings : public RefTarget<CharacterBaseSettings>20{21public:22JPH_OVERRIDE_NEW_DELETE2324/// Constructor25CharacterBaseSettings() = default;26CharacterBaseSettings(const CharacterBaseSettings &inSettings) = default;27CharacterBaseSettings & operator = (const CharacterBaseSettings &inSettings) = default;2829/// Virtual destructor30virtual ~CharacterBaseSettings() = default;3132/// Vector indicating the up direction of the character33Vec3 mUp = Vec3::sAxisY();3435/// Plane, defined in local space relative to the character. Every contact behind this plane can support the36/// character, every contact in front of this plane is treated as only colliding with the player.37/// Default: Accept any contact.38Plane mSupportingVolume { Vec3::sAxisY(), -1.0e10f };3940/// Maximum angle of slope that character can still walk on (radians).41float mMaxSlopeAngle = DegreesToRadians(50.0f);4243/// 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.44bool mEnhancedInternalEdgeRemoval = false;4546/// Initial shape that represents the character's volume.47/// Usually this is a capsule, make sure the shape is made so that the bottom of the shape is at (0, 0, 0).48RefConst<Shape> mShape;49};5051/// Base class for character class52class JPH_EXPORT CharacterBase : public RefTarget<CharacterBase>, public NonCopyable53{54public:55JPH_OVERRIDE_NEW_DELETE5657/// Constructor58CharacterBase(const CharacterBaseSettings *inSettings, PhysicsSystem *inSystem);5960/// Destructor61virtual ~CharacterBase() = default;6263/// Set the maximum angle of slope that character can still walk on (radians)64void SetMaxSlopeAngle(float inMaxSlopeAngle) { mCosMaxSlopeAngle = Cos(inMaxSlopeAngle); }65float GetCosMaxSlopeAngle() const { return mCosMaxSlopeAngle; }6667/// Set the up vector for the character68void SetUp(Vec3Arg inUp) { mUp = inUp; }69Vec3 GetUp() const { return mUp; }7071/// Check if the normal of the ground surface is too steep to walk on72bool IsSlopeTooSteep(Vec3Arg inNormal) const73{74// If cos max slope angle is close to one the system is turned off,75// otherwise check the angle between the up and normal vector76return mCosMaxSlopeAngle < cNoMaxSlopeAngle && inNormal.Dot(mUp) < mCosMaxSlopeAngle;77}7879/// Get the current shape that the character is using.80const Shape * GetShape() const { return mShape; }8182enum class EGroundState83{84OnGround, ///< Character is on the ground and can move freely.85OnSteepGround, ///< 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.86NotSupported, ///< Character is touching an object, but is not supported by it and should fall. The GetGroundXXX functions will return information about the touched object.87InAir, ///< Character is in the air and is not touching anything.88};8990/// Debug function to convert enum values to string91static const char * sToString(EGroundState inState);9293///@name Properties of the ground this character is standing on9495/// Current ground state96EGroundState GetGroundState() const { return mGroundState; }9798/// Returns true if the player is supported by normal or steep ground99bool IsSupported() const { return mGroundState == EGroundState::OnGround || mGroundState == EGroundState::OnSteepGround; }100101/// Get the contact point with the ground102RVec3 GetGroundPosition() const { return mGroundPosition; }103104/// Get the contact normal with the ground105Vec3 GetGroundNormal() const { return mGroundNormal; }106107/// Velocity in world space of ground108Vec3 GetGroundVelocity() const { return mGroundVelocity; }109110/// Material that the character is standing on111const PhysicsMaterial * GetGroundMaterial() const { return mGroundMaterial; }112113/// BodyID of the object the character is standing on. Note may have been removed!114BodyID GetGroundBodyID() const { return mGroundBodyID; }115116/// Sub part of the body that we're standing on.117SubShapeID GetGroundSubShapeID() const { return mGroundBodySubShapeID; }118119/// User data value of the body that we're standing on120uint64 GetGroundUserData() const { return mGroundUserData; }121122// Saving / restoring state for replay123virtual void SaveState(StateRecorder &inStream) const;124virtual void RestoreState(StateRecorder &inStream);125126protected:127// Cached physics system128PhysicsSystem * mSystem;129130// The shape that the body currently has131RefConst<Shape> mShape;132133// The character's world space up axis134Vec3 mUp;135136// Every contact behind this plane can support the character137Plane mSupportingVolume;138139// Beyond this value there is no max slope140static constexpr float cNoMaxSlopeAngle = 0.9999f;141142// Cosine of the maximum angle of slope that character can still walk on143float mCosMaxSlopeAngle;144145// Ground properties146EGroundState mGroundState = EGroundState::InAir;147BodyID mGroundBodyID;148SubShapeID mGroundBodySubShapeID;149RVec3 mGroundPosition = RVec3::sZero();150Vec3 mGroundNormal = Vec3::sZero();151Vec3 mGroundVelocity = Vec3::sZero();152RefConst<PhysicsMaterial> mGroundMaterial = PhysicsMaterial::sDefault;153uint64 mGroundUserData = 0;154};155156JPH_NAMESPACE_END157158159