Quark Physics  1.0
2D Rigid and Soft Body Physics Engine
QBody Class Reference

QBody objects are the base class for all types of bodies. Any class derived from QBody shares these methods and properties. Additionally, this base type provides flexible and common references for independent operations from qualified and named body types in the entire simulation. Virtual methods are defined for users to create different body types that inherit QBody. For example, the Update() method is used to apply different dynamics for all body types. QRigidBody instances derived from QBody have their own Update() solution, while QSoftBody instances have their own Update() solution. With these and similar virtual methods, unique new body types can be created. More...

#include <qbody.h>

Inheritance diagram for QBody:
Collaboration diagram for QBody:

Classes

struct  BodyPairEqual
 
struct  BodyPairHash
 
struct  CollisionInfo
 CollisionInfo structure contains collision information of a body object. This information is sent to the relevant event listeners during a collision event. More...
 

Public Types

enum  Modes { DYNAMIC , STATIC }
 
enum  BodyTypes { RIGID , AREA , SOFT }
 
enum  SimulationModels { MASS_SPRING , RIGID_BODY }
 

Public Member Functions

virtual void OnPreStep ()
 
virtual void OnStep ()
 
virtual bool OnCollision (CollisionInfo)
 
BodyTypes GetBodyType ()
 
QWorldGetWorld ()
 
QVector GetPosition ()
 
QVector GetPreviousPosition ()
 
float GetRotation ()
 
float GetRotationDegree ()
 
float GetPreviousRotation ()
 
QAABB GetAABB () const
 
float GetTotalInitialArea ()
 
float GetTotalPolygonsInitialArea ()
 
float GetTotalArea ()
 
float GetTotalPolygonsArea ()
 
Modes GetMode ()
 
float GetInertia ()
 
int GetLayersBit ()
 
int GetCollidableLayersBit ()
 
bool GetOverlapWithCollidableLayersBit (int layersBit)
 
bool GetOverlapWithLayersBit (int layersBit)
 
bool GetIsSleeping ()
 
bool GetCanSleep ()
 
SimulationModels GetSimulationModel ()
 
float GetFriction ()
 
float GetStaticFriction ()
 
float GetAirFriction ()
 
virtual float GetMass ()
 
float GetRestitution ()
 
float GetCircumference ()
 
bool GetBodySpecificTimeScaleEnabled ()
 
float GetBodySpesificTimeScale ()
 
bool GetEnabled ()
 
float GetVelocityLimit ()
 
bool GetIntegratedVelocitiesEnabled ()
 
QBodySetPosition (QVector value, bool withPreviousPosition=true)
 
QBodyAddPosition (QVector value, bool withPreviousPosition=true)
 
QBodySetPreviousPosition (QVector value)
 
QBodyAddPreviousPosition (QVector value)
 
QBodySetRotation (float angleRadian, bool withPreviousRotation=true)
 
QBodySetRotationDegree (float degree, bool withPreviousRotation=true)
 
QBodyAddRotation (float angleRadian, bool withPreviousRotation=true)
 
QBodySetPreviousRotation (float angleRadian)
 
QBodyAddPreviousRotation (float angleRadian)
 
QBodySetLayersBit (int value)
 
QBodySetCollidableLayersBit (int value)
 
QBodySetCanSleep (bool value)
 
QBodySetMode (QBody::Modes bodyMode)
 
QBodySetSimulationModel (SimulationModels model)
 
QBodySetFriction (float value)
 
QBodySetStaticFriction (float value)
 
QBodySetAirFriction (float value)
 
QBodySetMass (float value)
 
QBodySetRestitution (float value)
 
QBodySetBodySpecificTimeScaleEnabled (bool value)
 
QBodySetBodySpecificTimeScale (float value)
 
QBodySetEnabled (bool value)
 
QBodySetIntegratedVelocitiesEnabled (bool value)
 
QBodyAddMesh (QMesh *mesh)
 
QBodyRemoveMeshAt (int index)
 
QMeshGetMeshAt (int index)
 
int GetMeshCount ()
 
vector< QMesh * > * GetMeshes ()
 
QBodyAddMeshesFromFile (string filePath)
 
QBodyWakeUp ()
 
QBodySetVelocityLimit (float value)
 

Public Attributes

std::function< void(QBody *body)> PreStepEventListener
 
std::function< void(QBody *body)> StepEventListener
 
std::function< bool(QBody *body, CollisionInfo)> CollisionEventListener
 

Protected Member Functions

void UpdateAABB ()
 
void UpdateMeshTransforms ()
 
virtual void Update ()
 
virtual void PostUpdate ()
 
virtual bool CanGiveCollisionResponseTo (QBody *otherBody)
 

Static Protected Member Functions

static QVector ComputeFriction (QBody *bodyA, QBody *bodyB, QVector &normal, float penetration, QVector &relativeVelocity)
 
static bool CanCollide (QBody *bodyA, QBody *bodyB, bool checkBodiesAreEnabled=true)
 

Protected Attributes

QWorldworld
 
QVector position =QVector(0,0)
 
QVector prevPosition =QVector::Zero()
 
float rotation =0.0f
 
float prevRotation =0.0f
 
QAABB aabb
 
Modes mode =QBody::Modes::DYNAMIC
 
bool inertiaNeedsUpdate =true
 
bool circumferenceNeedsUpdate =true
 
bool enableBodySpecificTimeScale =false
 
float bodySpecificTimeScale =1.0f
 
BodyTypes bodyType =BodyTypes::RIGID
 
bool enabled =true
 
float velocityLimit =0.0f
 
bool enableIntegratedVelocities =true
 
float friction =0.2f
 
float staticFriction =0.5f
 
float airFriction =0.01f
 
float mass =1.0f
 
float restitution =0.0f
 
int layersBit =1
 
int collidableLayersBit =1
 
bool isKinematic =false
 
bool allowKinematicCollisions =false
 
bool isSleeping =false
 
int sleepTick =120
 
int fixedVelocityTick =0
 
int fixedAngularTick =0
 
bool canSleep =true
 
vector< QMesh * > _meshes =vector<QMesh*>()
 
SimulationModels simulationModel =SimulationModels::RIGID_BODY
 

Friends

class QMesh
 
class QWorld
 
class QManifold
 
class QParticle
 
class QJoint
 
class QBroadPhase
 

Detailed Description

QBody objects are the base class for all types of bodies. Any class derived from QBody shares these methods and properties. Additionally, this base type provides flexible and common references for independent operations from qualified and named body types in the entire simulation. Virtual methods are defined for users to create different body types that inherit QBody. For example, the Update() method is used to apply different dynamics for all body types. QRigidBody instances derived from QBody have their own Update() solution, while QSoftBody instances have their own Update() solution. With these and similar virtual methods, unique new body types can be created.

Member Enumeration Documentation

◆ Modes

Determines whether the body is dynamic or static. A static body does not react to any force, constraint or collision and does not move.A dynamic body reacts to forces, constraints, collisions, and any other world event.

◆ SimulationModels

Determines which approach will be used to simulate a body object.

Member Function Documentation

◆ AddMesh()

QBody * QBody::AddMesh ( QMesh mesh)

Adds a mesh to the body.

Parameters
meshA pointer of the mesh to add.
Returns
A pointer to the body itself.

◆ AddMeshesFromFile()

QBody * QBody::AddMeshesFromFile ( string  filePath)

Adds meshes with a json based *.qmesh file.

Parameters
filePathA file path.
Returns
A pointer to the body itself.

◆ AddPosition()

QBody* QBody::AddPosition ( QVector  value,
bool  withPreviousPosition = true 
)
inline

Adds a vector value the position of the body.

Parameters
valueA vector value to add.
withPreviousPositionDetermines whether apply the position to the previous position of the body.In this simulation, since velocities are implicit, if the previous position are the same as the newly set position, the positional velocity of the body will also be zeroed.Therefore, if you want to zero out the positional velocity when setting the new position, use this option.
Returns
A pointer to the body itself.

◆ AddPreviousPosition()

QBody* QBody::AddPreviousPosition ( QVector  value)
inline

Adds a vector the previous position of the body.

Parameters
valueA position value to add.
Returns
A pointer to the body itself.

◆ AddPreviousRotation()

QBody* QBody::AddPreviousRotation ( float  angleRadian)
inline

Adds a value to the previous rotation of the body.

Parameters
angleRadianA value to add, in radians.
Returns
A pointer to the body itself.

◆ AddRotation()

QBody* QBody::AddRotation ( float  angleRadian,
bool  withPreviousRotation = true 
)
inline

Adds a value to the rotation of the body.

Parameters
angleRadianA value to add, in radians.
withPreviousRotationDetermines whether apply the rotation to the previous rotation of the body.In this simulation, since velocities are implicit, if the previous rotation are the same as the newly set rotation, the angular velocity of the body will also be zeroed.Therefore, if you want to zero out the angular velocity when setting the new rotation, use this option.
Returns
A pointer to the body itself.

◆ GetAABB()

QAABB QBody::GetAABB ( ) const
inline

Returns the AABB feature of the body.

◆ GetAirFriction()

float QBody::GetAirFriction ( )
inline

Returns the air friction value of the body.

◆ GetBodySpecificTimeScaleEnabled()

bool QBody::GetBodySpecificTimeScaleEnabled ( )
inline

Returns whether the body spesific time scale is enabled.

◆ GetBodySpesificTimeScale()

float QBody::GetBodySpesificTimeScale ( )
inline

Returns the body spesific time scale

◆ GetBodyType()

BodyTypes QBody::GetBodyType ( )
inline

Returns the type of the body.

◆ GetCanSleep()

bool QBody::GetCanSleep ( )
inline

Returns whether the body can sleep.

◆ GetCircumference()

float QBody::GetCircumference ( )
inline

Returns the circumference of the body.

◆ GetCollidableLayersBit()

int QBody::GetCollidableLayersBit ( )
inline

Returns the bit mask that represents the collidable layers in which the body object is present.

◆ GetEnabled()

bool QBody::GetEnabled ( )
inline

Returns whether the body is enabled.

◆ GetFriction()

float QBody::GetFriction ( )
inline

Returns the friction value of the body.

◆ GetInertia()

float QBody::GetInertia ( )
inline

Returns the inertia of the body.

◆ GetIntegratedVelocitiesEnabled()

bool QBody::GetIntegratedVelocitiesEnabled ( )

Returns whether the application of gravity and various velocity integrators necessary for the body's movement in the physics world is enabled. It is set to true by default. Typically, it is disabled for specific body objects that require manual control.

◆ GetIsSleeping()

bool QBody::GetIsSleeping ( )
inline

Returns whether the body is sleeping.

◆ GetLayersBit()

int QBody::GetLayersBit ( )
inline

Returns the bit mask that represents the layers in which the body object is present.

◆ GetMass()

virtual float QBody::GetMass ( )
inlinevirtual

Returns the mass value of the body.

Reimplemented in QSoftBody.

◆ GetMeshAt()

QMesh * QBody::GetMeshAt ( int  index)

Returns the mesh of the body with specified index.

Parameters
indexThe index of the mesh.

◆ GetMeshCount()

int QBody::GetMeshCount ( )

Returns the count of the meshes of to the body.

◆ GetMeshes()

vector< QMesh * > * QBody::GetMeshes ( )

Returns the collection of the meshes of the body.

◆ GetMode()

Modes QBody::GetMode ( )
inline

Returns whether the body is dynamic or static.

◆ GetOverlapWithCollidableLayersBit()

bool QBody::GetOverlapWithCollidableLayersBit ( int  layersBit)
inline

Returns whether a body object can collide with other body objects that have the given layers bit as parameters.

Parameters
layersBitA bit value representing the layers to check.

◆ GetOverlapWithLayersBit()

bool QBody::GetOverlapWithLayersBit ( int  layersBit)
inline

Checks whether a body object overlaps with a given bit value that represents layers. Returns true if the body object is in at least one of the layers represented by the given bit.

Parameters
layersBitA bit value representing the layers to check.

◆ GetPosition()

QVector QBody::GetPosition ( )
inline

Returns the position of the body.

◆ GetPreviousPosition()

QVector QBody::GetPreviousPosition ( )
inline

Returns the previous position of the body.

◆ GetPreviousRotation()

float QBody::GetPreviousRotation ( )
inline

Returns the previous rotation of the body.

◆ GetRestitution()

float QBody::GetRestitution ( )
inline

Returns the restitution value of the body.

◆ GetRotation()

float QBody::GetRotation ( )
inline

Returns the rotation of the body.

◆ GetRotationDegree()

float QBody::GetRotationDegree ( )
inline

Returns the rotation of the body as degree.

◆ GetSimulationModel()

SimulationModels QBody::GetSimulationModel ( )
inline

Returns which approach will be used to simulate a body object.

◆ GetStaticFriction()

float QBody::GetStaticFriction ( )
inline

Returns the static friction value of the body.

◆ GetTotalArea()

float QBody::GetTotalArea ( )
inline

Returns the total area of the body.

◆ GetTotalInitialArea()

float QBody::GetTotalInitialArea ( )
inline

Returns the total initial area of the body. Initial area means the calculated total area with non-transformed meshes of the body.

◆ GetTotalPolygonsArea()

float QBody::GetTotalPolygonsArea ( )
inline

Returns the total area of the polygons of the body.

◆ GetTotalPolygonsInitialArea()

float QBody::GetTotalPolygonsInitialArea ( )
inline

Returns the total initial area of the polygons of the body. Initial area means the calculated total area with non-transformed meshes of the body.

◆ GetVelocityLimit()

float QBody::GetVelocityLimit ( )

Returns the velocity limit of the physics body. If set to 0, no velocity limit is applied. The default value is 0.

◆ GetWorld()

QWorld* QBody::GetWorld ( )
inline

Returns the world.

◆ OnCollision()

virtual bool QBody::OnCollision ( CollisionInfo  )
inlinevirtual

The event is triggered when a body object collides with another body object during a physics step.

Parameters
CollisionInfoContains collision informations.
Returns
If the event method returns true, collision responses are applied. If it returns false, collision responses are not applied.

◆ OnPreStep()

virtual void QBody::OnPreStep ( )
inlinevirtual

The event is triggered at the beginning of a physics step, before any collision or constraint operations have been applied to body objects. It is a good time to perform instantaneous operations that will manipulate the presence of a body object in the world.

◆ OnStep()

virtual void QBody::OnStep ( )
inlinevirtual

The event is triggered after collision and constraint operations are applied in a physics step. It is a good time to perform operations that are not instantaneous but rather intended to be applied in the next physics step on body objects.

◆ PostUpdate()

virtual void QBody::PostUpdate ( )
inlineprotectedvirtual

Called after all bodies have completed their Update step to perform post-update operations.

Reimplemented in QSoftBody, QRigidBody, and QPlatformerBody.

◆ RemoveMeshAt()

QBody * QBody::RemoveMeshAt ( int  index)

Removes a mesh from the body.

Parameters
indexThe index of the mesh to remove.
Returns
A pointer to the body itself.

◆ SetAirFriction()

QBody* QBody::SetAirFriction ( float  value)
inline

Sets the air friction value of the body.

Parameters
valueA value to set.
Returns
A pointer to the body itself.

◆ SetBodySpecificTimeScale()

QBody* QBody::SetBodySpecificTimeScale ( float  value)
inline

Sets the time scale specific to the body. The world has a time scale, but this allows you to assign a specific time scale to the body object as an exception.

Parameters
valueA value to set.
Returns
A pointer to the body itself.

◆ SetBodySpecificTimeScaleEnabled()

QBody* QBody::SetBodySpecificTimeScaleEnabled ( bool  value)
inline

Sets whether the body-specific time scale is enabled. The world has a time scale, but this allows you to assign a specific time scale to the body object as an exception.

Parameters
valueA value to set.
Returns
A pointer to the body itself.

◆ SetCanSleep()

QBody* QBody::SetCanSleep ( bool  value)
inline

Sets whether the body can sleep in the active sleep mode.

Parameters
valueTrue or false.
Returns
A pointer to the body itself.

◆ SetCollidableLayersBit()

QBody* QBody::SetCollidableLayersBit ( int  value)
inline

Sets the bit mask that represents the collidable layers in which the body object is present.A body object can collide with other body objects present in the layers defined by the user.

Parameters
valueA bit mask value to set.
Returns
A pointer to the body itself.

◆ SetEnabled()

QBody* QBody::SetEnabled ( bool  value)
inline

Sets whether the body is enabled.

Parameters
valueA value to set
Returns
A pointer to the body itself.

◆ SetFriction()

QBody* QBody::SetFriction ( float  value)
inline

Sets the friction value of the body.

Parameters
valueA value to set.
Returns
A pointer to the body itself.

◆ SetIntegratedVelocitiesEnabled()

QBody * QBody::SetIntegratedVelocitiesEnabled ( bool  value)

Sets whether the application of gravity and various velocity integrators necessary for the body's movement in the physics world is enabled. It is set to true by default. Typically, it is disabled for specific body objects that require manual control.

◆ SetLayersBit()

QBody* QBody::SetLayersBit ( int  value)
inline

Sets the bit mask that represents the layers in which the body object is present.

Parameters
valueA bit mask value to set.
Returns
A pointer to the body itself.

◆ SetMass()

QBody* QBody::SetMass ( float  value)
inline

Sets the mass value of the body.

Parameters
valueA value to set.
Returns
A pointer to the body itself.

◆ SetMode()

QBody* QBody::SetMode ( QBody::Modes  bodyMode)
inline

Sets whether the body is dynamic or static.

Parameters
bodyModeA mode to set.
Returns
A pointer to the body itself.

◆ SetPosition()

QBody* QBody::SetPosition ( QVector  value,
bool  withPreviousPosition = true 
)
inline

Sets the position of the body.

Parameters
valueA position value to set.
withPreviousPositionDetermines whether apply the position to the previous position of the body.In this simulation, since velocities are implicit, if the previous position are the same as the newly set position, the positional velocity of the body will also be zeroed.Therefore, if you want to zero out the positional velocity when setting the new position, use this option.
Returns
A pointer to the body itself.

◆ SetPreviousPosition()

QBody* QBody::SetPreviousPosition ( QVector  value)
inline

Sets the previous position of the body.

Parameters
valueA position value to set.
Returns
A pointer to the body itself.

◆ SetPreviousRotation()

QBody* QBody::SetPreviousRotation ( float  angleRadian)
inline

Sets the previous rotation of the body.

Parameters
angleRadianA rotation value to set, in radians.
Returns
A pointer to the body itself.

◆ SetRestitution()

QBody* QBody::SetRestitution ( float  value)
inline

Sets the restitution value of the body.

Parameters
valueA value to set.
Returns
A pointer to the body itself.

◆ SetRotation()

QBody* QBody::SetRotation ( float  angleRadian,
bool  withPreviousRotation = true 
)
inline

Sets the rotation of the body.

Parameters
angleRadianA rotation value to set, in radians.
withPreviousRotationDetermines whether apply the rotation to the previous rotation of the body.In this simulation, since velocities are implicit, if the previous rotation are the same as the newly set rotation, the angular velocity of the body will also be zeroed.Therefore, if you want to zero out the angular velocity when setting the new rotation, use this option.
Returns
A pointer to the body itself.

◆ SetRotationDegree()

QBody* QBody::SetRotationDegree ( float  degree,
bool  withPreviousRotation = true 
)
inline

Sets the rotation of the body with specified angle in degrees.

Parameters
degreeA rotation value to set, in degrees.
withPreviousRotationDetermines whether apply the rotation to the previous rotation of the body.In this simulation, since velocities are implicit, if the previous rotation are the same as the newly set rotation, the angular velocity of the body will also be zeroed.Therefore, if you want to zero out the angular velocity when setting the new rotation, use this option.
Returns
A pointer to the body itself.

◆ SetSimulationModel()

QBody* QBody::SetSimulationModel ( SimulationModels  model)
inline

Sets which approach will be used to simulate a body object.

Parameters
bodyModeA simulation model to set.
Returns
A pointer to the body itself.

◆ SetStaticFriction()

QBody* QBody::SetStaticFriction ( float  value)
inline

Sets the static friction value of the body.

Parameters
valueA value to set.
Returns
A pointer to the body itself.

◆ SetVelocityLimit()

QBody * QBody::SetVelocityLimit ( float  value)

Limits the velocity of the physics body. If set to 0, no velocity limit is applied. The default value is 0.

Returns
A pointer to the body itself.

◆ Update()

virtual void QBody::Update ( )
inlineprotectedvirtual

Updates properties of the soft body and applies needed physical dynamics.

Reimplemented in QSoftBody, and QRigidBody.

◆ WakeUp()

QBody* QBody::WakeUp ( )
inline

Wakes up a body that is in a sleeping state.

Returns
A pointer to the body itself.

Member Data Documentation

◆ CollisionEventListener

std::function<bool(QBody *body,CollisionInfo)> QBody::CollisionEventListener

This is the event listener callback function for the OnCollision event.

Parameters
bodyThe body object that triggers the event.
CollisionInfoContains collision informations.

◆ PreStepEventListener

std::function<void(QBody *body)> QBody::PreStepEventListener

This is the event listener callback function for the OnPreStep event.

Parameters
bodyThe body object that triggers the event.

◆ StepEventListener

std::function<void(QBody *body)> QBody::StepEventListener

This is the event listener callback function for the OnStep event.

Parameters
bodyThe body object that triggers the event.

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