A rigid body. More...
#include <Body.h>
Public Types | |
enum | Type { Type::Static = 0, Type::Kinematic, Type::Dynamic } |
The rigid body type. More... | |
using | Ptr = std::shared_ptr< Body > |
Shared Body pointer. More... | |
using | ConstPtr = std::shared_ptr< const Body > |
Const shared world pointer. More... | |
using | WorldPtr = std::shared_ptr< World > |
Shared World pointer. More... | |
template<typename... Args> | |
using | Callback = std::function< void(Args...)> |
Event listener. More... | |
Public Member Functions | |
Body (const Body &)=delete | |
Copy constructor. More... | |
Body & | operator= (const Body &)=delete |
Copy assignment operator. More... | |
Body (Body &&) noexcept | |
Move constructor. More... | |
Body & | operator= (Body &&) noexcept |
Move assignment operator. More... | |
Body::Ptr | copy () const |
Create a copy of the rigid body. More... | |
std::string | getClassName () const override |
Get the name of this class. More... | |
void | attachCollider (Collider::Ptr collider) |
Attach a collider to the body. More... | |
Collider::Ptr | getColliderById (unsigned int id) |
Get a collider by its id. More... | |
void | removeColliderWithId (unsigned int id) |
Remove a collider with a given id from the body. More... | |
void | setPosition (Vector2f position) |
Set the world position of the body's local origin. More... | |
Vector2f | getPosition () const |
Get the world position of the body's origin. More... | |
void | setRotation (float angle) |
Set the body's rotation about the world origin. More... | |
float | getRotation () const |
Get the body's world rotation. More... | |
Vector2f | getWorldCenter () const |
Get the world position of the centre of mass. More... | |
Vector2f | getLocalCenter () const |
Get the local position of the centre of mass. More... | |
void | setLinearVelocity (Vector2f velocity) |
Set the linear velocity of the body in pixels per second. More... | |
Vector2f | getLinearVelocity () const |
Get the linear velocity of the centre of mass. More... | |
void | setAngularVelocity (float degrees) |
Set the angular velocity in degrees per second. More... | |
float | getAngularVelocity () const |
Get the angular velocity. More... | |
void | applyForce (Vector2f force, Vector2f point, bool wake=true) |
Apply a force at a world point. More... | |
void | applyForceToCenter (Vector2f force, bool wake=true) |
Apply a force to the centre of mass. More... | |
void | applyTorque (float torque, bool wake=true) |
Apply a torque. More... | |
void | applyLinearImpulse (Vector2f impulse, Vector2f point, bool wake=true) |
Apply an impulse at a world point. More... | |
void | applyLinearImpulseToCenter (Vector2f impulse, bool wake=true) |
Apply an impulse at the centre of mass. More... | |
void | applyAngularImpulse (float impulse, bool wake=true) |
Apply an angular impulse. More... | |
float | getMass () const |
Get the total mass of the body. More... | |
float | getInertia () const |
Get the rotational inertia of the body about the local origin. More... | |
Vector2f | getLocalPoint (Vector2f worldPoint) const |
Get the local coordinate of a world coordinate. More... | |
Vector2f | getWorldPoint (Vector2f localPoint) const |
Get the world coordinate of a local coordinate. More... | |
Vector2f | getLocalRotation (Vector2f worldVector) const |
Get the local rotation of a world rotation. More... | |
Vector2f | getWorldRotation (Vector2f localVector) const |
Get the world rotation of a local rotation. More... | |
Vector2f | getLinearVelocityFromLocalPoint (Vector2f localPoint) const |
Get the world velocity of a local point. More... | |
Vector2f | getLinearVelocityFromWorldPoint (Vector2f worldPoint) const |
Get the world linear velocity of a world point on the body. More... | |
void | setLinearDamping (float damping) |
Set the linear damping of the body. More... | |
float | getLinearDamping () const |
Get the linear damping of the body. More... | |
void | setAngularDamping (float damping) |
Set the angular damping. More... | |
float | getAngularDamping () const |
Get the angular damping of the body. More... | |
void | setGravityScale (float scale) |
Set the gravity scale of the body. More... | |
float | getGravityScale () const |
Get the gravity cale of the body. More... | |
void | setType (Type type) |
Change the type of the body. More... | |
Type | getType () const |
Get the body type. More... | |
void | setFastBody (bool fast) |
Set whether or not the body is fast moving. More... | |
bool | isFastBody () const |
Check if the body is a fast moving body or not. More... | |
void | setSleepingAllowed (bool sleeps) |
Set whether or not this body isSleepingAllowed. More... | |
bool | isSleepingAllowed () const |
Check if the body is allowed to sleep when inactive or not. More... | |
void | setAwake (bool awake) |
Awake the body or put it to sleep. More... | |
bool | isAwake () const |
Check if the body is awake or sleeping. More... | |
void | setEnabled (bool enable) |
Enable or disable a body. More... | |
bool | isEnabled () const |
Check whether or not the body is enabled. More... | |
void | setFixedRotation (bool rotate) |
Set whether or not the body can rotate. More... | |
bool | isFixedRotation () const |
Check if the body can rotate or not. More... | |
std::shared_ptr< GameObject > | getGameObject () |
Get the game object the body is attached to. More... | |
std::shared_ptr< GameObject > | getGameObject () const |
WorldPtr | getWorld () |
Get the physics world the body is in. More... | |
const WorldPtr & | getWorld () const |
PropertyContainer & | getUserData () |
Get the user data added to this body. More... | |
void | forEachCollider (Callback< Collider::Ptr > callback) |
Execute a function for each collider attached to the body. More... | |
void | onCollisionStart (Callback< Body::Ptr, Body::Ptr > callback) |
Add an event listener to a collision begin event. More... | |
void | onCollisionEnd (Callback< Body::Ptr, Body::Ptr > callback) |
Add an event listener to a collision end event. More... | |
void | emitCollisionEvent (const std::string &event, const Body::Ptr &other) |
std::unique_ptr< b2Body, Callback< b2Body * > > & | getInternalBody () |
const std::unique_ptr< b2Body, Callback< b2Body * > > & | getInternalBody () const |
~Body () override | |
Destructor. More... | |
void | setTag (const std::string &tag) |
Assign the object an alias. More... | |
const std::string & | getTag () const |
Get the alias of the object. More... | |
unsigned int | getObjectId () const |
Get the id of the object. More... | |
virtual std::string | getClassType () const |
Get the name of the direct parent of an object instance. More... | |
int | onPropertyChange (const std::string &property, const Callback< Property > &callback) |
Add an event listener to a specific property change event. More... | |
void | onPropertyChange (const Callback< Property > &callback) |
Add an event listener to a property change event. More... | |
int | onEvent (const std::string &event, const Callback<> &callback) |
Add an event listener to an event. More... | |
bool | unsubscribe (const std::string &event, int id) |
Remove an event listener from an event. More... | |
int | onDestruction (const Callback<> &callback) |
Add a destruction listener. More... | |
bool | removeDestructionListener (int id) |
Remove a destruction listener form the object. More... | |
bool | operator== (const Object &rhs) const |
Check if two objects are the same object or not. More... | |
bool | operator!= (const Object &rhs) const |
Check if two objects are not the same object. More... | |
Protected Member Functions | |
void | emitChange (const Property &property) |
Dispatch a property change event. More... | |
void | emit (const std::string &event) |
Dispatch an action event. More... | |
Friends | |
class | World |
Needs access to constructor. More... | |
A rigid body.
Bodies have position and velocity. You can apply forces, torques, and impulses to bodies. Bodies can be static, kinematic, or dynamic:
Note that a rigid body without a Collider attached to it will not be able to collide with other rigid bodies. This means that it will not generate a collision when it overlaps with another rigid body. A Collider must be attached to the rigid body if you want the body to react to physics (gravity, friction, applied forces, impulses etc...) and also be able to collide with other rigid bodies. In addition a rigid body does not have a shape or size, the shape and the size of the body are derived from the body's Collider. As a result, when debug drawing is enabled, rigid bodies without colliders will not be rendered on the render window
A body is not constructed directly, use the World::createBody function to construct a rigid body
using ime::Body::Callback = std::function<void(Args...)> |
using ime::Body::ConstPtr = std::shared_ptr<const Body> |
using ime::Body::Ptr = std::shared_ptr<Body> |
using ime::Body::WorldPtr = std::shared_ptr<World> |
|
strong |
|
delete |
Copy constructor.
|
noexcept |
Move constructor.
|
override |
Destructor.
void ime::Body::applyAngularImpulse | ( | float | impulse, |
bool | wake = true |
||
) |
Apply an angular impulse.
impulse | The impulse to be applied in kg*m*m/s |
wake | True to wake the body, otherwise false |
By default, This function will wake the body if it is sleeping
Apply a force at a world point.
force | The force to apply in Newtons (N) |
point | The point at which to apply the force |
wake | True to wake the body if sleeping, otherwise false |
If the force is not applied at the centre of mass, it will generate a torque and affect the angular velocity. This wakes up the body
void ime::Body::applyForceToCenter | ( | Vector2f | force, |
bool | wake = true |
||
) |
Apply a force to the centre of mass.
force | The force to be applied in Newton |
wake | True to wake up the body, otherwise false |
This function will force the body to wake up
Apply an impulse at a world point.
impulse | The impulse to be applied in N-s or kg-m/s |
point | The world position to apply the impulse |
wake | True to wake the body otherwise false |
This function will affect the velocity and the angular velocity if the point of application is not at the centre of mass
This function will force the body to wake if it is sleeping
void ime::Body::applyLinearImpulseToCenter | ( | Vector2f | impulse, |
bool | wake = true |
||
) |
Apply an impulse at the centre of mass.
impulse | The impulse to be applied in N-s or kg-m/s |
wake | True to wake the body otherwise false |
By default, This function will wake the body if it is sleeping
void ime::Body::applyTorque | ( | float | torque, |
bool | wake = true |
||
) |
Apply a torque.
torque | The torque to be applied about the z-axis in N-m |
wake | True to wake the body, otherwise false |
This function affects the angular velocity. This function will wake the body if sleeping
void ime::Body::attachCollider | ( | Collider::Ptr | collider | ) |
Attach a collider to the body.
collider | The collider to be attached to the body |
Note that a body may have more than one collider. However a collider can only be attached to one rigid body. All colliders attached to the body are destroyed when the body is destroyed
By default, the body has no collider attached to it
Body::Ptr ime::Body::copy | ( | ) | const |
Create a copy of the rigid body.
|
protectedinherited |
Dispatch an action event.
event | The name of the event to be dispatched |
This function will invoke all event listeners of the specified event. The function should be used for events that represent an action, rather than those that represent a property change (Use emitChange for that)
|
protectedinherited |
Dispatch a property change event.
property | The property that changed |
This function will invoke all the event listeners of the specified property
void ime::Body::forEachCollider | ( | Callback< Collider::Ptr > | callback | ) |
Execute a function for each collider attached to the body.
callback | The function to be executed |
The callback is passed a collider on invocation
float ime::Body::getAngularDamping | ( | ) | const |
Get the angular damping of the body.
float ime::Body::getAngularVelocity | ( | ) | const |
Get the angular velocity.
|
overridevirtual |
|
virtualinherited |
Get the name of the direct parent of an object instance.
In contrast to getClassName which returns the name of the concrete class, this function returns the name of the concrete class's base class. This function is implemented by all derived classes of Object which also serve as base classes for other Objects. For classes whose direct parent is this class, this function will return the name of this class
Reimplemented in ime::SpriteImage, ime::Shape, ime::Drawable, ime::GridMover, ime::Joint, ime::Collider, and ime::GameObject.
Collider::Ptr ime::Body::getColliderById | ( | unsigned int | id | ) |
Get a collider by its id.
id | The unique identifier of the collider |
std::shared_ptr<GameObject> ime::Body::getGameObject | ( | ) |
Get the game object the body is attached to.
By default, the body is not attached to any game object
float ime::Body::getGravityScale | ( | ) | const |
Get the gravity cale of the body.
float ime::Body::getInertia | ( | ) | const |
Get the rotational inertia of the body about the local origin.
float ime::Body::getLinearDamping | ( | ) | const |
Get the linear damping of the body.
Vector2f ime::Body::getLinearVelocity | ( | ) | const |
Get the linear velocity of the centre of mass.
Get the world velocity of a local point.
localPoint | Local point to get world velocity of |
Get the world linear velocity of a world point on the body.
worldPoint | The world point |
Vector2f ime::Body::getLocalCenter | ( | ) | const |
Get the local position of the centre of mass.
Get the local coordinate of a world coordinate.
worldPoint | The world coordinate to be converted to local |
Get the local rotation of a world rotation.
worldVector | The world rotation to get local rotation from |
float ime::Body::getMass | ( | ) | const |
Get the total mass of the body.
The mass of the body is derived from the colliders attached to the body. The more colliders are attached the bigger the mass of the body, likewise the less number of colliders attached, the smaller the mass
By default, the mass is 0
|
inherited |
Get the id of the object.
Each object has a unique id
Vector2f ime::Body::getPosition | ( | ) | const |
Get the world position of the body's origin.
float ime::Body::getRotation | ( | ) | const |
Get the body's world rotation.
|
inherited |
Type ime::Body::getType | ( | ) | const |
Get the body type.
PropertyContainer& ime::Body::getUserData | ( | ) |
Get the user data added to this body.
The user data can be used to store additional information to the body. You can store any type of data in the user date. IME does not use this data and it is sorely available for you to use
WorldPtr ime::Body::getWorld | ( | ) |
Get the physics world the body is in.
Vector2f ime::Body::getWorldCenter | ( | ) | const |
Get the world position of the centre of mass.
Get the world coordinate of a local coordinate.
localPoint | A point on the body measured relative to the body's origin |
Get the world rotation of a local rotation.
localVector | The local rotation to be converted to world rotation |
bool ime::Body::isAwake | ( | ) | const |
Check if the body is awake or sleeping.
bool ime::Body::isEnabled | ( | ) | const |
Check whether or not the body is enabled.
bool ime::Body::isFastBody | ( | ) | const |
Check if the body is a fast moving body or not.
bool ime::Body::isFixedRotation | ( | ) | const |
Check if the body can rotate or not.
bool ime::Body::isSleepingAllowed | ( | ) | const |
Check if the body is allowed to sleep when inactive or not.
Add an event listener to a collision end event.
callback | The function to be executed when event is fired |
The callback function is called when two bodies stop overlapping. It is passed this body and the body that stopped overlapping with this body respectively. Pass nullptr to remove the callback
Add an event listener to a collision begin event.
callback | The function to be executed when event is fired |
The callback function is called when two bodies begin to overlap. It is passed this body and the body that started to overlap with this body respectively. Pass nullptr to remove the callback
|
inherited |
Add a destruction listener.
callback | Function to be executed when the object is destroyed |
Note that an object may have more than one destruction listeners, however, you have to keep the returned id if you may want to remove the callback at a later time
|
inherited |
Add an event listener to an event.
event | The name of the event to add an an event listener to |
callback | The function to be executed when the event takes place |
Unlike onPropertyChange, this function registers event listeners to events that occur when something happens to the object, or when the object does something (action events). Usually the name of the event/action is the name of the function:
Add an event listener to a property change event.
callback | The function to be executed when the property changes |
Note that only one callback function may be registered with this function. This means that adding a new event listener overwrites the previous event listener. To remove the callback, pass a nullptr as an argument. The function may be useful if you want to write the logic for property changes in one function.
|
inherited |
Add an event listener to a specific property change event.
property | The name of the property to listen for |
callback | The function to be executed when the property changes |
A property change event is triggered by any function that begins with set, where the the text after set is the name of the property. For example, for the setTag function, the property that the function modifies is Tag.
Note that when adding a property change event listener, the name of the property must be in lowercase:
Unlike onPropertyChange(const Callback&) you can add multiple event listeners to the same property using this function. However you must store the unique id of the event listener if you wish to remove it at a later time
|
inherited |
Check if two objects are not the same object.
rhs | Object to compare against this object |
Two objects are different from each other if they have different object id's
|
inherited |
Check if two objects are the same object or not.
rhs | Object to compare against this object |
Two objects are the same object if they have the same object id. Recall that each object instance has a unique id
void ime::Body::removeColliderWithId | ( | unsigned int | id | ) |
Remove a collider with a given id from the body.
id | The id of the collider to be removed |
|
inherited |
Remove a destruction listener form the object.
The | id of the destruction listener to be removed |
void ime::Body::setAngularDamping | ( | float | damping | ) |
Set the angular damping.
damping | The new angular damping |
The angular damping is used to reduce the angular velocity. The damping parameter can be larger than 1.0f but the damping effect becomes sensitive to the time step when the damping parameter is large. Units are 1/time
By default, the angular damping is zero
void ime::Body::setAngularVelocity | ( | float | degrees | ) |
Set the angular velocity in degrees per second.
degrees | The new angular velocity |
By default, the angular velocity is 0 degrees per second
void ime::Body::setAwake | ( | bool | awake | ) |
Awake the body or put it to sleep.
awake | True to awake the body or false to put it to sleep |
A sleeping body is not simulated. Note that if a body is awake and collides with a sleeping body, then the sleeping body wakes up. Bodies will also wake up if a joint or contact attached to them is destroyed.
By default, the body is awake
void ime::Body::setEnabled | ( | bool | enable | ) |
Enable or disable a body.
enable | True to enable or false to disable |
A disabled body is not simulated and cannot be collided with or woken up. If you pass a flag of true, all colliders will be added to the broad-phase. If you pass a flag of false, all colliders will be removed from the broad-phase and all contacts will be destroyed. Colliders and joints are otherwise unaffected. You may continue to create/destroy colliders and joints on disabled bodies. Colliders on a disabled body are implicitly disabled and will not participate in collisions, ray-casts, or queries. Joints connected to a disabled body are implicitly disabled. A disabled body is still owned by a world object and remains in the body list.
By default, the body is enabled
void ime::Body::setFastBody | ( | bool | fast | ) |
Set whether or not the body is fast moving.
fast | True to set as fast moving, otherwise false |
When set to true, the body is prevented from tunnelling through other moving bodies (The body is treated like a bullet for continuous collision detection). All bodies are prevented from from tunneling through kinematic and static bodies. This option is only considered for dynamic bodies
By default, the body is NOT a fast body
void ime::Body::setFixedRotation | ( | bool | rotate | ) |
Set whether or not the body can rotate.
rotate | True to allow rotations or false to disallow rotations |
By default, the body can rotate
void ime::Body::setGravityScale | ( | float | scale | ) |
Set the gravity scale of the body.
scale | The gravity scale |
By default, the gravity scale is 1.0f
void ime::Body::setLinearDamping | ( | float | damping | ) |
Set the linear damping of the body.
damping | The new linear damping |
The linear damping is used to reduce the linear velocity. The damping parameter can be larger than 1.0f but the damping effect becomes sensitive to the time step when the damping parameter is large. Units are 1/time
By default, the linear damping is 0.0f
void ime::Body::setLinearVelocity | ( | Vector2f | velocity | ) |
Set the linear velocity of the body in pixels per second.
velocity | The new linear velocity of the centre of mass |
By default, the linear velocity is 0 pixels per second
void ime::Body::setPosition | ( | Vector2f | position | ) |
Set the world position of the body's local origin.
position | The position to set |
By default, the position is (0, 0)
void ime::Body::setRotation | ( | float | angle | ) |
Set the body's rotation about the world origin.
angle | The body's world rotation |
By default, the rotation is 0 degrees
void ime::Body::setSleepingAllowed | ( | bool | sleeps | ) |
Set whether or not this body isSleepingAllowed.
sleeps | True to allow sleep or false to keep the body awake at all times |
By default, the body is allowed to sleep when not in contact with another body or is not in motion
|
inherited |
Assign the object an alias.
name | The alias of the object |
This function is useful if you want to refer to the object by tag instead of its id. Unlike an object id, multiple objects may have the same tag.
By default, the tag is an empty string
void ime::Body::setType | ( | Type | type | ) |
Change the type of the body.
type | The type of this body |
This function may alter the mass and velocity.
|
inherited |
Remove an event listener from an event.
event | The name of the event to remove event listener from |
id | The unique id of the event listener to be removed |