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