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
21732 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. Note that you need to add a body to the physics system before you can remove it.
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. Note that bodies need to be added to the physics system before they can be removed.
136
/// ioBodies may be shuffled around by this function.
137
void RemoveBodies(BodyID *ioBodies, int inNumber);
138
///@}
139
140
///@name Activate / deactivate a body. Note that you need to add a body to the physics system before you can activate it.
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
/// Note that the bodies involved in the constraint should be added to the physics system before activating a constraint.
156
void ActivateConstraint(const TwoBodyConstraint *inConstraint);
157
158
///@name Access to the shape of a body
159
///@{
160
161
/// Get the current shape
162
RefConst<Shape> GetShape(const BodyID &inBodyID) const;
163
164
/// Set a new shape on the body
165
/// @param inBodyID Body ID of body that had its shape changed
166
/// @param inShape The new shape
167
/// @param inUpdateMassProperties When true, the mass and inertia tensor is recalculated
168
/// @param inActivationMode Whether or not to activate the body
169
void SetShape(const BodyID &inBodyID, const Shape *inShape, bool inUpdateMassProperties, EActivation inActivationMode) const;
170
171
/// Notify all systems to indicate that a shape has changed (usable for MutableCompoundShapes)
172
/// @param inBodyID Body ID of body that had its shape changed
173
/// @param inPreviousCenterOfMass Center of mass of the shape before the alterations
174
/// @param inUpdateMassProperties When true, the mass and inertia tensor is recalculated
175
/// @param inActivationMode Whether or not to activate the body
176
void NotifyShapeChanged(const BodyID &inBodyID, Vec3Arg inPreviousCenterOfMass, bool inUpdateMassProperties, EActivation inActivationMode) const;
177
///@}
178
179
///@name Object layer of a body
180
///@{
181
void SetObjectLayer(const BodyID &inBodyID, ObjectLayer inLayer);
182
ObjectLayer GetObjectLayer(const BodyID &inBodyID) const;
183
///@}
184
185
///@name Position and rotation of a body
186
///@{
187
void SetPositionAndRotation(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, EActivation inActivationMode);
188
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.
189
void GetPositionAndRotation(const BodyID &inBodyID, RVec3 &outPosition, Quat &outRotation) const;
190
void SetPosition(const BodyID &inBodyID, RVec3Arg inPosition, EActivation inActivationMode);
191
RVec3 GetPosition(const BodyID &inBodyID) const;
192
RVec3 GetCenterOfMassPosition(const BodyID &inBodyID) const;
193
void SetRotation(const BodyID &inBodyID, QuatArg inRotation, EActivation inActivationMode);
194
Quat GetRotation(const BodyID &inBodyID) const;
195
RMat44 GetWorldTransform(const BodyID &inBodyID) const;
196
RMat44 GetCenterOfMassTransform(const BodyID &inBodyID) const;
197
///@}
198
199
/// Set velocity of body such that it will be positioned at inTargetPosition/Rotation in inDeltaTime seconds (will activate body if needed)
200
void MoveKinematic(const BodyID &inBodyID, RVec3Arg inTargetPosition, QuatArg inTargetRotation, float inDeltaTime);
201
202
/// Linear or angular velocity (functions will activate body if needed).
203
/// 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$
204
void SetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity);
205
void GetLinearAndAngularVelocity(const BodyID &inBodyID, Vec3 &outLinearVelocity, Vec3 &outAngularVelocity) const;
206
void SetLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity);
207
Vec3 GetLinearVelocity(const BodyID &inBodyID) const;
208
void AddLinearVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity); ///< Add velocity to current velocity
209
void AddLinearAndAngularVelocity(const BodyID &inBodyID, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity); ///< Add linear and angular to current velocities
210
void SetAngularVelocity(const BodyID &inBodyID, Vec3Arg inAngularVelocity);
211
Vec3 GetAngularVelocity(const BodyID &inBodyID) const;
212
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
213
214
/// Set the complete motion state of a body.
215
/// 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$
216
void SetPositionRotationAndVelocity(const BodyID &inBodyID, RVec3Arg inPosition, QuatArg inRotation, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity);
217
218
///@name Add forces to the body. Note that you should add a body to the physics system before applying forces or torques.
219
///@{
220
void AddForce(const BodyID &inBodyID, Vec3Arg inForce, EActivation inActivationMode = EActivation::Activate); ///< See Body::AddForce
221
void AddForce(const BodyID &inBodyID, Vec3Arg inForce, RVec3Arg inPoint, EActivation inActivationMode = EActivation::Activate); ///< Applied at inPoint
222
void AddTorque(const BodyID &inBodyID, Vec3Arg inTorque, EActivation inActivationMode = EActivation::Activate); ///< See Body::AddTorque
223
void AddForceAndTorque(const BodyID &inBodyID, Vec3Arg inForce, Vec3Arg inTorque, EActivation inActivationMode = EActivation::Activate); ///< A combination of Body::AddForce and Body::AddTorque
224
///@}
225
226
///@name Add an impulse to the body. Note that you should add a body to the physics system before applying impulses.
227
///@{
228
void AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse); ///< Applied at center of mass
229
void AddImpulse(const BodyID &inBodyID, Vec3Arg inImpulse, RVec3Arg inPoint); ///< Applied at inPoint
230
void AddAngularImpulse(const BodyID &inBodyID, Vec3Arg inAngularImpulse);
231
bool ApplyBuoyancyImpulse(const BodyID &inBodyID, RVec3Arg inSurfacePosition, Vec3Arg inSurfaceNormal, float inBuoyancy, float inLinearDrag, float inAngularDrag, Vec3Arg inFluidVelocity, Vec3Arg inGravity, float inDeltaTime);
232
///@}
233
234
///@name Body type
235
///@{
236
EBodyType GetBodyType(const BodyID &inBodyID) const;
237
///@}
238
239
///@name Body motion type
240
///@{
241
void SetMotionType(const BodyID &inBodyID, EMotionType inMotionType, EActivation inActivationMode);
242
EMotionType GetMotionType(const BodyID &inBodyID) const;
243
///@}
244
245
///@name Body motion quality
246
///@{
247
void SetMotionQuality(const BodyID &inBodyID, EMotionQuality inMotionQuality);
248
EMotionQuality GetMotionQuality(const BodyID &inBodyID) const;
249
///@}
250
251
/// Get inverse inertia tensor in world space
252
Mat44 GetInverseInertia(const BodyID &inBodyID) const;
253
254
///@name Restitution
255
///@{
256
void SetRestitution(const BodyID &inBodyID, float inRestitution);
257
float GetRestitution(const BodyID &inBodyID) const;
258
///@}
259
260
///@name Friction
261
///@{
262
void SetFriction(const BodyID &inBodyID, float inFriction);
263
float GetFriction(const BodyID &inBodyID) const;
264
///@}
265
266
///@name Gravity factor
267
///@{
268
void SetGravityFactor(const BodyID &inBodyID, float inGravityFactor);
269
float GetGravityFactor(const BodyID &inBodyID) const;
270
///@}
271
272
///@name Max linear velocity
273
///@{
274
void SetMaxLinearVelocity(const BodyID &inBodyID, float inLinearVelocity);
275
float GetMaxLinearVelocity(const BodyID &inBodyID) const;
276
///@}
277
278
///@name Max angular velocity
279
///@{
280
void SetMaxAngularVelocity(const BodyID &inBodyID, float inAngularVelocity);
281
float GetMaxAngularVelocity(const BodyID &inBodyID) const;
282
///@}
283
284
///@name Manifold reduction
285
///@{
286
void SetUseManifoldReduction(const BodyID &inBodyID, bool inUseReduction);
287
bool GetUseManifoldReduction(const BodyID &inBodyID) const;
288
///@}
289
290
///@name Sensor
291
///@{
292
void SetIsSensor(const BodyID &inBodyID, bool inIsSensor);
293
bool IsSensor(const BodyID &inBodyID) const;
294
///@}
295
296
///@name Collision group
297
///@{
298
void SetCollisionGroup(const BodyID &inBodyID, const CollisionGroup &inCollisionGroup);
299
const CollisionGroup & GetCollisionGroup(const BodyID &inBodyID) const;
300
///@}
301
302
/// Get transform and shape for this body, used to perform collision detection
303
TransformedShape GetTransformedShape(const BodyID &inBodyID) const;
304
305
/// Get the user data for a body
306
uint64 GetUserData(const BodyID &inBodyID) const;
307
void SetUserData(const BodyID &inBodyID, uint64 inUserData) const;
308
309
/// Get the material for a particular sub shape
310
const PhysicsMaterial * GetMaterial(const BodyID &inBodyID, const SubShapeID &inSubShapeID) const;
311
312
/// 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.
313
void InvalidateContactCache(const BodyID &inBodyID);
314
315
private:
316
/// Helper function to activate a single body
317
JPH_INLINE void ActivateBodyInternal(Body &ioBody) const;
318
319
BodyLockInterface * mBodyLockInterface = nullptr;
320
BodyManager * mBodyManager = nullptr;
321
BroadPhase * mBroadPhase = nullptr;
322
};
323
324
JPH_NAMESPACE_END
325
326