Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Character/Character.h
9912 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
#include <Jolt/Physics/Character/CharacterBase.h>
8
#include <Jolt/Physics/Collision/ObjectLayer.h>
9
#include <Jolt/Physics/Collision/TransformedShape.h>
10
#include <Jolt/Physics/EActivation.h>
11
#include <Jolt/Physics/Body/AllowedDOFs.h>
12
13
JPH_NAMESPACE_BEGIN
14
15
/// Contains the configuration of a character
16
class JPH_EXPORT CharacterSettings : public CharacterBaseSettings
17
{
18
public:
19
JPH_OVERRIDE_NEW_DELETE
20
21
/// Layer that this character will be added to
22
ObjectLayer mLayer = 0;
23
24
/// Mass of the character
25
float mMass = 80.0f;
26
27
/// Friction for the character
28
float mFriction = 0.2f;
29
30
/// Value to multiply gravity with for this character
31
float mGravityFactor = 1.0f;
32
33
/// Allowed degrees of freedom for this character
34
EAllowedDOFs mAllowedDOFs = EAllowedDOFs::TranslationX | EAllowedDOFs::TranslationY | EAllowedDOFs::TranslationZ;
35
};
36
37
/// Runtime character object.
38
/// This object usually represents the player or a humanoid AI. It uses a single rigid body,
39
/// usually with a capsule shape to simulate movement and collision for the character.
40
/// The character is a keyframed object, the application controls it by setting the velocity.
41
class JPH_EXPORT Character : public CharacterBase
42
{
43
public:
44
JPH_OVERRIDE_NEW_DELETE
45
46
/// Constructor
47
/// @param inSettings The settings for the character
48
/// @param inPosition Initial position for the character
49
/// @param inRotation Initial rotation for the character (usually only around Y)
50
/// @param inUserData Application specific value
51
/// @param inSystem Physics system that this character will be added to later
52
Character(const CharacterSettings *inSettings, RVec3Arg inPosition, QuatArg inRotation, uint64 inUserData, PhysicsSystem *inSystem);
53
54
/// Destructor
55
virtual ~Character() override;
56
57
/// Add bodies and constraints to the system and optionally activate the bodies
58
void AddToPhysicsSystem(EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true);
59
60
/// Remove bodies and constraints from the system
61
void RemoveFromPhysicsSystem(bool inLockBodies = true);
62
63
/// Wake up the character
64
void Activate(bool inLockBodies = true);
65
66
/// Needs to be called after every PhysicsSystem::Update
67
/// @param inMaxSeparationDistance Max distance between the floor and the character to still consider the character standing on the floor
68
/// @param inLockBodies If the collision query should use the locking body interface (true) or the non locking body interface (false)
69
void PostSimulation(float inMaxSeparationDistance, bool inLockBodies = true);
70
71
/// Control the velocity of the character
72
void SetLinearAndAngularVelocity(Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity, bool inLockBodies = true);
73
74
/// Get the linear velocity of the character (m / s)
75
Vec3 GetLinearVelocity(bool inLockBodies = true) const;
76
77
/// Set the linear velocity of the character (m / s)
78
void SetLinearVelocity(Vec3Arg inLinearVelocity, bool inLockBodies = true);
79
80
/// Add world space linear velocity to current velocity (m / s)
81
void AddLinearVelocity(Vec3Arg inLinearVelocity, bool inLockBodies = true);
82
83
/// Add impulse to the center of mass of the character
84
void AddImpulse(Vec3Arg inImpulse, bool inLockBodies = true);
85
86
/// Get the body associated with this character
87
BodyID GetBodyID() const { return mBodyID; }
88
89
/// Get position / rotation of the body
90
void GetPositionAndRotation(RVec3 &outPosition, Quat &outRotation, bool inLockBodies = true) const;
91
92
/// Set the position / rotation of the body, optionally activating it.
93
void SetPositionAndRotation(RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true) const;
94
95
/// Get the position of the character
96
RVec3 GetPosition(bool inLockBodies = true) const;
97
98
/// Set the position of the character, optionally activating it.
99
void SetPosition(RVec3Arg inPosition, EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true);
100
101
/// Get the rotation of the character
102
Quat GetRotation(bool inLockBodies = true) const;
103
104
/// Set the rotation of the character, optionally activating it.
105
void SetRotation(QuatArg inRotation, EActivation inActivationMode = EActivation::Activate, bool inLockBodies = true);
106
107
/// Position of the center of mass of the underlying rigid body
108
RVec3 GetCenterOfMassPosition(bool inLockBodies = true) const;
109
110
/// Calculate the world transform of the character
111
RMat44 GetWorldTransform(bool inLockBodies = true) const;
112
113
/// Get the layer of the character
114
ObjectLayer GetLayer() const { return mLayer; }
115
116
/// Update the layer of the character
117
void SetLayer(ObjectLayer inLayer, bool inLockBodies = true);
118
119
/// Switch the shape of the character (e.g. for stance). When inMaxPenetrationDepth is not FLT_MAX, it checks
120
/// if the new shape collides before switching shape. Returns true if the switch succeeded.
121
bool SetShape(const Shape *inShape, float inMaxPenetrationDepth, bool inLockBodies = true);
122
123
/// Get the transformed shape that represents the volume of the character, can be used for collision checks.
124
TransformedShape GetTransformedShape(bool inLockBodies = true) const;
125
126
/// @brief Get all contacts for the character at a particular location
127
/// @param inPosition Position to test.
128
/// @param inRotation Rotation at which to test the shape.
129
/// @param inMovementDirection A hint in which direction the character is moving, will be used to calculate a proper normal.
130
/// @param inMaxSeparationDistance How much distance around the character you want to report contacts in (can be 0 to match the character exactly).
131
/// @param inShape Shape to test collision with.
132
/// @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 origin
133
/// @param ioCollector Collision collector that receives the collision results.
134
/// @param inLockBodies If the collision query should use the locking body interface (true) or the non locking body interface (false)
135
void CheckCollision(RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inMovementDirection, float inMaxSeparationDistance, const Shape *inShape, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, bool inLockBodies = true) const;
136
137
private:
138
/// Check collisions between inShape and the world using the center of mass transform
139
void CheckCollision(RMat44Arg inCenterOfMassTransform, Vec3Arg inMovementDirection, float inMaxSeparationDistance, const Shape *inShape, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, bool inLockBodies) const;
140
141
/// Check collisions between inShape and the world using the current position / rotation of the character
142
void CheckCollision(const Shape *inShape, float inMaxSeparationDistance, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, bool inLockBodies) const;
143
144
/// The body of this character
145
BodyID mBodyID;
146
147
/// The layer the body is in
148
ObjectLayer mLayer;
149
};
150
151
JPH_NAMESPACE_END
152
153