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... | |
PhysicsEngine & | operator= (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 PhysIterations & | getIterations () 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... | |
Scene & | getScene () |
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... | |
DebugDrawerFilter & | getDebugDrawerFilter () |
Get the debug drawer filter data. More... | |
const DebugDrawerFilter & | getDebugDrawerFilter () 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... | |
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.
using ime::PhysicsEngine::Ptr = std::unique_ptr<PhysicsEngine> |
Unique PhysicsEngine pointer.
Definition at line 102 of file PhysicsEngine.h.
|
delete |
Copy constructor.
ime::PhysicsEngine::~PhysicsEngine | ( | ) |
Destructor.
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.
|
static |
Create the physics simulation.
scene | The scene this world belongs to |
gravity | The acceleration of bodies due to gravity |
RigidBody::Ptr ime::PhysicsEngine::createBody | ( | RigidBody::Type | type = RigidBody::Type::Static | ) |
Create a rigid body.
type | The type of the body to be created |
By default, this function creates a static body
Joint::Ptr ime::PhysicsEngine::createJoint | ( | const JointDefinition & | definition | ) |
Create a joint.
definition | Definition to create a joint from |
std::size_t ime::PhysicsEngine::getBodyCount | ( | ) | const |
DebugDrawerFilter & ime::PhysicsEngine::getDebugDrawerFilter | ( | ) |
Get the debug drawer filter data.
The returned data may be manipulated to set what is rendered by the debug drawer
Vector2f ime::PhysicsEngine::getGravity | ( | ) | const |
const PhysIterations & ime::PhysicsEngine::getIterations | ( | ) | const |
Get the physics iterations per time step of the world.
std::size_t ime::PhysicsEngine::getJointCount | ( | ) | const |
Scene & ime::PhysicsEngine::getScene | ( | ) |
Get the scene the simulation belongs to.
float ime::PhysicsEngine::getTimescale | ( | ) | const |
bool ime::PhysicsEngine::isAutoClearForcesEnabled | ( | ) | const |
Check whether or not the force buffer on all bodies is cleared after each update.
bool ime::PhysicsEngine::isContinuousPhysicsEnabled | ( | ) | const |
Check whether continuous physics is enabled or not.
bool ime::PhysicsEngine::isDebugDrawEnabled | ( | ) | const |
Check if debug draw is enabled or not.
bool ime::PhysicsEngine::isFixedStep | ( | ) | const |
Check if the world is updated in fixed or variable time steps.
bool ime::PhysicsEngine::isLocked | ( | ) | const |
Check if the world is in the middle of a time step or not.
bool ime::PhysicsEngine::isSleepEnabled | ( | ) | const |
Check if the world can sleep or not.
bool ime::PhysicsEngine::isSubSteppingEnabled | ( | ) | const |
Check if sub-stepping is enabled or not.
|
delete |
Copy assignment operator.
void ime::PhysicsEngine::queryAABB | ( | const AABBCallback & | callback, |
const AABB & | aabb | ||
) |
Query the world for all colliders that overlap the given AABB.
callback | The function to be executed for every Collider that overlaps the given AABB |
aabb | The 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
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.
callback | The function to be executed when the ray collides with a collider |
startPoint | The starting point of the ray |
endPoint | The 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.
void ime::PhysicsEngine::setAutoClearForcesEnable | ( | bool | autoClear | ) |
Enable or disable automatic force buffer clearance after an update.
autoClear | True 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
void ime::PhysicsEngine::setContinuousPhysicsEnable | ( | bool | enable | ) |
Enable or disable continuous physics.
enable | True to enable otherwise false |
By default the physics engine uses continues physics
void ime::PhysicsEngine::setDebugDrawEnable | ( | bool | enable | ) |
Enable or disable debug drawing.
enable | True 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
void ime::PhysicsEngine::setFixedStep | ( | bool | fixed | ) |
Set whether or not the world is updated in fixed times steps.
fixed | True 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
void ime::PhysicsEngine::setGravity | ( | const Vector2f & | gravity | ) |
void ime::PhysicsEngine::setIterations | ( | const PhysIterations & | iterations | ) |
Set the iterations per time-step of the world.
iterations | New iterations to set |
By default, the position and velocity iterations are 3 and 8 respectively
void ime::PhysicsEngine::setSleepEnable | ( | bool | sleep | ) |
Set whether or not the world sleeps.
sleep | True to enable sleeping or false to disable |
void ime::PhysicsEngine::setSubSteppingEnable | ( | bool | subStep | ) |
Enable or disable sub-stepping.
subStep | True to enable sub-stepping, otherwise false |
void ime::PhysicsEngine::setTimescale | ( | float | timescale | ) |
Set the simulation timescale.
timescale | The 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)