Avara3D 0.2.0
C++ API reference
a3d::PhysicsWorld Class Reference

Simulates rigid bodies and performs physics collision queries for a Scene. More...

#include <a3d/physics/PhysicsWorld.h>

Classes

struct  ConvexSweepTestOptions
 Options controlling a convex sweep collision query. More...
 
struct  RayTestOptions
 Options controlling a ray collision query. More...
 

Public Types

using DidBeginContactCallback = std::function< void(PhysicsWorld &physicsWorld, const PhysicsContact &contact)>
 Callback invoked when a pair of physics bodies begins a logical contact. More...
 
using DidContinueContactCallback = std::function< void(PhysicsWorld &physicsWorld, const PhysicsContact &contact)>
 Callback invoked once per simulation step while a body pair remains in contact. More...
 
using DidEndContactCallback = std::function< void(PhysicsWorld &physicsWorld, const PhysicsContact &contact)>
 Callback invoked when a tracked body pair ceases to be in contact. More...
 

Public Member Functions

 PhysicsWorld ()
 Creates a PhysicsWorld with Earth-like gravity of (0, -9.807, 0). More...
 
const math::vec3gravity () const
 Returns the world gravity acceleration vector. More...
 
void gravity (const math::vec3 &gravity)
 Sets the world gravity and immediately applies it to affected dynamic bodies. More...
 
std::optional< PhysicsContactcontactTest (const PhysicsBody &bodyA, const PhysicsBody &bodyB) const
 Tests for contact between bodyA and bodyB. More...
 
std::vector< PhysicsContactcontactTest (const PhysicsBody &body) const
 Returns current contacts between body and other physics bodies. More...
 
std::vector< HitTestResultrayTest (const math::vec3 &from, const math::vec3 &to, const RayTestOptions &options) const
 Tests the world-space segment from from to to against physics bodies. More...
 
std::vector< HitTestResultrayTest (const math::vec3 &from, const math::vec3 &to) const
 Tests the world-space segment from from to to and returns the closest physics hit. More...
 
std::vector< PhysicsContactconvexSweepTest (const PhysicsShape &shape, const math::mat4 &fromMat, const math::mat4 &toMat, const ConvexSweepTestOptions &options) const
 Sweeps shape between two transforms. More...
 
std::vector< PhysicsContactconvexSweepTest (const PhysicsShape &shape, const math::mat4 &fromMat, const math::mat4 &toMat) const
 Sweeps shape between two transforms. More...
 
Scenescene () const
 Returns the Scene containing this PhysicsWorld, or nullptr when it is not installed in a Scene. More...
 
DidBeginContactCallback didBeginContactCallback () const
 Returns the callback invoked when a body pair begins contact. More...
 
void didBeginContactCallback (DidBeginContactCallback function)
 Replaces the begin-contact callback; an empty callback disables it. More...
 
DidContinueContactCallback didContinueContactCallback () const
 Returns the callback invoked while a body pair remains in contact. More...
 
void didContinueContactCallback (DidContinueContactCallback function)
 Replaces the continuing-contact callback; an empty callback disables it. More...
 
DidEndContactCallback didEndContactCallback () const
 Returns the callback invoked when a tracked body pair ends contact. More...
 
void didEndContactCallback (DidEndContactCallback function)
 Replaces the end-contact callback; an empty callback disables it. More...
 

Detailed Description

Simulates rigid bodies and performs physics collision queries for a Scene.

Physics bodies attached to nodes in a Scene become members of its PhysicsWorld when they have a usable collision shape. The world applies its gravity to participating dynamic bodies and reports logical contact lifecycles through optional callbacks.

Definition at line 39 of file PhysicsWorld.h.

Member Typedef Documentation

◆ DidBeginContactCallback

using a3d::PhysicsWorld::DidBeginContactCallback = std::function<void(PhysicsWorld& physicsWorld, const PhysicsContact& contact)>

Callback invoked when a pair of physics bodies begins a logical contact.

A body pair produces one logical contact lifecycle even when multiple underlying contact points exist.

Definition at line 62 of file PhysicsWorld.h.

◆ DidContinueContactCallback

using a3d::PhysicsWorld::DidContinueContactCallback = std::function<void(PhysicsWorld& physicsWorld, const PhysicsContact& contact)>

Callback invoked once per simulation step while a body pair remains in contact.

Ordering relative to callbacks for unrelated body pairs is unspecified.

Definition at line 70 of file PhysicsWorld.h.

◆ DidEndContactCallback

using a3d::PhysicsWorld::DidEndContactCallback = std::function<void(PhysicsWorld& physicsWorld, const PhysicsContact& contact)>

Callback invoked when a tracked body pair ceases to be in contact.

Removing a physics body silently terminates its tracked contacts and does not generate end-contact callbacks. Ordering relative to callbacks for unrelated body pairs is unspecified.

Definition at line 80 of file PhysicsWorld.h.

Constructor & Destructor Documentation

◆ PhysicsWorld()

a3d::PhysicsWorld::PhysicsWorld ( )

Creates a PhysicsWorld with Earth-like gravity of (0, -9.807, 0).

Member Function Documentation

◆ contactTest() [1/2]

std::vector< PhysicsContact > a3d::PhysicsWorld::contactTest ( const PhysicsBody body) const

Returns current contacts between body and other physics bodies.

At most one representative PhysicsContact is returned for each body pair.

◆ contactTest() [2/2]

std::optional< PhysicsContact > a3d::PhysicsWorld::contactTest ( const PhysicsBody bodyA,
const PhysicsBody bodyB 
) const

Tests for contact between bodyA and bodyB.

If the bodies have multiple contact points, the returned PhysicsContact represents the point with greatest collision impulse, using penetration depth as a tie-breaker.

Returns
The representative contact, or an empty optional if the bodies are not in contact.

◆ convexSweepTest() [1/2]

std::vector< PhysicsContact > a3d::PhysicsWorld::convexSweepTest ( const PhysicsShape shape,
const math::mat4 fromMat,
const math::mat4 toMat 
) const

Sweeps shape between two transforms.

Not currently implemented.

Exceptions
std::runtime_erroralways; convex sweep tests are not currently implemented.

◆ convexSweepTest() [2/2]

std::vector< PhysicsContact > a3d::PhysicsWorld::convexSweepTest ( const PhysicsShape shape,
const math::mat4 fromMat,
const math::mat4 toMat,
const ConvexSweepTestOptions options 
) const

Sweeps shape between two transforms.

Not currently implemented.

Exceptions
std::runtime_erroralways; convex sweep tests are not currently implemented.

◆ didBeginContactCallback() [1/2]

DidBeginContactCallback a3d::PhysicsWorld::didBeginContactCallback ( ) const

Returns the callback invoked when a body pair begins contact.

◆ didBeginContactCallback() [2/2]

void a3d::PhysicsWorld::didBeginContactCallback ( DidBeginContactCallback  function)

Replaces the begin-contact callback; an empty callback disables it.

◆ didContinueContactCallback() [1/2]

DidContinueContactCallback a3d::PhysicsWorld::didContinueContactCallback ( ) const

Returns the callback invoked while a body pair remains in contact.

◆ didContinueContactCallback() [2/2]

void a3d::PhysicsWorld::didContinueContactCallback ( DidContinueContactCallback  function)

Replaces the continuing-contact callback; an empty callback disables it.

◆ didEndContactCallback() [1/2]

DidEndContactCallback a3d::PhysicsWorld::didEndContactCallback ( ) const

Returns the callback invoked when a tracked body pair ends contact.

◆ didEndContactCallback() [2/2]

void a3d::PhysicsWorld::didEndContactCallback ( DidEndContactCallback  function)

Replaces the end-contact callback; an empty callback disables it.

◆ gravity() [1/2]

const math::vec3 & a3d::PhysicsWorld::gravity ( ) const

Returns the world gravity acceleration vector.

◆ gravity() [2/2]

void a3d::PhysicsWorld::gravity ( const math::vec3 gravity)

Sets the world gravity and immediately applies it to affected dynamic bodies.

◆ rayTest() [1/2]

std::vector< HitTestResult > a3d::PhysicsWorld::rayTest ( const math::vec3 from,
const math::vec3 to 
) const

Tests the world-space segment from from to to and returns the closest physics hit.

Returns
Zero or one HitTestResult.

◆ rayTest() [2/2]

std::vector< HitTestResult > a3d::PhysicsWorld::rayTest ( const math::vec3 from,
const math::vec3 to,
const RayTestOptions options 
) const

Tests the world-space segment from from to to against physics bodies.

Parameters
optionscontrols whether any, the closest, or all hits are returned.
Returns
Physics hit results selected according to options.

◆ scene()

Scene * a3d::PhysicsWorld::scene ( ) const

Returns the Scene containing this PhysicsWorld, or nullptr when it is not installed in a Scene.


The documentation for this class was generated from the following file: