A rigid body. More...
#include <RigidBody.h>
Public Types | |
enum class | Type { Static = 0 , Kinematic , Dynamic } |
The rigid body type. More... | |
using | Ptr = std::unique_ptr< RigidBody > |
Unique body pointer. More... | |
template<typename... Args> | |
using | Callback = std::function< void(Args...)> |
Event listener. More... | |
Public Member Functions | |
RigidBody (const RigidBody &)=delete | |
Copy constructor. More... | |
RigidBody & | operator= (const RigidBody &)=delete |
Copy assignment operator. More... | |
RigidBody (RigidBody &&) noexcept | |
Move constructor. More... | |
RigidBody & | operator= (RigidBody &&) noexcept |
Move assignment operator. More... | |
RigidBody::Ptr | copy () const |
Create a copy of the rigid body. More... | |
std::string | getClassName () const override |
Get the name of this class. More... | |
Collider * | attachCollider (Collider::Ptr collider) |
Attach a collider to the body. More... | |
Collider * | getColliderById (unsigned int id) |
Get a collider by its id. More... | |
const Collider * | getColliderById (unsigned int id) const |
void | removeColliderWithId (unsigned int id) |
Remove a collider with a given id from the body. More... | |
void | setPosition (const 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 (const 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 (const Vector2f &force, const Vector2f &point, bool wake=true) |
Apply a force at a world point. More... | |
void | applyForceToCenter (const 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 (const Vector2f &impulse, const Vector2f &point, bool wake=true) |
Apply an impulse at a world point. More... | |
void | applyLinearImpulseToCenter (const 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 (const Vector2f &worldPoint) const |
Get the local coordinate of a world coordinate. More... | |
Vector2f | getWorldPoint (const Vector2f &localPoint) const |
Get the world coordinate of a local coordinate. More... | |
Vector2f | getLocalRotation (const Vector2f &worldVector) const |
Get the local rotation of a world rotation. More... | |
Vector2f | getWorldRotation (const Vector2f &localVector) const |
Get the world rotation of a local rotation. More... | |
Vector2f | getLinearVelocityFromLocalPoint (const Vector2f &localPoint) const |
Get the world velocity of a local point. More... | |
Vector2f | getLinearVelocityFromWorldPoint (const 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... | |
void | setGameObject (GameObject *gameObject) |
GameObject * | getGameObject () |
Get the game object the body is attached to. More... | |
const GameObject * | getGameObject () const |
PhysicsEngine * | getWorld () |
Get the physics world the body is in. More... | |
const PhysicsEngine * | getWorld () const |
PropertyContainer & | getUserData () |
Get the user data added to this body. More... | |
const PropertyContainer & | getUserData () const |
void | forEachCollider (const Callback< Collider * > &callback) const |
Execute a function for each collider attached to the body. More... | |
std::size_t | getColliderCount () const |
Get the number of colliders attached to the body. More... | |
std::unique_ptr< b2Body, Callback< b2Body * > > & | getInternalBody () |
const std::unique_ptr< b2Body, Callback< b2Body * > > & | getInternalBody () const |
~RigidBody () override | |
Destructor. More... | |
void | setTag (const std::string &tag) |
Assign the object an alias. More... | |
const std::string & | getTag () const |
Get the tag assigned to the object. More... | |
unsigned int | getObjectId () const |
Get the unique 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, bool oneTime=false) |
Add an event listener to a specific property change event. More... | |
int | onPropertyChange (const Callback< Property > &callback, bool oneTime=false) |
Add an event listener to any property change event. More... | |
void | suspendedEventListener (int id, bool suspend) |
Pause or resume execution of an event listener. More... | |
bool | isEventListenerSuspended (int id) const |
Check if an event listener is suspended or not. More... | |
bool | removeEventListener (const std::string &event, int id) |
Remove an event listener from an event. More... | |
bool | removeEventListener (int id) |
Remove an event listener. More... | |
int | onDestruction (const Callback<> &callback) |
Add a destruction listener. More... | |
bool | isSameObjectAs (const Object &other) const |
Check if another object is the same instance as this object. More... | |
Protected Member Functions | |
void | emitChange (const Property &property) |
Dispatch a property change event. More... | |
void | emitDestruction () |
Emit a destruction event. More... | |
Protected Attributes | |
EventEmitter | eventEmitter_ |
Event dispatcher. More... | |
Friends | |
class | PhysicsEngine |
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 PhysicsEngine::createBody function to construct a rigid body
Definition at line 74 of file RigidBody.h.
using ime::RigidBody::Callback = std::function<void(Args...)> |
Event listener.
Definition at line 79 of file RigidBody.h.
using ime::RigidBody::Ptr = std::unique_ptr<RigidBody> |
Unique body pointer.
Definition at line 76 of file RigidBody.h.
|
strong |
The rigid body type.
Definition at line 84 of file RigidBody.h.
|
delete |
Copy constructor.
|
noexcept |
Move constructor.
|
override |
Destructor.
void ime::RigidBody::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
void ime::RigidBody::applyForce | ( | const Vector2f & | force, |
const Vector2f & | point, | ||
bool | wake = true |
||
) |
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::RigidBody::applyForceToCenter | ( | const 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
void ime::RigidBody::applyLinearImpulse | ( | const Vector2f & | impulse, |
const Vector2f & | point, | ||
bool | wake = true |
||
) |
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::RigidBody::applyLinearImpulseToCenter | ( | const 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::RigidBody::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
Collider * ime::RigidBody::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
By default, the body has no collider attached to it
RigidBody::Ptr ime::RigidBody::copy | ( | ) | const |
Create a copy of the rigid body.
|
protectedinherited |
Dispatch a property change event.
property | The property that changed |
This function will invoke all the event listeners of the specified property
|
protectedinherited |
Emit a destruction event.
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::RigidBody::getAngularDamping | ( | ) | const |
Get the angular damping of the body.
float ime::RigidBody::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 ime::Object which also serve as base classes. For classes whose direct parent is this class, this function will return the name of this class
Reimplemented in ime::audio::Audio, ime::GameObject, ime::GridMover, ime::Collider, ime::Joint, ime::Drawable, ime::Shape, and ime::SpriteImage.
Collider * ime::RigidBody::getColliderById | ( | unsigned int | id | ) |
Get a collider by its id.
id | The unique identifier of the collider |
std::size_t ime::RigidBody::getColliderCount | ( | ) | const |
Get the number of colliders attached to the body.
GameObject * ime::RigidBody::getGameObject | ( | ) |
Get the game object the body is attached to.
By default, the body is not attached to any game object
float ime::RigidBody::getGravityScale | ( | ) | const |
Get the gravity cale of the body.
float ime::RigidBody::getInertia | ( | ) | const |
Get the rotational inertia of the body about the local origin.
float ime::RigidBody::getLinearDamping | ( | ) | const |
Get the linear damping of the body.
Vector2f ime::RigidBody::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::RigidBody::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::RigidBody::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 unique id of the object.
Note that each instance of ime::Object has a unique id
Vector2f ime::RigidBody::getPosition | ( | ) | const |
Get the world position of the body's origin.
float ime::RigidBody::getRotation | ( | ) | const |
Get the body's world rotation.
|
inherited |
Type ime::RigidBody::getType | ( | ) | const |
Get the body type.
PropertyContainer & ime::RigidBody::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
PhysicsEngine * ime::RigidBody::getWorld | ( | ) |
Get the physics world the body is in.
Vector2f ime::RigidBody::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::RigidBody::isAwake | ( | ) | const |
Check if the body is awake or sleeping.
bool ime::RigidBody::isEnabled | ( | ) | const |
Check whether or not the body is enabled.
|
inherited |
Check if an event listener is suspended or not.
id | The identification number of the listener to be checked |
This function also returns false if the specified event listener does not exist
bool ime::RigidBody::isFastBody | ( | ) | const |
Check if the body is a fast moving body or not.
bool ime::RigidBody::isFixedRotation | ( | ) | const |
Check if the body can rotate or not.
|
inherited |
Check if another object is the same instance as this object.
other | The object to compare against this object |
bool ime::RigidBody::isSleepingAllowed | ( | ) | const |
Check if the body is allowed to sleep when inactive or not.
|
inherited |
Add a destruction listener.
callback | Function to be executed when the object is destroyed |
The destruction listener is called when the object reaches the end of its lifetime. Note that an object may have multiple destruction listeners registered to it
|
inherited |
Add an event listener to any property change event.
callback | The function to be executed when any property changes |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
When unsubscribing the any property change event handler, you must pass "propertyChange" as the name of the event
|
inherited |
Add an event listener to a specific property change event.
property | The name of the property to add an event listener to |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
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, the setTag() function, modifies the tag property of the object, thus will generate a "tag" change event each time it is called
Note that multiple event listeners may be registered to the same property change event. In addition, when adding a property change event listener, the name of the property must be in lowercase.
void ime::RigidBody::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 an event listener from an event.
event | The name of the event to remove an event listener from |
id | The unique id of the event listener to be removed |
|
inherited |
Remove an event listener.
id | The id of the event listener to be removed |
void ime::RigidBody::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::RigidBody::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::RigidBody::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::RigidBody::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::RigidBody::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::RigidBody::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::RigidBody::setGravityScale | ( | float | scale | ) |
Set the gravity scale of the body.
scale | The gravity scale |
By default, the gravity scale is 1.0f
void ime::RigidBody::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::RigidBody::setLinearVelocity | ( | const 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::RigidBody::setPosition | ( | const 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::RigidBody::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::RigidBody::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.
tag | The alias of the object |
This function is useful if you want to refer to the object by a tag instead of its object id. Unlike an object id, multiple objects may have the same tag
By default, the tag is an empty string
void ime::RigidBody::setType | ( | Type | type | ) |
Change the type of the body.
type | The type of this body |
This function may alter the mass and velocity.
|
inherited |
Pause or resume execution of an event listener.
id | The event listeners unique identification number |
suspend | True to suspend/pause or false to unsuspend/resume |
|
friend |
Needs access to constructor.
Definition at line 622 of file RigidBody.h.
|
protectedinherited |