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

A circle circle. More...

#include <CircleCollider.h>

Inheritance diagram for ime::CircleCollider:
ime::Collider ime::Object

Public Types

using Ptr = std::unique_ptr< CircleCollider >
 Unique collider pointer. More...
 
enum class  Type { Circle , Box , Polygon , Edge }
 The type of the collider. More...
 
using CollisionCallback = std::function< void(Collider *, Collider *)>
 RigidBody collision callback. More...
 

Public Member Functions

 CircleCollider (float radius=0.0f)
 Default constructor. More...
 
 CircleCollider (CircleCollider &&) noexcept
 Move constructor. More...
 
CircleCollideroperator= (CircleCollider &&) noexcept
 Move assignment operator. More...
 
CircleCollider::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 setPosition (const Vector2f &position)
 Set the position of the circle. More...
 
Vector2f getPosition () const
 Get the position of the circle. More...
 
void setRadius (float radius)
 Set the radius of the circle. More...
 
float getRadius () const
 Get the radius of the circle. More...
 
 ~CircleCollider () 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 whether or not the collider acts as a sensor. 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...
 
bool isAttachedToBody () const
 Check if the collider is attached to a rigid body or not. More...
 
RigidBodygetBody ()
 Get the body the collider is attached to. More...
 
const RigidBodygetBody () const
 
bool containsPoint (const Vector2f &point) const
 Check if the collider contains a point or not. More...
 
PropertyContainergetUserData ()
 Get the user data extracted from the collider definition. More...
 
const PropertyContainergetUserData () const
 
void onContactBegin (const CollisionCallback &callback)
 Add an event listener to a collision start event. More...
 
void onContactEnd (const CollisionCallback &callback)
 Add an event listener to a collision end event. More...
 
void onContactStay (const CollisionCallback &callback)
 Add an event listener to a contact stay event. More...
 
void emitContact (const std::string &event, Collider *other)
 
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...
 
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...
 

Static Public Member Functions

static CircleCollider::Ptr create (float radius=0.0f)
 Create a new circle collider. 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...
 

Detailed Description

A circle circle.

Circle colliders have a position and radius. Circles are solid. You cannot make a hollow circle using the circle collider. The origin of a circle collider is always at the local centre of the circle

Definition at line 44 of file CircleCollider.h.

Member Typedef Documentation

◆ CollisionCallback

using ime::Collider::CollisionCallback = std::function<void(Collider*, Collider*)>
inherited

RigidBody collision callback.

Definition at line 122 of file Collider.h.

◆ Ptr

using ime::CircleCollider::Ptr = std::unique_ptr<CircleCollider>

Unique collider pointer.

Definition at line 46 of file CircleCollider.h.

Member Enumeration Documentation

◆ Type

enum class 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 127 of file Collider.h.

Constructor & Destructor Documentation

◆ CircleCollider() [1/2]

ime::CircleCollider::CircleCollider ( float  radius = 0.0f)
explicit

Default constructor.

Parameters
radiusThe radius of the circle

By default, the circle radius is 0.0f

◆ CircleCollider() [2/2]

ime::CircleCollider::CircleCollider ( CircleCollider &&  )
noexcept

Move constructor.

◆ ~CircleCollider()

ime::CircleCollider::~CircleCollider ( )
override

Destructor.

Member Function Documentation

◆ clone()

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

Create a copy of the collider.

Returns
A new circle 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 ( const 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()

CircleCollider::Ptr ime::CircleCollider::copy ( ) const

Create a copy of the collider.

Returns
A new circle 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 CircleCollider::Ptr ime::CircleCollider::create ( float  radius = 0.0f)
static

Create a new circle collider.

Parameters
radiusThe radius of the circle
Returns
The created circle collider

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

By default, the circle radius is 0.0f

◆ 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

◆ emitDestruction()

void ime::Object::emitDestruction ( )
protectedinherited

Emit a destruction event.

Note
This function must be the first statement in the definition of a destructor to avoid undefined behavior. In addition, note that destruction listeners are invoked once. Therefore, multiple classes in a hierarchy may call this function but the class that makes the call first will be the one that invokes the destruction listeners

◆ getBody()

RigidBody * 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::CircleCollider::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 unique id of the object.

Returns
The unique id of the object

Note that each instance of ime::Object has a unique id

See also
setTag

◆ getPosition()

Vector2f ime::CircleCollider::getPosition ( ) const

Get the position of the circle.

Returns
The position of the circle

◆ getRadius()

float ime::CircleCollider::getRadius ( ) const

Get the radius of the circle.

Returns
The radius of the circle

◆ 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 tag assigned to the object.

Returns
The tag of the object
See also
setTag

◆ 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

◆ isAttachedToBody()

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

Check if the collider is attached to a rigid body or not.

Returns
True if it is attached to a body, otherwise false
Note
A collider can only be attached to a single rigid Body

◆ isEventListenerSuspended()

bool ime::Object::isEventListenerSuspended ( int  id) const
inherited

Check if an event listener is suspended or not.

Parameters
idThe identification number of the listener to be checked
Returns
True if suspended, otherwise false

This function also returns false if the specified event listener does not exist

See also
suspendedEventListener

◆ isSameObjectAs()

bool ime::Object::isSameObjectAs ( const Object other) const
inherited

Check if another object is the same instance as this object.

Parameters
otherThe object to compare against this object
Returns
True if other is the same instance as this object, otherwise false

◆ 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

◆ onContactBegin()

void ime::Collider::onContactBegin ( const CollisionCallback callback)
inherited

Add an event listener to a collision start event.

Parameters
callbackThe function to be executed when this collider starts overlapping with another collider

Note that the callback is called when the colliders come into contact and not when they remain in contact. In other words it is called once per interaction. On invocation, the callback is passed this collider and the collider that it started overlapping with respectively. In addition, only one event listener may be registered to this event, subsequent event listeners overwrite the current one. Pass nullptr to remove the current event listener

Note
The callback is invoked for sensor and non-sensor colliders
Warning
Don't keep the pointers passed to the callback, they are invalidated when the callback execution terminates
See also
onContactEnd, onContactStay and setSensor

◆ onContactEnd()

void ime::Collider::onContactEnd ( const CollisionCallback callback)
inherited

Add an event listener to a collision end event.

Parameters
callbackThe function to be executed when the event is fired

The callback is called when this collider stops overlapping with another collider. The callback is passed this collider and the collider it stopped overlapping with respectively. Note that only only one event listener may be registered to this event, subsequent event listeners overwrite the current one. Pass nullptr to remove the current event listener

Note
The callback is invoked for sensor and non-sensor colliders
Warning
Don't keep the pointers passed to the callback, they are invalidated when the callback execution terminates
See also
onContactBegin, onContactStay and setSensor

◆ onContactStay()

void ime::Collider::onContactStay ( const CollisionCallback callback)
inherited

Add an event listener to a contact stay event.

Parameters
callbackThe function to be executed when the event is fired

The callback is called while this collider remains in contact with another collider. On invocation, the callback is passed this collider and the collider its currently overlapping with respectively.

Note that only one event listener may be registered to this event, subsequent event listeners overwrite the current one. Pass nullptr to remove the current event listener

Note
The callback is only invoked for non-sensor contacts. That is, if one of the two colliders in contact is a sensor, the on stay event will not be triggered. The event is also not triggered if the body the collider is attached to is not awake
Warning
Don't keep the pointers passed to the callback, they are invalidated when the callback execution terminates
See also
onContactBegin, onContactEnd and setSensor

◆ 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 unique id of the destruction listener

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

Warning
It's not advised to call virtual functions in the destruction callback as some parts of the object may have already been destroyed by the time the callback is invoked. In such an event, the behavior is undefined
See also
removeEventListener

◆ onPropertyChange() [1/2]

int ime::Object::onPropertyChange ( const Callback< Property > &  callback,
bool  oneTime = false 
)
inherited

Add an event listener to any property change event.

Parameters
callbackThe function to be executed when any property changes
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
Returns
The unique id of the event listener

When unsubscribing the any property change event handler, you must pass "propertyChange" as the name of the event

See also
onPropertyChange(const std::string&, const ime::Callback<ime::Property>&)

◆ onPropertyChange() [2/2]

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

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to add an event listener to
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
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, 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.

// 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");
See also
unsubscribe and onPropertyChange(const ime::Callback<ime::Property>&)

◆ operator=()

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

Move assignment operator.

◆ removeEventListener() [1/2]

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

Remove an event listener from an event.

Parameters
eventThe name of the event to remove an 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 tag of the object to console every time it changes
auto tagChangeId = object.onPropertyChange("tag", [](ime::Property tag) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the tag of the object when it changes
object.removeEventListener("tag", tagChangeId);
Class that can store a value of any type.
Definition: Property.h:38

◆ removeEventListener() [2/2]

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

Remove an event listener.

Parameters
idThe id of the event listener to be removed
Returns
True if the event listener was removed or false if no such handler exists

◆ 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

◆ 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

◆ setPosition()

void ime::CircleCollider::setPosition ( const Vector2f position)

Set the position of the circle.

Parameters
positionThe position of the circle

◆ setRadius()

void ime::CircleCollider::setRadius ( float  radius)

Set the radius of the circle.

Parameters
radiusThe radius of the circle

◆ 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 whether or not the collider acts as a sensor.

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. However, sensors only form contacts when at least one body is dynamic, therefore, 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
tagThe 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

See also
getObjectId

◆ suspendedEventListener()

void ime::Object::suspendedEventListener ( int  id,
bool  suspend 
)
inherited

Pause or resume execution of an event listener.

Parameters
idThe event listeners unique identification number
suspendTrue to suspend/pause or false to unsuspend/resume
See also
isEventListenerSuspended

Member Data Documentation

◆ eventEmitter_

EventEmitter ime::Object::eventEmitter_
protectedinherited

Event dispatcher.

Definition at line 289 of file Object.h.


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