World of Rigid Bodies (WoRB)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions > Class Template Reference

Encapsulates a system of rigid bodies. More...

#include <WoRB.h>

Collaboration diagram for WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >:

Data Structures

class  RigidBodies
 Represents a rigid body iterator for the WorldOfRigidBodies class. More...

Public Member Functions

 WorldOfRigidBodies ()
 Constructs an instance of WoRB class.
void RemoveObjects ()
 Removes all objects from the system.
void Add (Geometry *object)
 Adds new object to the system.
void Add (Geometry &object)
 Adds new object to the system.
void InitializeODE ()
 Prepares ODE (recalculates derived quantities)
void SolveODE (double h)
 Solves (integrates) equations of motion for the whole system.

Data Fields

Quaternion Gravity
 Holds common gravity applied to all objects (set to 0 to disable).
double Time
 Holds the system local time, in s.
unsigned long TimeStepCount
 Holds the number of integrator time-steps, from the simulation start.
double TotalKineticEnergy
 Holds the total kinetic energy of the system, in J.
double TotalPotentialEnergy
 Holds the total potential energy of the system, in J.
Quaternion TotalLinearMomentum
 Holds the total linear momentum of the system, in kg m^2 s^-1.
Quaternion TotalAngularMomentum
 Holds the total angular momentum of the system, in kg m^2 s^-1.
CollisionResolver Collisions
 Holds the collision data for all registered collisions.
Collision CollisionRegistry [MaxCollisions]
 Holds the memory parcel where collisions are registered.

Private Attributes

unsigned ObjectCount
 Holds the number of the objects in the system.
GeometryObject [MaxObjects]
 Points to an array of the objects handled by the system.

Friends

class RigidBodies

Detailed Description

template<unsigned MaxObjects, unsigned MaxCollisions>
class WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >

Encapsulates a system of rigid bodies.

Definition at line 30 of file WoRB.h.


Constructor & Destructor Documentation

template<unsigned MaxObjects, unsigned MaxCollisions>
WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::WorldOfRigidBodies ( ) [inline]

Constructs an instance of WoRB class.

Definition at line 160 of file WoRB.h.

            : Collisions( CollisionRegistry, MaxCollisions )
        {
        }

Member Function Documentation

template<unsigned MaxObjects, unsigned MaxCollisions>
void WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::Add ( Geometry object) [inline]

Adds new object to the system.

Definition at line 176 of file WoRB.h.

Referenced by WoRB_MexFunction::Parse().

        {
            Object[ ObjectCount++ ] = object;
        }
template<unsigned MaxObjects, unsigned MaxCollisions>
void WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::Add ( Geometry object) [inline]

Adds new object to the system.

Definition at line 183 of file WoRB.h.

        {
            Object[ ObjectCount++ ] = &object;
        }
template<unsigned MaxObjects, unsigned MaxCollisions>
void WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::InitializeODE ( ) [inline]

Prepares ODE (recalculates derived quantities)

Definition at line 192 of file WoRB.h.

Referenced by WoRB_MexFunction::Parse().

        {
            Time          = 0;
            TimeStepCount = 0;

            Collisions.Initialize ();

            for ( RigidBodies body = RigidBodies(this); body.Exists(); ++body )
            {
                body->CalculateDerivedQuantities ();
                body->ClearAccumulators ();
            }

            TotalKineticEnergy   = 0;
            TotalPotentialEnergy = 0;
            TotalLinearMomentum  = 0;
            TotalAngularMomentum = 0;

            for ( RigidBodies body = RigidBodies(this); body.Exists(); ++body )
            {
                TotalKineticEnergy   += body->KineticEnergy;
                TotalPotentialEnergy += body->PotentialEnergy;
                TotalLinearMomentum  += body->LinearMomentum;
                TotalAngularMomentum += body->TotalAngularMomentum;
            }
        }
template<unsigned MaxObjects, unsigned MaxCollisions>
void WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::RemoveObjects ( ) [inline]

Removes all objects from the system.

Definition at line 169 of file WoRB.h.

        {
            ObjectCount = 0;
        }
template<unsigned MaxObjects, unsigned MaxCollisions>
void WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::SolveODE ( double  h) [inline]

Solves (integrates) equations of motion for the whole system.

Solves ODE for all the objects in the system, performs the contact generation and then resolves detected contacts.

Parameters:
hIntegrator time-step length

Definition at line 224 of file WoRB.h.

        {
            /////////////////////////////////////////////////////////////////////////////
            // Calculate and accumulate all external and internal forces
            //

            // Add gravity
            //
            for ( RigidBodies body = RigidBodies(this); body.Exists(); ++body )
            {
                Quaternion f_g = body->Mass() * Gravity;
                double E_p = - f_g.Dot( body->Position );

                body->AddExternalForce( f_g, E_p );
            }

            /////////////////////////////////////////////////////////////////////////////
            // Solve ODE for every object in the system
            //
            for ( RigidBodies body = RigidBodies(this); body.Exists(); ++body )
            {
                body->SolveODE( h );
            }

            // Solve system local time (avoiding `Time += h` cause of rounding-errors).
            //
            Time = h * (++TimeStepCount);

            /////////////////////////////////////////////////////////////////////////////
            // Calculate derived quantities

            TotalKineticEnergy   = 0;
            TotalPotentialEnergy = 0;
            TotalLinearMomentum  = 0;
            TotalAngularMomentum = 0;

            for ( RigidBodies body = RigidBodies(this); body.Exists(); ++body )
            {
                TotalKineticEnergy   += body->KineticEnergy;
                TotalPotentialEnergy += body->PotentialEnergy;
                TotalLinearMomentum  += body->LinearMomentum;
                TotalAngularMomentum += body->TotalAngularMomentum;
            }

            /////////////////////////////////////////////////////////////////////////////
            // Collision Detection

            Collisions.Initialize ();

            // Detect and register collisions between all objects in the system
            //
            for ( unsigned i = 0; i < ObjectCount; ++i )
            {
                for ( unsigned j = i + 1; j < ObjectCount; ++j )
                {
                    Object[i]->Detect( Collisions, Object[j] );
                }
            }

            Collisions.UpdateDerivedQuantities( h );

            /////////////////////////////////////////////////////////////////////////////
            // Collision Response

            Collisions.ImpulseTransfers( h );
            Collisions.PositionProjections ();

            /////////////////////////////////////////////////////////////////////////////
            // Prepare force and torque accumulators for the next time-step
            //
            for ( RigidBodies body = RigidBodies(this); body.Exists(); ++body )
            {
                body->ClearAccumulators ();
            }
        }

Friends And Related Function Documentation

template<unsigned MaxObjects, unsigned MaxCollisions>
friend class RigidBodies [friend]

Field Documentation

template<unsigned MaxObjects, unsigned MaxCollisions>
Collision WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::CollisionRegistry[MaxCollisions]

Holds the memory parcel where collisions are registered.

Definition at line 154 of file WoRB.h.

template<unsigned MaxObjects, unsigned MaxCollisions>
CollisionResolver WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::Collisions
template<unsigned MaxObjects, unsigned MaxCollisions>
Quaternion WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::Gravity

Holds common gravity applied to all objects (set to 0 to disable).

Definition at line 118 of file WoRB.h.

Referenced by WoRB_MexFunction::Parse(), and WoRB::WorldOfRigidBodies< 256, 1024 >::SolveODE().

template<unsigned MaxObjects, unsigned MaxCollisions>
double WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::Time
template<unsigned MaxObjects, unsigned MaxCollisions>
unsigned long WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::TimeStepCount

Holds the number of integrator time-steps, from the simulation start.

Definition at line 128 of file WoRB.h.

Referenced by WoRB::WorldOfRigidBodies< 256, 1024 >::InitializeODE(), WoRB_MexFunction::OnProcessData(), and WoRB::WorldOfRigidBodies< 256, 1024 >::SolveODE().

template<unsigned MaxObjects, unsigned MaxCollisions>
Quaternion WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::TotalAngularMomentum

Holds the total angular momentum of the system, in kg m^2 s^-1.

Definition at line 144 of file WoRB.h.

Referenced by WoRB::WorldOfRigidBodies< 256, 1024 >::InitializeODE(), WoRB_MexFunction::OnProcessData(), and WoRB::WorldOfRigidBodies< 256, 1024 >::SolveODE().

template<unsigned MaxObjects, unsigned MaxCollisions>
double WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::TotalKineticEnergy

Holds the total kinetic energy of the system, in J.

Definition at line 132 of file WoRB.h.

Referenced by WoRB::WorldOfRigidBodies< 256, 1024 >::InitializeODE(), WoRB_MexFunction::OnProcessData(), and WoRB::WorldOfRigidBodies< 256, 1024 >::SolveODE().

template<unsigned MaxObjects, unsigned MaxCollisions>
Quaternion WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::TotalLinearMomentum

Holds the total linear momentum of the system, in kg m^2 s^-1.

Definition at line 140 of file WoRB.h.

Referenced by WoRB::WorldOfRigidBodies< 256, 1024 >::InitializeODE(), WoRB_MexFunction::OnProcessData(), and WoRB::WorldOfRigidBodies< 256, 1024 >::SolveODE().

template<unsigned MaxObjects, unsigned MaxCollisions>
double WoRB::WorldOfRigidBodies< MaxObjects, MaxCollisions >::TotalPotentialEnergy

Holds the total potential energy of the system, in J.

Definition at line 136 of file WoRB.h.

Referenced by WoRB::WorldOfRigidBodies< 256, 1024 >::InitializeODE(), WoRB_MexFunction::OnProcessData(), and WoRB::WorldOfRigidBodies< 256, 1024 >::SolveODE().


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