Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Character/Character.h
21797 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Physics/Character/CharacterBase.h>7#include <Jolt/Physics/Collision/ObjectLayer.h>8#include <Jolt/Physics/Collision/TransformedShape.h>9#include <Jolt/Physics/EActivation.h>10#include <Jolt/Physics/Body/AllowedDOFs.h>1112JPH_NAMESPACE_BEGIN1314/// Contains the configuration of a character15class JPH_EXPORT CharacterSettings : public CharacterBaseSettings16{17public:18JPH_OVERRIDE_NEW_DELETE1920/// Constructor21CharacterSettings() = default;22CharacterSettings(const CharacterSettings &) = default;23CharacterSettings & operator = (const CharacterSettings &) = default;2425/// Layer that this character will be added to26ObjectLayer mLayer = 0;2728/// Mass of the character29float mMass = 80.0f;3031/// Friction for the character32float mFriction = 0.2f;3334/// Value to multiply gravity with for this character35float mGravityFactor = 1.0f;3637/// Allowed degrees of freedom for this character38EAllowedDOFs mAllowedDOFs = EAllowedDOFs::TranslationX | EAllowedDOFs::TranslationY | EAllowedDOFs::TranslationZ;39};4041/// Runtime character object.42/// This object usually represents the player or a humanoid AI. It uses a single rigid body,43/// usually with a capsule shape to simulate movement and collision for the character.44/// The character is a keyframed object, the application controls it by setting the velocity.45class JPH_EXPORT Character : public CharacterBase46{47public:48JPH_OVERRIDE_NEW_DELETE4950/// Constructor51/// @param inSettings The settings for the character52/// @param inPosition Initial position for the character53/// @param inRotation Initial rotation for the character (usually only around Y)54/// @param inUserData Application specific value55/// @param inSystem Physics system that this character will be added to later56Character(const CharacterSettings *inSettings, RVec3Arg inPosition, QuatArg inRotation, uint64 inUserData, PhysicsSystem *inSystem);5758/// Destructor59virtual ~Character() override;6061/// Add bodies and constraints to the system and optionally activate the bodies62void AddToPhysicsSystem(EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true);6364/// Remove bodies and constraints from the system65void RemoveFromPhysicsSystem(bool inLockBodies = true);6667/// Wake up the character68void Activate(bool inLockBodies = true);6970/// Needs to be called after every PhysicsSystem::Update71/// @param inMaxSeparationDistance Max distance between the floor and the character to still consider the character standing on the floor72/// @param inLockBodies If the collision query should use the locking body interface (true) or the non locking body interface (false)73void PostSimulation(float inMaxSeparationDistance, bool inLockBodies = true);7475/// Control the velocity of the character76void SetLinearAndAngularVelocity(Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity, bool inLockBodies = true);7778/// Get the linear velocity of the character (m / s)79Vec3 GetLinearVelocity(bool inLockBodies = true) const;8081/// Set the linear velocity of the character (m / s)82void SetLinearVelocity(Vec3Arg inLinearVelocity, bool inLockBodies = true);8384/// Add world space linear velocity to current velocity (m / s)85void AddLinearVelocity(Vec3Arg inLinearVelocity, bool inLockBodies = true);8687/// Add impulse to the center of mass of the character88void AddImpulse(Vec3Arg inImpulse, bool inLockBodies = true);8990/// Get the body associated with this character91BodyID GetBodyID() const { return mBodyID; }9293/// Get position / rotation of the body94void GetPositionAndRotation(RVec3 &outPosition, Quat &outRotation, bool inLockBodies = true) const;9596/// Set the position / rotation of the body, optionally activating it.97void SetPositionAndRotation(RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true) const;9899/// Get the position of the character100RVec3 GetPosition(bool inLockBodies = true) const;101102/// Set the position of the character, optionally activating it.103void SetPosition(RVec3Arg inPosition, EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true);104105/// Get the rotation of the character106Quat GetRotation(bool inLockBodies = true) const;107108/// Set the rotation of the character, optionally activating it.109void SetRotation(QuatArg inRotation, EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true);110111/// Position of the center of mass of the underlying rigid body112RVec3 GetCenterOfMassPosition(bool inLockBodies = true) const;113114/// Calculate the world transform of the character115RMat44 GetWorldTransform(bool inLockBodies = true) const;116117/// Get the layer of the character118ObjectLayer GetLayer() const { return mLayer; }119120/// Update the layer of the character121void SetLayer(ObjectLayer inLayer, bool inLockBodies = true);122123/// Switch the shape of the character (e.g. for stance). When inMaxPenetrationDepth is not FLT_MAX, it checks124/// if the new shape collides before switching shape. Returns true if the switch succeeded.125bool SetShape(const Shape *inShape, float inMaxPenetrationDepth, bool inLockBodies = true);126127/// Get the transformed shape that represents the volume of the character, can be used for collision checks.128TransformedShape GetTransformedShape(bool inLockBodies = true) const;129130/// @brief Get all contacts for the character at a particular location131/// @param inPosition Position to test.132/// @param inRotation Rotation at which to test the shape.133/// @param inMovementDirection A hint in which direction the character is moving, will be used to calculate a proper normal.134/// @param inMaxSeparationDistance How much distance around the character you want to report contacts in (can be 0 to match the character exactly).135/// @param inShape Shape to test collision with.136/// @param inBaseOffset All hit results will be returned relative to this offset, can be zero to get results in world position, but when you're testing far from the origin you get better precision by picking a position that's closer e.g. GetPosition() since floats are most accurate near the origin137/// @param ioCollector Collision collector that receives the collision results.138/// @param inLockBodies If the collision query should use the locking body interface (true) or the non locking body interface (false)139void CheckCollision(RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inMovementDirection, float inMaxSeparationDistance, const Shape *inShape, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, bool inLockBodies = true) const;140141/// Get the character settings that can recreate this character142CharacterSettings GetCharacterSettings(bool inLockBodies = true) const;143144private:145/// Check collisions between inShape and the world using the center of mass transform146void CheckCollision(RMat44Arg inCenterOfMassTransform, Vec3Arg inMovementDirection, float inMaxSeparationDistance, const Shape *inShape, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, bool inLockBodies) const;147148/// Check collisions between inShape and the world using the current position / rotation of the character149void CheckCollision(const Shape *inShape, float inMaxSeparationDistance, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, bool inLockBodies) const;150151/// The body of this character152BodyID mBodyID;153154/// The layer the body is in155ObjectLayer mLayer;156};157158JPH_NAMESPACE_END159160161