Avara3D 0.2.0
C++ API reference
PhysicsBody.h
1//
2// PhysicsBody.h
3// avara3d
4//
5// Created by Morgan Davis on 1/26/18.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_PHYSICS_PHYSICSBODY_H
10#define AVARA3D_PHYSICS_PHYSICSBODY_H
11
12#include <memory>
13
14#include "a3d/Math.h"
15
16namespace a3d {
17
18 class Mesh;
19 class Node;
20 class PhysicsWorld;
21 class PhysicsBodyProxy;
22 class PhysicsShapeProxy;
23 class PhysicsShape;
24 class Scene;
25
40
41 public:
42 // [Public Types]
43
45 enum class Type : uint8_t {
46 Static,
47 Dynamic,
48 Kinematic
49 };
50
52 enum class CenterOfMassCalculation : uint8_t {
53 BoundsCenter,
54 // VolumeCentroid
55 };
56
57 // [Public Static Member Functions]
58
60 static std::unique_ptr<PhysicsBody> StaticBody();
61
70 static std::unique_ptr<PhysicsBody> StaticBody(const std::shared_ptr<PhysicsShape>& shape);
71
73 static std::unique_ptr<PhysicsBody> DynamicBody();
74
83 static std::unique_ptr<PhysicsBody> DynamicBody(const std::shared_ptr<PhysicsShape>& shape);
84
86 static std::unique_ptr<PhysicsBody> KinematicBody();
87
96 static std::unique_ptr<PhysicsBody> KinematicBody(const std::shared_ptr<PhysicsShape>& shape);
97
98 // [Public Lifecycle Functions]
99
102
111 PhysicsBody(Type type, const std::shared_ptr<PhysicsShape>& shape);
112
113 PhysicsBody(const PhysicsBody&) = delete;
114 PhysicsBody& operator=(const PhysicsBody&) = delete;
115
116 PhysicsBody(PhysicsBody&&) = delete;
117 PhysicsBody& operator=(PhysicsBody&&) = delete;
118
119 ~PhysicsBody();
120
121 // [Public Member Functions]
122
124 Type type() const;
125
134
136 const std::shared_ptr<PhysicsShape>& shape() const;
137
149 void shape(const std::shared_ptr<PhysicsShape>& shape);
150
152 float mass() const;
153
160 void mass(float mass);
161
164
166 void momentOfInertia(const math::vec3& moment);
167
170
176 void centerOfMass(const math::vec3& offset);
177
179 float friction() const;
180
182 void friction(float friction);
183
185 float rollingFriction() const;
186
189
191 float spinningFriction() const;
192
195
197 float restitution() const;
198
201
204
206 void linearVelocity(const math::vec3& velocity);
207
210
212 void angularVelocity(const math::vec3& velocity);
213
216
218 void linearFactor(const math::vec3& factor);
219
222
224 void angularFactor(const math::vec3& factor);
225
227 float linearDamping() const;
228
230 void linearDamping(float damping);
231
233 float angularDamping() const;
234
236 void angularDamping(float damping);
237
240
242 void linearSleepingThreshold(float threshold);
243
246
248 void angularSleepingThreshold(float threshold);
249
257 void applyForce(const math::vec3& force, bool impulse);
258
267 void applyForce(const math::vec3& force, const math::vec3& location, bool impulse);
268
276 void applyTorque(const math::vec3& torque, bool impulse);
277
280
283
286
288 void ccdEnabled(bool enabled);
289
291 bool ccdEnabled() const;
292
294 void ccdMotionThreshold(float distance);
295
297 float ccdMotionThreshold() const;
298
300 void ccdSweptSphereRadius(float radius);
301
303 float ccdSweptSphereRadius() const;
304
306 bool affectedByGravity() const;
307
310
312 bool allowsResting() const;
313
322
324 bool resting() const;
325
327 void resting(bool resting);
328
331
340 void autocalculatesCenterOfMass(bool autocalculate);
341
344
353
356
358 void autocalculatesMomentOfInertia(bool autocalculate);
359
360 // [Internal Member Functions]
361
362 void attachedToNode(const std::shared_ptr<Node>& node);
363 void detachedFromNode(const std::shared_ptr<Node>& node);
364
365 void meshDetachedFromNode(const std::shared_ptr<Node>& node, const std::shared_ptr<Mesh>& mesh);
366 void meshAttachedToNode(const std::shared_ptr<Node>& node, const std::shared_ptr<Mesh>& mesh);
367
368 void meshWillChangeOnNode(const std::shared_ptr<Node>& node, const std::shared_ptr<Mesh>& oldMesh);
369 void meshDidChangeOnNode(const std::shared_ptr<Node>& node, const std::shared_ptr<Mesh>& newMesh);
370
371 void physicsWorldReachable(PhysicsWorld& world);
372 void physicsWorldUnreachable(PhysicsWorld& world);
373
374 void addedToWorld(PhysicsWorld& world);
375 void removedFromWorld(PhysicsWorld& world);
376
377 void shapeWillUpdate();
378 void shapeDidUpdate();
379
380 void syncTransformFromNode();
381
382 std::weak_ptr<Node> node() const;
383
384 // the scene's world, if it exists. not the same as _world.
385 PhysicsWorld* physicsWorld() const;
386
387 PhysicsBodyProxy* proxy() const;
388
389 private:
390 // [Private Member Functions]
391
392 void checkAutocreateShape(const std::shared_ptr<Node>& node);
393 void checkAutocreateShape(const std::shared_ptr<Mesh>& mesh);
394
395 void checkAddToWorld();
396
397 // [Private Member Variables]
398
399 std::shared_ptr<PhysicsShape> _shape;
400 bool _shapeAutocreated;
401 std::unique_ptr<PhysicsBodyProxy> _proxy;
402 std::weak_ptr<Node> _node;
403 // either a pointer to the world we are currently in or null.
404 PhysicsWorld* _world;
405 };
406
407}
408
409#endif // AVARA3D_PHYSICS_PHYSICSBODY_H
Rigid-body simulation state attached to a Node.
Definition: PhysicsBody.h:39
void autocalculatesMomentOfInertia(bool autocalculate)
Enables or disables automatic moment-of-inertia calculation.
static std::unique_ptr< PhysicsBody > KinematicBody(const std::shared_ptr< PhysicsShape > &shape)
Creates a kinematic body using shape.
void linearDamping(float damping)
Sets the linear damping coefficient.
bool allowsResting() const
Returns whether the body is allowed to enter the resting state automatically.
PhysicsBody(Type type, const std::shared_ptr< PhysicsShape > &shape)
Creates a body of type using shape.
float linearDamping() const
Returns the linear damping coefficient.
void ccdMotionThreshold(float distance)
Sets the motion-distance threshold used by continuous collision detection; negative values become zer...
bool resting() const
Returns whether the body is currently resting.
void applyTorque(const math::vec3 &torque, bool impulse)
Applies torque or a torque impulse to a dynamic body.
void centerOfMassCalculation(CenterOfMassCalculation calculation)
Sets the method used for automatic center-of-mass calculation.
void resting(bool resting)
Forces the body into or out of the resting state.
void allowsResting(bool allowsResting)
Sets whether the body may enter the resting state automatically.
Type type() const
Returns the body type.
float spinningFriction() const
Returns the spinning-friction coefficient.
bool autocalculatesMomentOfInertia() const
Returns whether the moment of inertia is calculated automatically from mass and collision shape.
void linearSleepingThreshold(float threshold)
Sets the linear-speed threshold used when deciding whether the body may rest.
void restitution(float restitution)
Sets the collision restitution coefficient.
math::vec3 angularVelocity() const
Returns the body angular velocity in radians per second.
void autocalculatesCenterOfMass(bool autocalculate)
Enables or disables automatic center-of-mass calculation.
void ccdEnabled(bool enabled)
Enables or disables continuous collision detection for this body.
void mass(float mass)
Sets the mass of a dynamic body.
bool ccdEnabled() const
Returns whether continuous collision detection is enabled.
CenterOfMassCalculation centerOfMassCalculation() const
Returns the method used for automatic center-of-mass calculation.
math::vec3 angularFactor() const
Returns the per-axis factor applied to angular motion.
math::vec3 momentOfInertia() const
Returns the body's local principal-axis moment of inertia.
static std::unique_ptr< PhysicsBody > KinematicBody()
Creates a shape-less kinematic body.
float linearSleepingThreshold() const
Returns the linear-speed threshold used when deciding whether the body may rest.
math::vec3 totalForce() const
Returns the accumulated force for the current simulation step.
void clearForces()
Clears accumulated forces and torques without changing the body's velocities.
void friction(float friction)
Sets the sliding-friction coefficient.
PhysicsBody(Type type)
Creates a shape-less body of type.
math::vec3 centerOfMass() const
Returns the center-of-mass offset from the owning node's origin in local coordinates.
bool affectedByGravity() const
Returns whether this body is affected by its PhysicsWorld gravity.
void angularVelocity(const math::vec3 &velocity)
Sets the body angular velocity in radians per second.
void linearFactor(const math::vec3 &factor)
Sets the per-axis factor applied to linear motion.
float restitution() const
Returns the collision restitution coefficient.
Type
Determines how a rigid body participates in simulation.
Definition: PhysicsBody.h:45
float ccdMotionThreshold() const
Returns the configured continuous-collision motion threshold.
math::vec3 totalTorque() const
Returns the accumulated torque for the current simulation step.
static std::unique_ptr< PhysicsBody > StaticBody()
Creates a shape-less static body.
void momentOfInertia(const math::vec3 &moment)
Sets the local principal-axis moment of inertia when automatic calculation is disabled.
float ccdSweptSphereRadius() const
Returns the configured continuous-collision swept-sphere radius.
void linearVelocity(const math::vec3 &velocity)
Sets the body linear velocity in world coordinates.
void spinningFriction(float friction)
Sets the spinning-friction coefficient.
void shape(const std::shared_ptr< PhysicsShape > &shape)
Replaces the collision shape retained by this body; nullptr removes it.
void rollingFriction(float friction)
Sets the rolling-friction coefficient.
void centerOfMass(const math::vec3 &offset)
Sets a dynamic body's local center-of-mass offset and disables automatic calculation.
CenterOfMassCalculation
Selects the method used to automatically determine a dynamic body's center of mass.
Definition: PhysicsBody.h:52
void applyForce(const math::vec3 &force, const math::vec3 &location, bool impulse)
Applies a force or impulse at a world-space position on a dynamic body.
void ccdSweptSphereRadius(float radius)
Sets the swept-sphere radius used by continuous collision detection; negative values become zero.
void applyForce(const math::vec3 &force, bool impulse)
Applies a central force or impulse to a dynamic body.
void angularSleepingThreshold(float threshold)
Sets the angular-speed threshold used when deciding whether the body may rest.
static std::unique_ptr< PhysicsBody > DynamicBody(const std::shared_ptr< PhysicsShape > &shape)
Creates a dynamic body using shape.
float friction() const
Returns the sliding-friction coefficient.
const std::shared_ptr< PhysicsShape > & shape() const
Returns the collision shape retained by this body, or nullptr if no shape is assigned.
static std::unique_ptr< PhysicsBody > DynamicBody()
Creates a shape-less dynamic body.
bool autocalculatesCenterOfMass() const
Returns whether the center of mass is calculated automatically.
float mass() const
Returns the body mass.
void angularDamping(float damping)
Sets the angular damping coefficient.
float rollingFriction() const
Returns the rolling-friction coefficient.
float angularSleepingThreshold() const
Returns the angular-speed threshold used when deciding whether the body may rest.
void type(Type type)
Validates the requested body type.
void affectedByGravity(bool affectedByGravity)
Sets whether this body is affected by its PhysicsWorld gravity.
void angularFactor(const math::vec3 &factor)
Sets the per-axis factor applied to angular motion.
math::vec3 linearVelocity() const
Returns the body linear velocity in world coordinates.
float angularDamping() const
Returns the angular damping coefficient.
static std::unique_ptr< PhysicsBody > StaticBody(const std::shared_ptr< PhysicsShape > &shape)
Creates a static body using shape.
math::vec3 linearFactor() const
Returns the per-axis factor applied to linear motion.
Simulates rigid bodies and performs physics collision queries for a Scene.
Definition: PhysicsWorld.h:39