Public Types | Public Member Functions | Protected Member Functions | Friends | List of all members
ime::Body Class Reference

A rigid body. More...

#include <Body.h>

Inheritance diagram for ime::Body:
ime::Object

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...
 
Bodyoperator= (const Body &)=delete
 Copy assignment operator. More...
 
 Body (Body &&) noexcept
 Move constructor. More...
 
Bodyoperator= (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< GameObjectgetGameObject ()
 Get the game object the body is attached to. More...
 
std::shared_ptr< GameObjectgetGameObject () const
 
WorldPtr getWorld ()
 Get the physics world the body is in. More...
 
const WorldPtrgetWorld () const
 
PropertyContainergetUserData ()
 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...
 

Detailed Description

A rigid body.

Bodies have position and velocity. You can apply forces, torques, and impulses to bodies. Bodies can be static, kinematic, or dynamic:

  1. A static body does not move under simulation and behaves as if it has infinite mass.
  2. A kinematic body moves under simulation according to its velocity. Kinematic bodies do not respond to forces
  3. A dynamic body is fully simulated. They can be moved manually by the user, but normally they move according to forces

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

Definition at line 74 of file Body.h.

Member Typedef Documentation

◆ Callback

template<typename... Args>
using ime::Body::Callback = std::function<void(Args...)>

Event listener.

Definition at line 81 of file Body.h.

◆ ConstPtr

using ime::Body::ConstPtr = std::shared_ptr<const Body>

Const shared world pointer.

Definition at line 77 of file Body.h.

◆ Ptr

using ime::Body::Ptr = std::shared_ptr<Body>

Shared Body pointer.

Definition at line 76 of file Body.h.

◆ WorldPtr

using ime::Body::WorldPtr = std::shared_ptr<World>

Shared World pointer.

Definition at line 78 of file Body.h.

Member Enumeration Documentation

◆ Type

enum ime::Body::Type
strong

The rigid body type.

Enumerator
Static 

Zero mass, zero velocity, may be moved manually.

Kinematic 

Zero mass, non-zero velocity set by user, moved by physics engine.

Dynamic 

Positive mass, non-zero velocity determined by forces, moved by physics engine.

Definition at line 86 of file Body.h.

Constructor & Destructor Documentation

◆ Body() [1/2]

ime::Body::Body ( const Body )
delete

Copy constructor.

◆ Body() [2/2]

ime::Body::Body ( Body &&  )
noexcept

Move constructor.

◆ ~Body()

ime::Body::~Body ( )
override

Destructor.

Member Function Documentation

◆ applyAngularImpulse()

void ime::Body::applyAngularImpulse ( float  impulse,
bool  wake = true 
)

Apply an angular impulse.

Parameters
impulseThe impulse to be applied in kg*m*m/s
wakeTrue to wake the body, otherwise false

By default, This function will wake the body if it is sleeping

◆ applyForce()

void ime::Body::applyForce ( Vector2f  force,
Vector2f  point,
bool  wake = true 
)

Apply a force at a world point.

Parameters
forceThe force to apply in Newtons (N)
pointThe point at which to apply the force
wakeTrue 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

◆ applyForceToCenter()

void ime::Body::applyForceToCenter ( Vector2f  force,
bool  wake = true 
)

Apply a force to the centre of mass.

Parameters
forceThe force to be applied in Newton
wakeTrue to wake up the body, otherwise false

This function will force the body to wake up

◆ applyLinearImpulse()

void ime::Body::applyLinearImpulse ( Vector2f  impulse,
Vector2f  point,
bool  wake = true 
)

Apply an impulse at a world point.

Parameters
impulseThe impulse to be applied in N-s or kg-m/s
pointThe world position to apply the impulse
wakeTrue 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

◆ applyLinearImpulseToCenter()

void ime::Body::applyLinearImpulseToCenter ( Vector2f  impulse,
bool  wake = true 
)

Apply an impulse at the centre of mass.

Parameters
impulseThe impulse to be applied in N-s or kg-m/s
wakeTrue to wake the body otherwise false

By default, This function will wake the body if it is sleeping

◆ applyTorque()

void ime::Body::applyTorque ( float  torque,
bool  wake = true 
)

Apply a torque.

Parameters
torqueThe torque to be applied about the z-axis in N-m
wakeTrue to wake the body, otherwise false

This function affects the angular velocity. This function will wake the body if sleeping

◆ attachCollider()

void ime::Body::attachCollider ( Collider::Ptr  collider)

Attach a collider to the body.

Parameters
colliderThe 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

Warning
This function is locked during callbacks

◆ copy()

Body::Ptr ime::Body::copy ( ) const

Create a copy of the rigid body.

Returns
The new rigid body
Note
The user data of this object will not be copied and the copy will not be attached to a game object

◆ emit()

void ime::Object::emit ( const std::string &  event)
protectedinherited

Dispatch an action event.

Parameters
eventThe 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)

See also
emitChange

◆ emitChange()

void ime::Object::emitChange ( const Property property)
protectedinherited

Dispatch a property change event.

Parameters
propertyThe property that changed

This function will invoke all the event listeners of the specified property

See also
emit

◆ forEachCollider()

void ime::Body::forEachCollider ( Callback< Collider::Ptr callback)

Execute a function for each collider attached to the body.

Parameters
callbackThe function to be executed

The callback is passed a collider on invocation

◆ getAngularDamping()

float ime::Body::getAngularDamping ( ) const

Get the angular damping of the body.

Returns
The angular damping of the body

◆ getAngularVelocity()

float ime::Body::getAngularVelocity ( ) const

Get the angular velocity.

Returns
The new angular velocity

◆ getClassName()

std::string ime::Body::getClassName ( ) const
overridevirtual

Get the name of this class.

Returns
The name of this class

Implements ime::Object.

◆ getClassType()

virtual std::string ime::Object::getClassType ( ) const
virtualinherited

Get the name of the direct parent of an object instance.

Returns
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

auto rectangle = ime::RectangleShape(); // RectangleShape is derived from Shape
std::cout << rectangle->getClassName(); // Prints "RectangleShape"
std::cout << rectangle->getClassType(); // Prints "Shape"
See also
getClassName

Reimplemented in ime::SpriteImage, ime::Shape, ime::Drawable, ime::GridMover, ime::Joint, ime::Collider, and ime::GameObject.

◆ getColliderById()

Collider::Ptr ime::Body::getColliderById ( unsigned int  id)

Get a collider by its id.

Parameters
idThe unique identifier of the collider
Returns
The collider with the given id or a nullptr if the body does not have a collider with the given id attached to it
Warning
This function is locked during callbacks

◆ getGameObject()

std::shared_ptr<GameObject> ime::Body::getGameObject ( )

Get the game object the body is attached to.

Returns
The game object this body is attached to or a nullptr if teh body is not attached to any game object

By default, the body is not attached to any game object

◆ getGravityScale()

float ime::Body::getGravityScale ( ) const

Get the gravity cale of the body.

Returns
The gravity scale of the body

◆ getInertia()

float ime::Body::getInertia ( ) const

Get the rotational inertia of the body about the local origin.

Returns
The rotational inertia in kg-m^2

◆ getLinearDamping()

float ime::Body::getLinearDamping ( ) const

Get the linear damping of the body.

Returns
The linear damping of the body

◆ getLinearVelocity()

Vector2f ime::Body::getLinearVelocity ( ) const

Get the linear velocity of the centre of mass.

Returns
The linear velocity of the center of mass.

◆ getLinearVelocityFromLocalPoint()

Vector2f ime::Body::getLinearVelocityFromLocalPoint ( Vector2f  localPoint) const

Get the world velocity of a local point.

Parameters
localPointLocal point to get world velocity of
Returns
The world velocity of a point

◆ getLinearVelocityFromWorldPoint()

Vector2f ime::Body::getLinearVelocityFromWorldPoint ( Vector2f  worldPoint) const

Get the world linear velocity of a world point on the body.

Parameters
worldPointThe world point
Returns
The world velocity of a point

◆ getLocalCenter()

Vector2f ime::Body::getLocalCenter ( ) const

Get the local position of the centre of mass.

Returns
The local position of the centre of mass

◆ getLocalPoint()

Vector2f ime::Body::getLocalPoint ( Vector2f  worldPoint) const

Get the local coordinate of a world coordinate.

Parameters
worldPointThe world coordinate to be converted to local
Returns
The given world coordinate relative to the body's origin

◆ getLocalRotation()

Vector2f ime::Body::getLocalRotation ( Vector2f  worldVector) const

Get the local rotation of a world rotation.

Parameters
worldVectorThe world rotation to get local rotation from
Returns
worldVector in local rotation

◆ getMass()

float ime::Body::getMass ( ) const

Get the total mass of the body.

Returns
The mass of the body in kilograms (Kg)

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

◆ getObjectId()

unsigned int ime::Object::getObjectId ( ) const
inherited

Get the id of the object.

Returns
The id of the object

Each object has a unique id

◆ getPosition()

Vector2f ime::Body::getPosition ( ) const

Get the world position of the body's origin.

Returns
The world position of the body's origin

◆ getRotation()

float ime::Body::getRotation ( ) const

Get the body's world rotation.

Returns
The current world rotation of the body

◆ getTag()

const std::string& ime::Object::getTag ( ) const
inherited

Get the alias of the object.

Returns
The alias of the object
See also
setTag and getObjectId

◆ getType()

Type ime::Body::getType ( ) const

Get the body type.

Returns
The type of this body

◆ getUserData()

PropertyContainer& ime::Body::getUserData ( )

Get the user data added to this body.

Returns
The user data

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

◆ getWorld()

WorldPtr ime::Body::getWorld ( )

Get the physics world the body is in.

Returns
The physics world the body is simulated in

◆ getWorldCenter()

Vector2f ime::Body::getWorldCenter ( ) const

Get the world position of the centre of mass.

Returns
The world position centre position

◆ getWorldPoint()

Vector2f ime::Body::getWorldPoint ( Vector2f  localPoint) const

Get the world coordinate of a local coordinate.

Parameters
localPointA point on the body measured relative to the body's origin
Returns
The given point in world coordinates

◆ getWorldRotation()

Vector2f ime::Body::getWorldRotation ( Vector2f  localVector) const

Get the world rotation of a local rotation.

Parameters
localVectorThe local rotation to be converted to world rotation
Returns
The given local rotation in world rotation

◆ isAwake()

bool ime::Body::isAwake ( ) const

Check if the body is awake or sleeping.

Returns
True if awake, otherwise false

◆ isEnabled()

bool ime::Body::isEnabled ( ) const

Check whether or not the body is enabled.

Returns
True if enabled or false if disabled

◆ isFastBody()

bool ime::Body::isFastBody ( ) const

Check if the body is a fast moving body or not.

Returns
True if body is a fast moving body, otherwise false
See also
setFastBody

◆ isFixedRotation()

bool ime::Body::isFixedRotation ( ) const

Check if the body can rotate or not.

Returns
True if the body cn rotate, otherwise false
See also
setFixedRotation

◆ isSleepingAllowed()

bool ime::Body::isSleepingAllowed ( ) const

Check if the body is allowed to sleep when inactive or not.

Returns
True if the body sleeps or false if the body never sleeps
See also
setSleepingAllowed

◆ onCollisionEnd()

void ime::Body::onCollisionEnd ( Callback< Body::Ptr, Body::Ptr callback)

Add an event listener to a collision end event.

Parameters
callbackThe 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

Note
A collision end event can only occur if the body has a Collider attached to it
See also
setCollidable
onCollisionStart

◆ onCollisionStart()

void ime::Body::onCollisionStart ( Callback< Body::Ptr, Body::Ptr callback)

Add an event listener to a collision begin event.

Parameters
callbackThe 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

Note
A collision begin event can only occur if the body has a Collider attached to it
See also
setCollidable
onCollisionEnd

◆ onDestruction()

int ime::Object::onDestruction ( const Callback<> &  callback)
inherited

Add a destruction listener.

Parameters
callbackFunction to be executed when the object is destroyed
Returns
The id of the destruction listener

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

Warning
The callback is called when the object is destroyed. Do not try to access the object in the callback. Doing so is undefined behavior
See also
removeDestructionListener

◆ onEvent()

int ime::Object::onEvent ( const std::string &  event,
const Callback<> &  callback 
)
inherited

Add an event listener to an event.

Parameters
eventThe name of the event to add an an event listener to
callbackThe function to be executed when the event takes place
Returns
The unique identification number of the event listener

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 event listeners to the object
object.onEvent("attachRigidBody", [] {
std::cout << "Rigid body attached to object << std::endl;
});
object.onEvent("removeRigidBody", [] {
std::cout << "Rigid body removed from object << std::endl;
});
// Invokes event listener(s)
object.attachRigidBody(body);
object.removeRigidBody();
See also
onPropertyChange and unsubscribe

◆ onPropertyChange() [1/2]

void ime::Object::onPropertyChange ( const Callback< Property > &  callback)
inherited

Add an event listener to a property change event.

Parameters
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

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.

See also
onPropertyChange(std::string, Callback)

◆ onPropertyChange() [2/2]

int ime::Object::onPropertyChange ( const std::string &  property,
const Callback< Property > &  callback 
)
inherited

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to listen for
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

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:

// Prints the tag of the player object to the console everytime it changes
player.onPropertyChange("tag", [](const Property& property) {
cout << "New tag: " << property.getValue<std::string>() << endl;
});
...
//Sets tag = "player1" and invokes event listener(s)
player.setTag("player1");

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

See also
unsubscribe and onPropertyChange(Callback)

◆ operator!=()

bool ime::Object::operator!= ( const Object rhs) const
inherited

Check if two objects are not the same object.

Parameters
rhsObject to compare against this object
Returns
True if rhs is NOT the same object as this object, otherwise false

Two objects are different from each other if they have different object id's

See also
getObjectId and operator==

◆ operator=() [1/2]

Body& ime::Body::operator= ( Body &&  )
noexcept

Move assignment operator.

◆ operator=() [2/2]

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

Copy assignment operator.

◆ operator==()

bool ime::Object::operator== ( const Object rhs) const
inherited

Check if two objects are the same object or not.

Parameters
rhsObject to compare against this object
Returns
True if rhs is the same object as this object, otherwise false

Two objects are the same object if they have the same object id. Recall that each object instance has a unique id

See also
getObjectId and operator!=

◆ removeColliderWithId()

void ime::Body::removeColliderWithId ( unsigned int  id)

Remove a collider with a given id from the body.

Parameters
idThe id of the collider to be removed
  • The mass of the body will be adjusted if the body is dynamic and the collider has a positive density
Note
All colliders attached to a body are destroyed when the body is destroyed
Warning
This function is locked during world callbacks

◆ removeDestructionListener()

bool ime::Object::removeDestructionListener ( int  id)
inherited

Remove a destruction listener form the object.

Parameters
Theid of the destruction listener to be removed
Returns
True if the destruction listener was removed or false if the listener with the given id does not exist

◆ setAngularDamping()

void ime::Body::setAngularDamping ( float  damping)

Set the angular damping.

Parameters
dampingThe 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

◆ setAngularVelocity()

void ime::Body::setAngularVelocity ( float  degrees)

Set the angular velocity in degrees per second.

Parameters
degreesThe new angular velocity

By default, the angular velocity is 0 degrees per second

◆ setAwake()

void ime::Body::setAwake ( bool  awake)

Awake the body or put it to sleep.

Parameters
awakeTrue 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

◆ setEnabled()

void ime::Body::setEnabled ( bool  enable)

Enable or disable a body.

Parameters
enableTrue 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.

Warning
Enabling a disabled body is almost as expensive as creating the body from scratch, so use this function sparingly

By default, the body is enabled

Warning
This function is locked during callbacks

◆ setFastBody()

void ime::Body::setFastBody ( bool  fast)

Set whether or not the body is fast moving.

Parameters
fastTrue 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

Warning
Fast bodies increases processing time and hence decreases performance. Therefore, you should only set the body as a fast body if it is indeed a fast body, such as a bullet

◆ setFixedRotation()

void ime::Body::setFixedRotation ( bool  rotate)

Set whether or not the body can rotate.

Parameters
rotateTrue to allow rotations or false to disallow rotations

By default, the body can rotate

◆ setGravityScale()

void ime::Body::setGravityScale ( float  scale)

Set the gravity scale of the body.

Parameters
scaleThe gravity scale

By default, the gravity scale is 1.0f

◆ setLinearDamping()

void ime::Body::setLinearDamping ( float  damping)

Set the linear damping of the body.

Parameters
dampingThe 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

◆ setLinearVelocity()

void ime::Body::setLinearVelocity ( Vector2f  velocity)

Set the linear velocity of the body in pixels per second.

Parameters
velocityThe new linear velocity of the centre of mass

By default, the linear velocity is 0 pixels per second

◆ setPosition()

void ime::Body::setPosition ( Vector2f  position)

Set the world position of the body's local origin.

Parameters
positionThe position to set

By default, the position is (0, 0)

◆ setRotation()

void ime::Body::setRotation ( float  angle)

Set the body's rotation about the world origin.

Parameters
angleThe body's world rotation

By default, the rotation is 0 degrees

◆ setSleepingAllowed()

void ime::Body::setSleepingAllowed ( bool  sleeps)

Set whether or not this body isSleepingAllowed.

Parameters
sleepsTrue 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

Note
Setting the body to never sleep increases CPU usage

◆ setTag()

void ime::Object::setTag ( const std::string &  tag)
inherited

Assign the object an alias.

Parameters
nameThe 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

◆ setType()

void ime::Body::setType ( Type  type)

Change the type of the body.

Parameters
typeThe type of this body

This function may alter the mass and velocity.

Warning
This function is locked during callbacks

◆ unsubscribe()

bool ime::Object::unsubscribe ( const std::string &  event,
int  id 
)
inherited

Remove an event listener from an event.

Parameters
eventThe name of the event to remove event listener from
idThe unique id of the event listener to be removed
Returns
True if the event listener was removed or false if the event or the event listener is does not exist
// Display the name of the object to console every time it changes
auto nameChangeId = object.onPropertyChange("name", [](Property name) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the name of the object when it changes
object.unsubscribe("name", id);

Friends And Related Function Documentation

◆ World

friend class World
friend

Needs access to constructor.

Definition at line 645 of file Body.h.


The documentation for this class was generated from the following file:
ime::RectangleShape
A 2D shape having four sides and four corners (90 degree angles)
Definition: RectangleShape.h:36