Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Character/Character.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/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/// Layer that this character will be added to21ObjectLayer mLayer = 0;2223/// Mass of the character24float mMass = 80.0f;2526/// Friction for the character27float mFriction = 0.2f;2829/// Value to multiply gravity with for this character30float mGravityFactor = 1.0f;3132/// Allowed degrees of freedom for this character33EAllowedDOFs mAllowedDOFs = EAllowedDOFs::TranslationX | EAllowedDOFs::TranslationY | EAllowedDOFs::TranslationZ;34};3536/// Runtime character object.37/// This object usually represents the player or a humanoid AI. It uses a single rigid body,38/// usually with a capsule shape to simulate movement and collision for the character.39/// The character is a keyframed object, the application controls it by setting the velocity.40class JPH_EXPORT Character : public CharacterBase41{42public:43JPH_OVERRIDE_NEW_DELETE4445/// Constructor46/// @param inSettings The settings for the character47/// @param inPosition Initial position for the character48/// @param inRotation Initial rotation for the character (usually only around Y)49/// @param inUserData Application specific value50/// @param inSystem Physics system that this character will be added to later51Character(const CharacterSettings *inSettings, RVec3Arg inPosition, QuatArg inRotation, uint64 inUserData, PhysicsSystem *inSystem);5253/// Destructor54virtual ~Character() override;5556/// Add bodies and constraints to the system and optionally activate the bodies57void AddToPhysicsSystem(EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true);5859/// Remove bodies and constraints from the system60void RemoveFromPhysicsSystem(bool inLockBodies = true);6162/// Wake up the character63void Activate(bool inLockBodies = true);6465/// Needs to be called after every PhysicsSystem::Update66/// @param inMaxSeparationDistance Max distance between the floor and the character to still consider the character standing on the floor67/// @param inLockBodies If the collision query should use the locking body interface (true) or the non locking body interface (false)68void PostSimulation(float inMaxSeparationDistance, bool inLockBodies = true);6970/// Control the velocity of the character71void SetLinearAndAngularVelocity(Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity, bool inLockBodies = true);7273/// Get the linear velocity of the character (m / s)74Vec3 GetLinearVelocity(bool inLockBodies = true) const;7576/// Set the linear velocity of the character (m / s)77void SetLinearVelocity(Vec3Arg inLinearVelocity, bool inLockBodies = true);7879/// Add world space linear velocity to current velocity (m / s)80void AddLinearVelocity(Vec3Arg inLinearVelocity, bool inLockBodies = true);8182/// Add impulse to the center of mass of the character83void AddImpulse(Vec3Arg inImpulse, bool inLockBodies = true);8485/// Get the body associated with this character86BodyID GetBodyID() const { return mBodyID; }8788/// Get position / rotation of the body89void GetPositionAndRotation(RVec3 &outPosition, Quat &outRotation, bool inLockBodies = true) const;9091/// Set the position / rotation of the body, optionally activating it.92void SetPositionAndRotation(RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true) const;9394/// Get the position of the character95RVec3 GetPosition(bool inLockBodies = true) const;9697/// Set the position of the character, optionally activating it.98void SetPosition(RVec3Arg inPosition, EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true);99100/// Get the rotation of the character101Quat GetRotation(bool inLockBodies = true) const;102103/// Set the rotation of the character, optionally activating it.104void SetRotation(QuatArg inRotation, EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true);105106/// Position of the center of mass of the underlying rigid body107RVec3 GetCenterOfMassPosition(bool inLockBodies = true) const;108109/// Calculate the world transform of the character110RMat44 GetWorldTransform(bool inLockBodies = true) const;111112/// Get the layer of the character113ObjectLayer GetLayer() const { return mLayer; }114115/// Update the layer of the character116void SetLayer(ObjectLayer inLayer, bool inLockBodies = true);117118/// Switch the shape of the character (e.g. for stance). When inMaxPenetrationDepth is not FLT_MAX, it checks119/// if the new shape collides before switching shape. Returns true if the switch succeeded.120bool SetShape(const Shape *inShape, float inMaxPenetrationDepth, bool inLockBodies = true);121122/// Get the transformed shape that represents the volume of the character, can be used for collision checks.123TransformedShape GetTransformedShape(bool inLockBodies = true) const;124125/// @brief Get all contacts for the character at a particular location126/// @param inPosition Position to test.127/// @param inRotation Rotation at which to test the shape.128/// @param inMovementDirection A hint in which direction the character is moving, will be used to calculate a proper normal.129/// @param inMaxSeparationDistance How much distance around the character you want to report contacts in (can be 0 to match the character exactly).130/// @param inShape Shape to test collision with.131/// @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 origin132/// @param ioCollector Collision collector that receives the collision results.133/// @param inLockBodies If the collision query should use the locking body interface (true) or the non locking body interface (false)134void CheckCollision(RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inMovementDirection, float inMaxSeparationDistance, const Shape *inShape, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, bool inLockBodies = true) const;135136private:137/// Check collisions between inShape and the world using the center of mass transform138void CheckCollision(RMat44Arg inCenterOfMassTransform, Vec3Arg inMovementDirection, float inMaxSeparationDistance, const Shape *inShape, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, bool inLockBodies) const;139140/// Check collisions between inShape and the world using the current position / rotation of the character141void CheckCollision(const Shape *inShape, float inMaxSeparationDistance, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, bool inLockBodies) const;142143/// The body of this character144BodyID mBodyID;145146/// The layer the body is in147ObjectLayer mLayer;148};149150JPH_NAMESPACE_END151152153