ToyMaker Game Engine 0.0.2
ToyMaker is a game engine developed and maintained by Zoheb Shujauddin.
Loading...
Searching...
No Matches

The physics system, an ECS system that tracks and updates the state of each physics object in the scene according to its properties. More...

#include <system.hpp>

Inheritance diagram for ToyMaker::PhysicsSystem:
ToyMaker::System< PhysicsSystem, std::tuple< PhysicsState, ObjectBounds >, std::tuple<> > ToyMaker::SignalTracker

Classes

struct  PhysicsStateFull
union  CollisionEvent
struct  CollisionReport

Public Types

using ConstraintID = std::size_t
 ID naming a unique constraint registered with the physics system.
using SignalCollidedData = std::pair<CollisionPair, Collision>
 Struct containing data about a collision event.
using SignalSeparatedData = CollisionPair
 Struct containing data about a separation event.
using SignalCollided = std::shared_ptr<Signal<SignalCollidedData>>
 Signal for reporting collision start events.
using SignalSeparated = std::shared_ptr<Signal<SignalSeparatedData>>
 Signal for reporting the separation of two colliding bodies.

Public Member Functions

 PhysicsSystem (std::weak_ptr< ECSWorld > world)
void setSubsteps (uint8_t newSubsteps)
 Sets the number of substeps used for physics integration using XPBD.
uint8_t getSubsteps () const
 Gets the number of substeps used in the physics solver's XPBD implementation.
template<typename TConstraint, typename TConstraintData>
ConstraintID registerConstraint (const std::vector< std::pair< EntityID, TConstraintData > > &constraintData, float compliance)
 Registers a constraint for the physics system to evaluate every substep.
void unregisterConstraint (ConstraintID constraint)
 Removes a constraint.
Public Member Functions inherited from ToyMaker::SignalTracker
 SignalTracker ()
 Constructs a new SignalTracker object.
 SignalTracker (const SignalTracker &other)
 Constructs a new SignalTracker.
SignalTrackeroperator= (const SignalTracker &other)
 Copy assignment operator.
 SignalTracker (SignalTracker &&other)
 Moves resources from another SignalTracker into this one, invalidating them from the other.
SignalTrackeroperator= (SignalTracker &&other)
 Moves resources from another SignalTracker into this one, destroying this tracker's resources in the process.
void connect (const std::string &theirSignal, const std::string &ourObserver, SignalTracker &other)
 Method that connects one of this objects SignalObservers to another tracker's Signal.

Static Public Member Functions

static std::string getSystemTypeName ()
 The system type string for this class.

Static Public Attributes

static const std::string SignalCollidedPrefix { "collided_" }
 Prefix for the name under which collided Signals are advertised.
static const std::string SignalSeparatedPrefix { "separated_" }
 Prefix for the name under which separated Signals are advertised.

Private Member Functions

void onSimulationActivated () override
 Marks this system as requiring initialization on the nearest update.
void onSimulationStep (uint32_t timestepMillis) override
 Updates physics simulation.
void onEntityEnabled (EntityID entityID) override
 Updates this entity's physics properties.
void onEntityUpdated (EntityID entityID, ComponentType updatedComponent) override
 Updates this entity's physics properties.
void onEntityDisabled (EntityID entityID) override
 Removes entity from to-initialize list.
bool isConstraintActive (ConstraintID constraint) const
 Whether or not a particular constraint is active.
void updateProperties (EntityID entityID)
 Computes rotational inertia from mass and object bounds.
void refreshActiveConstraints ()
 Determines which constraints are inactive (by virtue of all of their entities being enabled) and which ones aren't.
void integrateForces (float substepSeconds, std::unordered_map< EntityID, PhysicsStateFull > &previousState, std::unordered_map< EntityID, PhysicsStateFull > &currentState)
 Derives position and rotation updates for physics objects based on their current state and forces acting on them.
void deriveVelocities (float substepSeconds, const std::unordered_map< EntityID, PhysicsStateFull > &previousState, std::unordered_map< EntityID, PhysicsStateFull > &currentState)
 Derives actual object velocities after integration and constraint solve.
void applyPositionCollisionConstraints (std::map< CollisionPair, CollisionConstraint > &potentialCollisions, float substepSeconds, std::unordered_map< EntityID, PhysicsStateFull > &currentState)
 Correctly applies collision position constraint for each potential collision detected.
void applyVelocityCollisionConstraints (std::map< CollisionPair, CollisionConstraint > &constraints, float substepSeconds, std::unordered_map< EntityID, PhysicsStateFull > &currentState)
 Correctly applies collision velocity constraint for each potential collision detected.
void applyPositionConstraints (std::map< CollisionPair, CollisionConstraint > &constraints, float substepSeconds, std::unordered_map< EntityID, PhysicsStateFull > &currentState)
 Applies all active position constraints.
void applyVelocityConstraints (std::map< CollisionPair, CollisionConstraint > &constraints, float substepSeconds, std::unordered_map< EntityID, PhysicsStateFull > &currentState)
 Applies all active velocity constraints.
void updateCollisionEventQueue (std::map< CollisionPair, CollisionConstraint > &potentialCollisions, std::queue< CollisionReport > &queuedReports, std::unordered_map< EntityID, PhysicsStateFull > &previousStates, std::unordered_map< EntityID, PhysicsStateFull > &currentStates, uint8_t nthSubstep)
 Tests each pair of potential colliders for intersection, adds collision report to the queue if configured.
void onCollided (const CollisionPair &pair, const Collision &collision, std::queue< CollisionReport > &queuedReports)
 Registers a pair of entities as intersecting, signalling to collision signal observers if required.
void onSeparated (const CollisionPair &pair, std::queue< CollisionReport > &queuedReports)
 Removes pair of entities from collision list, appending collision event to queue if required.
void registerCollisionSignal (EntityID entity)
 Registers collision signals for this entity.
void unregisterCollisionSignal (EntityID entity)
 Unregisters collision signals for this entity.
void reportCollisions (std::queue< CollisionReport > &reports, std::unordered_map< EntityID, std::pair< SignalCollided, SignalSeparated > > signallers) const
 Signals all collisions and separations collected over the course of the simulation step, in the order those events occurred.
bool wasColliding (const CollisionPair &pair) const
 Whether or not a particular pair of entities have their collision tracked by the physics system.
void collectPotentialCollisions (float substepSeconds, std::queue< CollisionReport > &queuedReports)
 Collects potential collisions and builds constraints from them.

Private Attributes

std::set< EntityIDmEntitiesUninitialized {}
 Holds entities that haven't undergone proper initialization, and therefore aren't eligible for proper physics updates.
std::unordered_map< EntityID, PhysicsStateFullmEntityStatePrevious {}
 Storage for local entity states prior to the integration step each substep.
std::unordered_map< EntityID, PhysicsStateFullmEntityStateCurrent {}
 Storage for current local entity states, eventually pushed to ECS component tables.
std::unordered_map< EntityID, std::set< ConstraintID > > mEntityConstraintMap {}
 Associates each entity with its set of constraints, used to determine whether the constraint can be evaluated at a given substep.
SweepPrune mBroadPhase {}
 Representation of the structure/technique being used to detect likely collisions.
std::unordered_map< EntityID, std::unordered_set< EntityID > > mEntityCollision {}
 All currently intersecting entities.
std::unordered_set< EntityIDmPotentialColliders {}
 A list of entities that may collide this frame.
std::queue< CollisionReportmCollisionReports {}
 Storage for collision reports that should be signalled this simulation frame.
std::unordered_map< EntityID, std::pair< SignalCollided, SignalSeparated > > mCollisionSignallers {}
 Entities whose collision events are signalled by this system.
std::map< CollisionPair, CollisionConstraintmCollisionConstraints {}
 Storage for pairs of entities that are likely to collide (or are colliding) this simulation frame.
std::vector< std::pair< std::unique_ptr< BaseConstraint >, std::vector< EntityID > > > mConstraints {}
 A list of all constraints known to the physics system, evaluated every frame.
std::unordered_set< ConstraintIDmConstraintsDeleted {}
 IDs of constraints that were deleted and which may be reused to define a new constraint.
uint8_t mSubsteps { 3 }
 The number of substeps used in the physics system's XPBD implementation.
bool mRequiresInitialization { true }
 Whether physics properties for all eligible entities should be recomputed.
bool mConstraintsInitialized { true }
 Whether we have yet to determine which constraints are active and which are not.

Detailed Description

The physics system, an ECS system that tracks and updates the state of each physics object in the scene according to its properties.

Relies strongly on the spatial query system to provide information about object overlaps and collisions. Collision events are generated when an object with a certain interaction mask overlaps with objects belonging to one of the layers indicated by that mask.

The physics system implemented here is based on XPBD (Extended Position Based Dynamics). See paper Detailed Rigid Body Simulation using Extended Position Based Dynamics.

See also
Spatial Queries

Member Typedef Documentation

◆ ConstraintID

ID naming a unique constraint registered with the physics system.

See also
registerConstraint()
unregisterConstraint()

Member Function Documentation

◆ getSystemTypeName()

std::string ToyMaker::PhysicsSystem::getSystemTypeName ( )
inlinestatic

The system type string for this class.

Returns
std::string This class' system type string

◆ integrateForces()

void PhysicsSystem::integrateForces ( float substepSeconds,
std::unordered_map< EntityID, PhysicsStateFull > & previousState,
std::unordered_map< EntityID, PhysicsStateFull > & currentState )
private

Derives position and rotation updates for physics objects based on their current state and forces acting on them.

See Impulse Based Dynamics for derivations of integration steps.

◆ onEntityEnabled()

void PhysicsSystem::onEntityEnabled ( EntityID entityID)
overrideprivate

Updates this entity's physics properties.

Parameters
entityIDThe updated entity

◆ onEntityUpdated()

void PhysicsSystem::onEntityUpdated ( EntityID entityID,
ComponentType updatedComponent )
overrideprivate

Updates this entity's physics properties.

Parameters
entityIDThe updated entity
updatedComponentThe updated component belonging to the entity

◆ onSimulationStep()

void PhysicsSystem::onSimulationStep ( uint32_t timestepMillis)
overrideprivate

Updates physics simulation.

Parameters
timestepMillisNumber of milliseconds by which to advance the simulation

◆ setSubsteps()

void ToyMaker::PhysicsSystem::setSubsteps ( uint8_t newSubsteps)
inline

Sets the number of substeps used for physics integration using XPBD.

See Detailed Rigid Body Simulation with Extended Position Based Dynamics - Matthias Muller et al

◆ updateCollisionEventQueue()

void PhysicsSystem::updateCollisionEventQueue ( std::map< CollisionPair, CollisionConstraint > & potentialCollisions,
std::queue< CollisionReport > & queuedReports,
std::unordered_map< EntityID, PhysicsStateFull > & previousStates,
std::unordered_map< EntityID, PhysicsStateFull > & currentStates,
uint8_t nthSubstep )
private

Tests each pair of potential colliders for intersection, adds collision report to the queue if configured.

Parameters
potentialCollisionsMap of all collisions that might take place this frame
queuedReportsAll reports due to be signalled this frame.

◆ updateProperties()

void PhysicsSystem::updateProperties ( EntityID entityID)
private

Computes rotational inertia from mass and object bounds.

Parameters
entityIDThe entity in need of an update

Member Data Documentation

◆ mBroadPhase

SweepPrune ToyMaker::PhysicsSystem::mBroadPhase {}
private

Representation of the structure/technique being used to detect likely collisions.

For the time being, this is just sweep and prune.

◆ mConstraints

std::vector<std::pair<std::unique_ptr<BaseConstraint>, std::vector<EntityID> > > ToyMaker::PhysicsSystem::mConstraints {}
private

A list of all constraints known to the physics system, evaluated every frame.

Constraints whose indices are found in mDeletedConstraints are inactive, and will not be evaluated.

◆ mSubsteps

uint8_t ToyMaker::PhysicsSystem::mSubsteps { 3 }
private

The number of substeps used in the physics system's XPBD implementation.

See also
getSubsteps
setSubsteps

See Detailed Rigid Body Simulation with Extended Position Based Dynamics - Matthias Muller et al


The documentation for this class was generated from the following files:
  • ToyMaker_Main/include/toymaker/engine/physics/system.hpp
  • ToyMaker_Main/src/physics/system.cpp