Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Body/BodyInterface.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/Body/BodyID.h>
8
#include <Jolt/Physics/EActivation.h>
9
#include <Jolt/Physics/Collision/ObjectLayer.h>
10
#include <Jolt/Physics/Body/MotionType.h>
11
#include <Jolt/Physics/Body/MotionQuality.h>
12
#include <Jolt/Physics/Body/BodyType.h>
13
#include <Jolt/Core/Reference.h>
14
15
JPH_NAMESPACE_BEGIN
16
17
class Body;
18
class BodyCreationSettings;
19
class SoftBodyCreationSettings;
20
class BodyLockInterface;
21
class BroadPhase;
22
class BodyManager;
23
class TransformedShape;
24
class PhysicsMaterial;
25
class SubShapeID;
26
class Shape;
27
class TwoBodyConstraintSettings;
28
class TwoBodyConstraint;
29
class BroadPhaseLayerFilter;
30
class AABox;
31
class CollisionGroup;
32
33
/// Class that provides operations on bodies using a body ID. Note that if you need to do multiple operations on a single body, it is more efficient to lock the body once and combine the operations.
34
/// All quantities are in world space unless otherwise specified.
35
class JPH_EXPORT BodyInterface : public NonCopyable
36
{
37
public:
38
/// Initialize the interface (should only be called by PhysicsSystem)
39
void Init(BodyLockInterface &inBodyLockInterface, BodyManager &inBodyManager, BroadPhase &inBroadPhase) { mBodyLockInterface = &inBodyLockInterface; mBodyManager = &inBodyManager; mBroadPhase = &inBroadPhase; }
40
41
/// Create a rigid body
42
/// @return Created body or null when out of bodies
43
Body * CreateBody(const BodyCreationSettings &inSettings);
44
45
/// Create a soft body
46
/// @return Created body or null when out of bodies
47
Body * CreateSoftBody(const SoftBodyCreationSettings &inSettings);
48
49
/// Create a rigid body with specified ID. This function can be used if a simulation is to run in sync between clients or if a simulation needs to be restored exactly.
50
/// The ID created on the server can be replicated to the client and used to create a deterministic simulation.
51
/// @return Created body or null when the body ID is invalid or a body of the same ID already exists.
52
Body * CreateBodyWithID(const BodyID &inBodyID, const BodyCreationSettings &inSettings);
53
54
/// Create a soft body with specified ID. See comments at CreateBodyWithID.
55
Body * CreateSoftBodyWithID(const BodyID &inBodyID, const SoftBodyCreationSettings &inSettings);
56
57
/// Advanced use only. Creates a rigid body without specifying an ID. This body cannot be added to the physics system until it has been assigned a body ID.
58
/// This can be used to decouple allocation from registering the body. A call to CreateBodyWithoutID followed by AssignBodyID is equivalent to calling CreateBodyWithID.
59
/// @return Created body
60
Body * CreateBodyWithoutID(const BodyCreationSettings &inSettings) const;
61
62
/// Advanced use only. Creates a body without specifying an ID. See comments at CreateBodyWithoutID.
63
Body * CreateSoftBodyWithoutID(const SoftBodyCreationSettings &inSettings) const;
64
65
/// Advanced use only. Destroy a body previously created with CreateBodyWithoutID that hasn't gotten an ID yet through the AssignBodyID function,
66
/// or a body that has had its body ID unassigned through UnassignBodyIDs. Bodies that have an ID should be destroyed through DestroyBody.
67
void DestroyBodyWithoutID(Body *inBody) const;
68
69
/// Advanced use only. Assigns the next available body ID to a body that was created using CreateBodyWithoutID. After this call, the body can be added to the physics system.
70
/// @return false if the body already has an ID or out of body ids.
71
bool AssignBodyID(Body *ioBody);
72
73
/// Advanced use only. Assigns a body ID to a body that was created using CreateBodyWithoutID. After this call, the body can be added to the physics system.
74
/// @return false if the body already has an ID or if the ID is not valid.
75
bool AssignBodyID(Body *ioBody, const BodyID &inBodyID);
76
77
/// Advanced use only. See UnassignBodyIDs. Unassigns the ID of a single body.
78
Body * UnassignBodyID(const BodyID &inBodyID);
79
80
/// Advanced use only. Removes a number of body IDs from their bodies and returns the body pointers. Before calling this, the body should have been removed from the physics system.
81
/// The body can be destroyed through DestroyBodyWithoutID. This can be used to decouple deallocation. A call to UnassignBodyIDs followed by calls to DestroyBodyWithoutID is equivalent to calling DestroyBodies.
82
/// @param inBodyIDs A list of body IDs
83
/// @param inNumber Number of bodies in the list
84
/// @param outBodies If not null on input, this will contain a list of body pointers corresponding to inBodyIDs that can be destroyed afterwards (caller assumes ownership over these).
85
void UnassignBodyIDs(const BodyID *inBodyIDs, int inNumber, Body **outBodies);
86
87
/// Destroy a body.
88
/// Make sure that you remove the body from the physics system using BodyInterface::RemoveBody before calling this function.
89
void DestroyBody(const BodyID &inBodyID);
90
91
/// Destroy multiple bodies
92
/// Make sure that you remove the bodies from the physics system using BodyInterface::RemoveBody before calling this function.
93
void DestroyBodies(const BodyID *inBodyIDs, int inNumber);
94
95
/// Add body to the physics system.
96
/// Note that if you need to add multiple bodies, use the AddBodiesPrepare/AddBodiesFinalize function.
97
/// Adding many bodies, one at a time, results in a really inefficient broadphase until PhysicsSystem::OptimizeBroadPhase is called or when PhysicsSystem::Update rebuilds the tree!
98
/// After adding, to get a body by ID use the BodyLockRead or BodyLockWrite interface!
99
void AddBody(const BodyID &inBodyID, EActivation inActivationMode);
100
101
/// Remove body from the physics system.
102
void RemoveBody(const BodyID &inBodyID);
103
104
/// Check if a body has been added to the physics system.
105
bool IsAdded(const BodyID &inBodyID) const;
106
107
/// Combines CreateBody and AddBody
108
/// @return Created body ID or an invalid ID when out of bodies
109
BodyID CreateAndAddBody(const BodyCreationSettings &inSettings, EActivation inActivationMode);
110
111
/// Combines CreateSoftBody and AddBody
112
/// @return Created body ID or an invalid ID when out of bodies
113
BodyID CreateAndAddSoftBody(const SoftBodyCreationSettings &inSettings, EActivation inActivationMode);
114
115
/// Add state handle, used to keep track of a batch of bodies while adding them to the PhysicsSystem.
116
using AddState = void *;
117
118
///@name Batch adding interface
119
///@{
120
121
/// Prepare adding inNumber bodies at ioBodies to the PhysicsSystem, returns a handle that should be used in AddBodiesFinalize/Abort.
122
/// This can be done on a background thread without influencing the PhysicsSystem.
123
/// ioBodies may be shuffled around by this function and should be kept that way until AddBodiesFinalize/Abort is called.
124
AddState AddBodiesPrepare(BodyID *ioBodies, int inNumber);
125
126
/// Finalize adding bodies to the PhysicsSystem, supply the return value of AddBodiesPrepare in inAddState.
127
/// Please ensure that the ioBodies array passed to AddBodiesPrepare is unmodified and passed again to this function.
128
void AddBodiesFinalize(BodyID *ioBodies, int inNumber, AddState inAddState, EActivation inActivationMode);
129
130
/// Abort adding bodies to the PhysicsSystem, supply the return value of AddBodiesPrepare in inAddState.
131
/// This can be done on a background thread without influencing the PhysicsSystem.
132
/// Please ensure that the ioBodies array passed to AddBodiesPrepare is unmodified and passed again to this function.
133
void AddBodiesAbort(BodyID *ioBodies, int inNumber, AddState inAddState);
134
135
/// Remove inNumber bodies in ioBodies from the PhysicsSystem.
136
/// ioBodies may be shuffled around by this function.
137
void RemoveBodies(BodyID *ioBodies, int inNumber);
138
///@}
139
140
///@name Activate / deactivate a body
141
///@{
142
void ActivateBody(const BodyID &inBodyID);
143
void ActivateBodies(const BodyID *inBodyIDs, int inNumber);
144
void ActivateBodiesInAABox(const AABox &inBox, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter);
145
void DeactivateBody(const BodyID &inBodyID);
146
void DeactivateBodies(const BodyID *inBodyIDs, int inNumber);
147
bool IsActive(const BodyID &inBodyID) const;
148
void ResetSleepTimer(const BodyID &inBodyID);
149
///@}
150
151
/// Create a two body constraint
152
TwoBodyConstraint * CreateConstraint(const TwoBodyConstraintSettings *inSettings, const BodyID &inBodyID1, const BodyID &inBodyID2);
153
154
/// Activate non-static bodies attached to a constraint
155
void ActivateConstraint(const TwoBodyConstraint *inConstraint);
156
157
///@name Access to the shape of a body
158
///@{
159
160
/// Get the current shape
161
RefConst<Shape> GetShape(const BodyID &inBodyID) const;
162
163
/// Set a new shape on the body
164
/// @param inBodyID Body ID of body that had its shape changed
165
/// @param inShape The new shape
166
/// @param inUpdateMassProperties When true, the mass and inertia tensor is recalculated
167
/// @param inActivationMode Whether or not to activate the body
168
void SetShape(const BodyID &inBodyID, const Shape *inShape, bool inUpdateMassProperties, EActivation inActivationMode) const;
169
170
/// Notify all systems to indicate that a shape has changed (usable for MutableCompoundShapes)
171
/// @param inBodyID Body ID of body that had its shape changed
172
/// @param inPreviousCenterOfMass Center of mass of the shape before the alterations
173
/// @param inUpdateMassProperties When true, the mass and inertia tensor is recalculated
174
/// @param inActivationMode Whether or not to activate the body
175
void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inPreviousCenterOfMass, bool inUpdateMassProperties, EActivation inActivationMode) const;
176
///@}
177
178
///@name Object layer of a body
179
///@{
180
void SetObjectLayer(const BodyID &inBodyID, ObjectLayer inLayer);
181
ObjectLayer GetObjectLayer(const BodyID &inBodyID) const;
182
///@}
183
184
///@name Position and rotation of a body
185
///@{
186
void SetPositionAndRotation(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode);
187
void SetPositionAndRotationWhenChanged(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode); ///< Will only update the position/rotation and activate the body when the difference is larger than a very small number. This avoids updating the broadphase/waking up a body when the resulting position/orientation doesn't really change.
188
void GetPositionAndRotation(const BodyID &inBodyID, RVec3 &outPosition, Quat &outRotation) const;
189
void SetPosition(const BodyID &inBodyID, RVec3Arg inPosition, EActivation inActivationMode);
190
RVec3 GetPosition(const BodyID &inBodyID) const;
191
RVec3 GetCenterOfMassPosition(const BodyID &inBodyID) const;
192
void SetRotation(const BodyID &inBodyID, QuatArg inRotation, EActivation inActivationMode);
193
Quat GetRotation(const BodyID &inBodyID) const;
194
RMat44 GetWorldTransform(const BodyID &inBodyID) const;
195
RMat44 GetCenterOfMassTransform(const BodyID &inBodyID) const;
196
///@}
197
198
/// Set velocity of body such that it will be positioned at inTargetPosition/Rotation in inDeltaTime seconds (will activate body if needed)
199
void MoveKinematic(const BodyID &inBodyID, RVec3Arg inTargetPosition, QuatArg inTargetRotation, float inDeltaTime);
200
201
/// Linear or angular velocity (functions will activate body if needed).
202
/// Note that the linear velocity is the velocity of the center of mass, which may not coincide with the position of your object, to correct for this: \f$VelocityCOM = Velocity - AngularVelocity \times ShapeCOM\f$
203
void SetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity);
204
void GetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3 &outLinearVelocity, Vec3 &outAngularVelocity) const;
205
void SetLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity);
206
Vec3 GetLinearVelocity(const BodyID &inBodyID) const;
207
void AddLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity); ///< Add velocity to current velocity
208
void AddLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity); ///< Add linear and angular to current velocities
209
void SetAngularVelocity(const BodyID &inBodyID, Vec3Arg inAngularVelocity);
210
Vec3 GetAngularVelocity(const BodyID &inBodyID) const;
211
Vec3 GetPointVelocity(const BodyID &inBodyID, RVec3Arg inPoint) const; ///< Velocity of point inPoint (in world space, e.g. on the surface of the body) of the body
212
213
/// Set the complete motion state of a body.
214
/// Note that the linear velocity is the velocity of the center of mass, which may not coincide with the position of your object, to correct for this: \f$VelocityCOM = Velocity - AngularVelocity \times ShapeCOM\f$
215
void SetPositionRotationAndVelocity(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity);
216
217
///@name Add forces to the body
218
///@{
219
void AddForce(const BodyID &inBodyID, Vec3Arg inForce, EActivation inActivationMode = EActivation::Activate); ///< See Body::AddForce
220
void AddForce(const BodyID &inBodyID, Vec3Arg inForce, RVec3Arg inPoint, EActivation inActivationMode = EActivation::Activate); ///< Applied at inPoint
221
void AddTorque(const BodyID &inBodyID, Vec3Arg inTorque, EActivation inActivationMode = EActivation::Activate); ///< See Body::AddTorque
222
void AddForceAndTorque(const BodyID &inBodyID, Vec3Arg inForce, Vec3Arg inTorque, EActivation inActivationMode = EActivation::Activate); ///< A combination of Body::AddForce and Body::AddTorque
223
///@}
224
225
///@name Add an impulse to the body
226
///@{
227
void AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse); ///< Applied at center of mass
228
void AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse, RVec3Arg inPoint); ///< Applied at inPoint
229
void AddAngularImpulse(const BodyID &inBodyID, Vec3Arg inAngularImpulse);
230
bool ApplyBuoyancyImpulse(const BodyID &inBodyID, RVec3Arg inSurfacePosition, Vec3Arg inSurfaceNormal, float inBuoyancy, float inLinearDrag, float inAngularDrag, Vec3Arg inFluidVelocity, Vec3Arg inGravity, float inDeltaTime);
231
///@}
232
233
///@name Body type
234
///@{
235
EBodyType GetBodyType(const BodyID &inBodyID) const;
236
///@}
237
238
///@name Body motion type
239
///@{
240
void SetMotionType(const BodyID &inBodyID, EMotionType inMotionType, EActivation inActivationMode);
241
EMotionType GetMotionType(const BodyID &inBodyID) const;
242
///@}
243
244
///@name Body motion quality
245
///@{
246
void SetMotionQuality(const BodyID &inBodyID, EMotionQuality inMotionQuality);
247
EMotionQuality GetMotionQuality(const BodyID &inBodyID) const;
248
///@}
249
250
/// Get inverse inertia tensor in world space
251
Mat44 GetInverseInertia(const BodyID &inBodyID) const;
252
253
///@name Restitution
254
///@{
255
void SetRestitution(const BodyID &inBodyID, float inRestitution);
256
float GetRestitution(const BodyID &inBodyID) const;
257
///@}
258
259
///@name Friction
260
///@{
261
void SetFriction(const BodyID &inBodyID, float inFriction);
262
float GetFriction(const BodyID &inBodyID) const;
263
///@}
264
265
///@name Gravity factor
266
///@{
267
void SetGravityFactor(const BodyID &inBodyID, float inGravityFactor);
268
float GetGravityFactor(const BodyID &inBodyID) const;
269
///@}
270
271
///@name Manifold reduction
272
///@{
273
void SetUseManifoldReduction(const BodyID &inBodyID, bool inUseReduction);
274
bool GetUseManifoldReduction(const BodyID &inBodyID) const;
275
///@}
276
277
///@name Collision group
278
///@{
279
void SetCollisionGroup(const BodyID &inBodyID, const CollisionGroup &inCollisionGroup);
280
const CollisionGroup & GetCollisionGroup(const BodyID &inBodyID) const;
281
///@}
282
283
/// Get transform and shape for this body, used to perform collision detection
284
TransformedShape GetTransformedShape(const BodyID &inBodyID) const;
285
286
/// Get the user data for a body
287
uint64 GetUserData(const BodyID &inBodyID) const;
288
void SetUserData(const BodyID &inBodyID, uint64 inUserData) const;
289
290
/// Get the material for a particular sub shape
291
const PhysicsMaterial * GetMaterial(const BodyID &inBodyID, const SubShapeID &inSubShapeID) const;
292
293
/// Set the Body::EFlags::InvalidateContactCache flag for the specified body. This means that the collision cache is invalid for any body pair involving that body until the next physics step.
294
void InvalidateContactCache(const BodyID &inBodyID);
295
296
private:
297
/// Helper function to activate a single body
298
JPH_INLINE void ActivateBodyInternal(Body &ioBody) const;
299
300
BodyLockInterface * mBodyLockInterface = nullptr;
301
BodyManager * mBodyManager = nullptr;
302
BroadPhase * mBroadPhase = nullptr;
303
};
304
305
JPH_NAMESPACE_END
306
307