Public Types | Public Member Functions | Static Public Member Functions | List of all members
ime::PhysicsEngine Class Referencefinal

The physics world is responsible for creating, managing, colliding and updating all of the bodies within it. More...

#include <PhysicsEngine.h>

Public Types

using Ptr = std::unique_ptr< PhysicsEngine >
 Unique PhysicsEngine pointer. More...
 

Public Member Functions

 PhysicsEngine (const PhysicsEngine &)=delete
 Copy constructor. More...
 
PhysicsEngineoperator= (const PhysicsEngine &)=delete
 Copy assignment operator. More...
 
void setGravity (const Vector2f &gravity)
 Change the gravity of the world. More...
 
Vector2f getGravity () const
 Get the gravity of the world. More...
 
void setIterations (const PhysIterations &iterations)
 Set the iterations per time-step of the world. More...
 
const PhysIterationsgetIterations () const
 Get the physics iterations per time step of the world. More...
 
void setTimescale (float timescale)
 Set the simulation timescale. More...
 
float getTimescale () const
 Get the timescale. More...
 
void setContinuousPhysicsEnable (bool enable)
 Enable or disable continuous physics. More...
 
bool isContinuousPhysicsEnabled () const
 Check whether continuous physics is enabled or not. More...
 
void setFixedStep (bool fixed)
 Set whether or not the world is updated in fixed times steps. More...
 
bool isFixedStep () const
 Check if the world is updated in fixed or variable time steps. More...
 
RigidBody::Ptr createBody (RigidBody::Type type=RigidBody::Type::Static)
 Create a rigid body. More...
 
Joint::Ptr createJoint (const JointDefinition &definition)
 Create a joint. More...
 
void update (Time deltaTime)
 
void setAutoClearForcesEnable (bool autoClear)
 Enable or disable automatic force buffer clearance after an update. More...
 
bool isAutoClearForcesEnabled () const
 Check whether or not the force buffer on all bodies is cleared after each update. More...
 
void clearForces ()
 Manually clear the force buffer on all bodies. More...
 
void setSleepEnable (bool sleep)
 Set whether or not the world sleeps. More...
 
bool isSleepEnabled () const
 Check if the world can sleep or not. More...
 
void setSubSteppingEnable (bool subStep)
 Enable or disable sub-stepping. More...
 
bool isSubSteppingEnabled () const
 Check if sub-stepping is enabled or not. More...
 
std::size_t getBodyCount () const
 Get the number of bodies in the world. More...
 
std::size_t getJointCount () const
 Get the number of joints in the world. More...
 
bool isLocked () const
 Check if the world is in the middle of a time step or not. More...
 
void rayCast (const RayCastCallback &callback, Vector2f startPoint, Vector2f endPoint)
 Ray-cast the world for all the colliders in the path of the ray. More...
 
void queryAABB (const AABBCallback &callback, const AABB &aabb)
 Query the world for all colliders that overlap the given AABB. More...
 
ScenegetScene ()
 Get the scene the simulation belongs to. More...
 
void setDebugDrawEnable (bool enable)
 Enable or disable debug drawing. More...
 
bool isDebugDrawEnabled () const
 Check if debug draw is enabled or not. More...
 
DebugDrawerFiltergetDebugDrawerFilter ()
 Get the debug drawer filter data. More...
 
const DebugDrawerFiltergetDebugDrawerFilter () const
 
std::unique_ptr< b2World > & getInternalWorld ()
 
void createDebugDrawer (priv::RenderTarget &renderWindow)
 
 ~PhysicsEngine ()
 Destructor. More...
 

Static Public Member Functions

static PhysicsEngine::Ptr create (Scene &scene, const Vector2f &gravity)
 Create the physics simulation. More...
 

Detailed Description

The physics world is responsible for creating, managing, colliding and updating all of the bodies within it.

Definition at line 100 of file PhysicsEngine.h.

Member Typedef Documentation

◆ Ptr

using ime::PhysicsEngine::Ptr = std::unique_ptr<PhysicsEngine>

Unique PhysicsEngine pointer.

Definition at line 102 of file PhysicsEngine.h.

Constructor & Destructor Documentation

◆ PhysicsEngine()

ime::PhysicsEngine::PhysicsEngine ( const PhysicsEngine )
delete

Copy constructor.

◆ ~PhysicsEngine()

ime::PhysicsEngine::~PhysicsEngine ( )

Destructor.

Member Function Documentation

◆ clearForces()

void ime::PhysicsEngine::clearForces ( )

Manually clear the force buffer on all bodies.

By default, forces are cleared automatically after each update. The default behavior is modified by calling autoClearForces. The purpose of this function is to support sub-stepping. Sub-stepping is often used to maintain a fixed sized time step under a variable frame-rate. When you perform sub-stepping you will disable auto clearing of forces and instead call clearForces after all sub-steps are complete in one pass of your game loop.

See also
setSubSteppingEnable and setAutoClearForcesEnable

◆ create()

static PhysicsEngine::Ptr ime::PhysicsEngine::create ( Scene scene,
const Vector2f gravity 
)
static

Create the physics simulation.

Parameters
sceneThe scene this world belongs to
gravityThe acceleration of bodies due to gravity
Returns
The created physics world

◆ createBody()

RigidBody::Ptr ime::PhysicsEngine::createBody ( RigidBody::Type  type = RigidBody::Type::Static)

Create a rigid body.

Parameters
typeThe type of the body to be created
Returns
The created body or a nullptr if this function is called inside a world callback

By default, this function creates a static body

Warning
This function is locked a
See also
getBodyCount

◆ createJoint()

Joint::Ptr ime::PhysicsEngine::createJoint ( const JointDefinition definition)

Create a joint.

Parameters
definitionDefinition to create a joint from
Returns
The created joint
Note
If the joined bodies are set to not collide, they will stop colliding after the joint is created
Warning
This function is locked during callbacks. This usually means should not attempt to create a joint inside a callback dispatched by the world (Callbacks are dispatched during a step)
See also
getJointCount

◆ getBodyCount()

std::size_t ime::PhysicsEngine::getBodyCount ( ) const

Get the number of bodies in the world.

Returns
The number of bodies in th world
See also
createBody

◆ getDebugDrawerFilter()

DebugDrawerFilter & ime::PhysicsEngine::getDebugDrawerFilter ( )

Get the debug drawer filter data.

Returns
A reference to the debug drawer filter data

The returned data may be manipulated to set what is rendered by the debug drawer

world.getDebugDrawerFilter().drawAABB = true; // Draw bounding boxes
See also
setDebugDrawEnable

◆ getGravity()

Vector2f ime::PhysicsEngine::getGravity ( ) const

Get the gravity of the world.

Returns
The gravity of the world
See also
getGravity

◆ getIterations()

const PhysIterations & ime::PhysicsEngine::getIterations ( ) const

Get the physics iterations per time step of the world.

Returns
The physics iterations
See also
setIterations

◆ getJointCount()

std::size_t ime::PhysicsEngine::getJointCount ( ) const

Get the number of joints in the world.

Returns
The number of joints in the world
See also
createJoint

◆ getScene()

Scene & ime::PhysicsEngine::getScene ( )

Get the scene the simulation belongs to.

Returns
The scene this simulation belongs to

◆ getTimescale()

float ime::PhysicsEngine::getTimescale ( ) const

Get the timescale.

Returns
The timescale
See also
setTimescale

◆ isAutoClearForcesEnabled()

bool ime::PhysicsEngine::isAutoClearForcesEnabled ( ) const

Check whether or not the force buffer on all bodies is cleared after each update.

Returns
True if the force buffer is automatically cleared, otherwise false
See also
setAutoClearForcesEnable

◆ isContinuousPhysicsEnabled()

bool ime::PhysicsEngine::isContinuousPhysicsEnabled ( ) const

Check whether continuous physics is enabled or not.

Returns
True if enabled, otherwise false
See also
setContinuousPhysicsEnable

◆ isDebugDrawEnabled()

bool ime::PhysicsEngine::isDebugDrawEnabled ( ) const

Check if debug draw is enabled or not.

Returns
True if debug draw is enabled, otherwise false
See also
setDebugDrawEnable

◆ isFixedStep()

bool ime::PhysicsEngine::isFixedStep ( ) const

Check if the world is updated in fixed or variable time steps.

Returns
True if world is updated in fixed time steps otherwise false
See also
setFixedUpdate

◆ isLocked()

bool ime::PhysicsEngine::isLocked ( ) const

Check if the world is in the middle of a time step or not.

Returns
True if the world is in the middle of a time step, otherwise false
Note
Some functions are locked while the world is in a time step. This means that any request to execute them will be denied

◆ isSleepEnabled()

bool ime::PhysicsEngine::isSleepEnabled ( ) const

Check if the world can sleep or not.

Returns
True if the world can sleep, otherwise false
See also
setSleepEnable

◆ isSubSteppingEnabled()

bool ime::PhysicsEngine::isSubSteppingEnabled ( ) const

Check if sub-stepping is enabled or not.

Returns
True if enabled, or false if disabled
See also
setSubSteppingEnable

◆ operator=()

PhysicsEngine & ime::PhysicsEngine::operator= ( const PhysicsEngine )
delete

Copy assignment operator.

◆ queryAABB()

void ime::PhysicsEngine::queryAABB ( const AABBCallback callback,
const AABB aabb 
)

Query the world for all colliders that overlap the given AABB.

Parameters
callbackThe function to be executed for every Collider that overlaps the given AABB
aabbThe AABB to query

The callback is executed for every collider that overlaps aabb. The callback must, on every invocation return true to continue with the query or false to terminate the query. When true is returned, the query will continue until all overlapping colliders are processed

◆ rayCast()

void ime::PhysicsEngine::rayCast ( const RayCastCallback callback,
Vector2f  startPoint,
Vector2f  endPoint 
)

Ray-cast the world for all the colliders in the path of the ray.

Parameters
callbackThe function to be executed when the ray collides with a collider
startPointThe starting point of the ray
endPointThe ending point of the ray

You can use ray casts to do line-of-sight checks, fire guns, etc. The callback will be called for every collider hit by the ray and the value it returns determines how the ray proceeds. Returning a value of zero indicates the ray cast should be terminated. A value of one indicates the ray cast should continue as if no hit occurred. A value of -1 filters the collider (The the ray cast will proceed as if the collider does not exist). If you return the fraction from the argument list, the ray will be clipped to the current intersection point. So you can ray cast any shape, ray cast all shapes, or ray cast the closest shape by returning the appropriate value.

Warning
Due to round-off errors, ray casts can sneak through small cracks between polygons in your static environment. If this is not acceptable in your game, trying slightly overlapping your polygons

◆ setAutoClearForcesEnable()

void ime::PhysicsEngine::setAutoClearForcesEnable ( bool  autoClear)

Enable or disable automatic force buffer clearance after an update.

Parameters
autoClearTrue to enable or false to disable

By default, the force buffer on all bodies is cleared after each call to update. You can disable automatic force buffer clearance if you want to use subStepping

See also
setSubSteppingEnable and clearForces

◆ setContinuousPhysicsEnable()

void ime::PhysicsEngine::setContinuousPhysicsEnable ( bool  enable)

Enable or disable continuous physics.

Parameters
enableTrue to enable otherwise false

By default the physics engine uses continues physics

See also
getContinuousPhysicsEnable

◆ setDebugDrawEnable()

void ime::PhysicsEngine::setDebugDrawEnable ( bool  enable)

Enable or disable debug drawing.

Parameters
enableTrue to enable debug drawing or false to disable it

Debug drawing allows you to see what the rigid bodies are doing and where they are in the world. When enabled, the physics simulation will render all the bodies it contains using geometric shapes such as circles and rectangles (Depending on the type of colliders on the bodies). By default, the simulation will only render the shapes of the rigid bodies, however you can use the getDebugDrawerFilter() function to control what gets rendered by the debug drawer.

Debug drawing is useful in many different ways. For instance say you have a rigid body attached to a game object and when the game object collides with a wall, the game object sprite always enters the wall by half of its size due to a origin mismatched between the sprite and the rigid body. In this case enabling debug draw will point out the problem immediately, saving you a lot of debugging time.

Be default, debug drawing is disabled

Note
Debug drawing is only available when IME is linked to in debug mode, calling this function when IME is linked to in release mode has no effect

◆ setFixedStep()

void ime::PhysicsEngine::setFixedStep ( bool  fixed)

Set whether or not the world is updated in fixed times steps.

Parameters
fixedTrue to update world in fixed time steps otherwise false

When set to false, the physics update is synced with the render fps which may vary from time to time. It is advised to use fixed time steps for consistent physics

By default, the world is updated using a fixed time step

See also
isFixedStep

◆ setGravity()

void ime::PhysicsEngine::setGravity ( const Vector2f gravity)

Change the gravity of the world.

Parameters
gravityThe new gravity
See also
getGravity

◆ setIterations()

void ime::PhysicsEngine::setIterations ( const PhysIterations iterations)

Set the iterations per time-step of the world.

Parameters
iterationsNew iterations to set

By default, the position and velocity iterations are 3 and 8 respectively

See also
ime::PhysIterations, getIterations

◆ setSleepEnable()

void ime::PhysicsEngine::setSleepEnable ( bool  sleep)

Set whether or not the world sleeps.

Parameters
sleepTrue to enable sleeping or false to disable
See also
isSleepEnabled

◆ setSubSteppingEnable()

void ime::PhysicsEngine::setSubSteppingEnable ( bool  subStep)

Enable or disable sub-stepping.

Parameters
subStepTrue to enable sub-stepping, otherwise false
See also
isSubSteppingEnabled

◆ setTimescale()

void ime::PhysicsEngine::setTimescale ( float  timescale)

Set the simulation timescale.

Parameters
timescaleThe new timescale

The timescale can be used to speed up or slow down the simulation without changing the FPS limit. Values above 1.0f speed up the simulation whilst values below 1.0f slow it down A timescale of zero freezes the simulation (Dynamic bodies stop moving).

By default the timescale is 1.0f (real-time)

Note
The simulation timescale will be multiplied by the timescale of the scene this simulation belongs to. If the scenes timescale is set to 2.0f and the simulation timescale is 2.0f, then the simulation will run 4 times as fast
See also
getTimescale

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