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

Solid convex polygon collider. More...

#include <PolygonCollider.h>

Inheritance diagram for ime::PolygonCollider:
ime::Collider ime::Object

Public Types

using Ptr = std::shared_ptr< PolygonCollider >
 Shared polygon collider. More...
 
enum  Type { Type::Circle, Type::Box, Type::Polygon, Type::Edge }
 The type of the collider. More...
 
using BodyPtr = std::shared_ptr< Body >
 Shared Body pointer. More...
 

Public Member Functions

 PolygonCollider ()
 Default constructor. More...
 
 PolygonCollider (PolygonCollider &&) noexcept
 Move constructor. More...
 
PolygonCollideroperator= (PolygonCollider &&) noexcept
 Move assignment operator. More...
 
PolygonCollider::Ptr copy () const
 Create a copy of the collider. More...
 
Collider::Ptr clone () const override
 Create a copy of the collider. More...
 
std::string getClassName () const override
 Get the name of this class. More...
 
void set (const std::vector< Vector2f > &vertices)
 Create a convex hull from the an array of local points. More...
 
 ~PolygonCollider () override
 Destructor. More...
 
std::string getClassType () const override
 Get the name of this class. More...
 
Type getType () const
 Get the type of the collider. More...
 
void setDensity (float density)
 Set the density of the collider. More...
 
float getDensity () const
 Get the density of the collider. More...
 
void setFriction (float friction)
 Set the coefficient of friction. More...
 
float getFriction () const
 Get the coefficient of friction. More...
 
void setRestitution (float restitution)
 Set the coefficient of restitution. More...
 
float getRestitution () const
 Get the coefficient of restitution of the collider. More...
 
void setRestitutionThreshold (float threshold)
 Set the restitution threshold of the collider. More...
 
float getRestitutionThreshold () const
 Get the restitution velocity threshold of the collider. More...
 
void setSensor (bool sensor)
 Set if the collider is a sensor or not. More...
 
bool isSensor () const
 Check whether or not the collider is a sensor. More...
 
void setCollisionFilter (const CollisionFilterData &filterData)
 Set the collision filter data of the collider. More...
 
const CollisionFilterDatagetCollisionFilterData () const
 Get the collision filter data for the collider. More...
 
void resetCollisionFilterData ()
 Reset the collision filtering data to default. More...
 
void setEnable (bool enable)
 Enable or disable the collider. More...
 
BodyPtr getBody ()
 Get the body the collider is attached to. More...
 
const BodyPtrgetBody () const
 
bool containsPoint (Vector2f point) const
 Check if the collider contains a point or not. More...
 
PropertyContainergetUserData ()
 Get the user data extracted from the collider definition. 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...
 
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...
 

Static Public Member Functions

static PolygonCollider::Ptr create ()
 Create a new polygon collider. 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...
 

Detailed Description

Solid convex polygon collider.

A polygon is convex when all line segments connecting two points in t he interior do not cross any edge of the polygon. Polygons are solid and never hollow. A polygon must have 3 or more vertices

Definition at line 44 of file PolygonCollider.h.

Member Typedef Documentation

◆ BodyPtr

using ime::Collider::BodyPtr = std::shared_ptr<Body>
inherited

Shared Body pointer.

Definition at line 121 of file Collider.h.

◆ Ptr

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

Shared polygon collider.

Definition at line 46 of file PolygonCollider.h.

Member Enumeration Documentation

◆ Type

enum ime::Collider::Type
stronginherited

The type of the collider.

Enumerator
Circle 

Circle collider.

Box 

Box collider.

Polygon 

Polygon collider.

Edge 

Edge collider.

Definition at line 126 of file Collider.h.

Constructor & Destructor Documentation

◆ PolygonCollider() [1/2]

ime::PolygonCollider::PolygonCollider ( )

Default constructor.

◆ PolygonCollider() [2/2]

ime::PolygonCollider::PolygonCollider ( PolygonCollider &&  )
noexcept

Move constructor.

◆ ~PolygonCollider()

ime::PolygonCollider::~PolygonCollider ( )
override

Destructor.

Member Function Documentation

◆ clone()

Collider::Ptr ime::PolygonCollider::clone ( ) const
overridevirtual

Create a copy of the collider.

Returns
A new polygon collider

You should use this function if you don't care about the type of the collider, otherwise use the colliders copy function

Warning
You must attach a rigid Body to the returned collider before using it. Failure to do so is undefined behavior
See also
copy

Implements ime::Collider.

◆ containsPoint()

bool ime::Collider::containsPoint ( Vector2f  point) const
inherited

Check if the collider contains a point or not.

Parameters
pointThe point to be checked in world coordinates
Returns
True if the collider contains the point, otherwise false

◆ copy()

PolygonCollider::Ptr ime::PolygonCollider::copy ( ) const

Create a copy of the collider.

Returns
A new polygon collider
Warning
You must attach a rigid Body to the returned collider before using it. Failure to do so is undefined behavior
See also
clone

◆ create()

static PolygonCollider::Ptr ime::PolygonCollider::create ( )
static

Create a new polygon collider.

Returns
The created polygon collider

This function is just a helper function for creating a shared pointer to a polygon collider. The class does not keep the pointer

◆ 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

◆ getBody()

BodyPtr ime::Collider::getBody ( )
inherited

Get the body the collider is attached to.

Returns
The body this collider is attached to if any, otherwise a nullptr

◆ getClassName()

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

Get the name of this class.

Returns
The name of this class

Implements ime::Object.

◆ getClassType()

std::string ime::Collider::getClassType ( ) const
overridevirtualinherited

Get the name of this class.

Returns
The name of this class

Note that this function is only implemented by child classes of Object which also serve as a base class for other classes

See also
Object::getClassType and Object::getClassName

Reimplemented from ime::Object.

◆ getCollisionFilterData()

const CollisionFilterData& ime::Collider::getCollisionFilterData ( ) const
inherited

Get the collision filter data for the collider.

Returns
The collision filter data of the collider

◆ getDensity()

float ime::Collider::getDensity ( ) const
inherited

Get the density of the collider.

Returns
The density of the collider

◆ getFriction()

float ime::Collider::getFriction ( ) const
inherited

Get the coefficient of friction.

Returns
The coefficient of friction

◆ 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

◆ getRestitution()

float ime::Collider::getRestitution ( ) const
inherited

Get the coefficient of restitution of the collider.

Returns
The coefficient of restitution of the collider

◆ getRestitutionThreshold()

float ime::Collider::getRestitutionThreshold ( ) const
inherited

Get the restitution velocity threshold of the collider.

Returns
The restitution velocity

◆ 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::Collider::getType ( ) const
inherited

Get the type of the collider.

Returns
The type of the collider

◆ getUserData()

PropertyContainer& ime::Collider::getUserData ( )
inherited

Get the user data extracted from the collider definition.

Returns
The user data

◆ isSensor()

bool ime::Collider::isSensor ( ) const
inherited

Check whether or not the collider is a sensor.

Returns
True if the collider is a sensor, otherwise false

◆ 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=()

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

Move 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!=

◆ 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

◆ resetCollisionFilterData()

void ime::Collider::resetCollisionFilterData ( )
inherited

Reset the collision filtering data to default.

By default, the collider does not belong to any collision group and the rigid body the collider is attached to will collide with any rigid body (that is also collidable) it comes into contact with

◆ set()

void ime::PolygonCollider::set ( const std::vector< Vector2f > &  vertices)

Create a convex hull from the an array of local points.

Parameters
verticesThe vertices to create the convex hull from

The origin of the polygon is at the local coordinate (0, 0)

Warning
The maximum number of vertices allowed is 8
The points may be reordered, even if they form a convex polygon. Collinear points are handled but not removed. Collinear points may lead to poor stacking behaviour

◆ setCollisionFilter()

void ime::Collider::setCollisionFilter ( const CollisionFilterData filterData)
inherited

Set the collision filter data of the collider.

Parameters
filterDataThe new collision filter data

This function will not update contacts until the next time step when either parent body is active or awake

By default, the collider does not belong to any collision group and the rigid body the collider is attached to will collide with any rigid body (that is also collidable) it comes into contact with

◆ setDensity()

void ime::Collider::setDensity ( float  density)
inherited

Set the density of the collider.

Parameters
densityThe density to set

The colliders density is used to compute the mass properties of the rigid body. The density can be zero or positive. You should use similar densities for all of your colliders. This will improve stacking stability

By default, the density is 1.0f

◆ setEnable()

void ime::Collider::setEnable ( bool  enable)
inherited

Enable or disable the collider.

Parameters
enableTrue to enable or false to disable

This function will only modify the collision bitmask and leave the category bit and group index as is. When enable is set to false, the collision bitmask will be set to 0 causing the collider to not collide with any other collider and when enable is set to true, the collision bitmask will be set to the value it was before it was set to zero.

This function is a just a shortcut for:

// When set this way, you will also have to save/remember the previous
// collision bitmask value if you wish to restore it and not set a new
// one
auto filterData = collider.getCollisionFilterData();
filterData.collisionBitMask = 0;
collider.setCollisionFilter(filterData);

By default, the collider is enabled

See also
setCollisionFilter

◆ setFriction()

void ime::Collider::setFriction ( float  friction)
inherited

Set the coefficient of friction.

Parameters
frictionThe coefficient to set

Friction is used to make objects slide along each other realistically The friction parameter is usually set between 0 and 1, but can be any non-negative value. A friction value of 0 turns off friction and a value of 1 makes the friction strong

Note
This will not change the coefficient of existing contacts

By default, the friction is 0.2f

◆ setRestitution()

void ime::Collider::setRestitution ( float  restitution)
inherited

Set the coefficient of restitution.

Parameters
restitutionThe coefficient to set

Restitution is used to make objects bounce. The restitution value is usually set to be between 0 and 1. Consider dropping a ball on a table. A value of zero means the ball won't bounce. This is called an inelastic collision. A value of one means the ball's velocity will be exactly reflected. This is called a perfectly elastic collision

Note
This will not change the restitution of existing contacts

By default, the restitution is 0.0f

◆ setRestitutionThreshold()

void ime::Collider::setRestitutionThreshold ( float  threshold)
inherited

Set the restitution threshold of the collider.

Parameters
thresholdThe new restitution threshold
Note
This will not change the restitution threshold of existing contacts

By default, the restitution threshold is 1.0f

◆ setSensor()

void ime::Collider::setSensor ( bool  sensor)
inherited

Set if the collider is a sensor or not.

Parameters
sensorTrue to set as sensor, otherwise false

A sensor detects a collision but does not generate a collision response. This is useful if you only want to know when two colliders overlap. You can flag any collider as being a sensor. Sensors may be attached to static, kinematic, or dynamic bodies. Remember that you may have multiple colliders per body and you can have any mix of sensors and non-sensor colliders. Also, sensors only form contacts when at least one body is dynamic, so you will not get a contact for kinematic versus kinematic, kinematic versus static, or static versus static

By default, the collider is not a sensor

◆ 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

◆ 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);

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