Abstract Base class for colliders. More...
#include <Collider.h>
Public Types | |
enum class | Type { Circle , Box , Polygon , Edge } |
The type of the collider. More... | |
using | Ptr = std::unique_ptr< Collider > |
Unique collider pointer. More... | |
using | CollisionCallback = std::function< void(Collider *, Collider *)> |
RigidBody collision callback. More... | |
Public Member Functions | |
Collider (Type type) | |
Construct a collider. More... | |
Collider (Collider &&) noexcept | |
Move constructor. More... | |
Collider & | operator= (Collider &&) noexcept |
Move assignment operator. More... | |
virtual Collider::Ptr | clone () const =0 |
Create a copy of the collider. 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 CollisionFilterData & | getCollisionFilterData () 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... | |
RigidBody * | getBody () |
Get the body the collider is attached to. More... | |
const RigidBody * | getBody () const |
bool | containsPoint (const Vector2f &point) const |
Check if the collider contains a point or not. More... | |
PropertyContainer & | getUserData () |
Get the user data extracted from the collider definition. More... | |
const PropertyContainer & | getUserData () 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) |
~Collider () override=0 | |
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 | getClassName () const =0 |
Get the name of the objects concrete class. 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 | |
Collider (const Collider &) | |
Copy constructor. More... | |
Collider & | operator= (const Collider &) |
Copy assignment operator. More... | |
virtual b2Shape & | getInternalShape ()=0 |
virtual const b2Shape & | getInternalShape () const =0 |
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 | RigidBody |
Needs access to constructor. More... | |
Abstract Base class for colliders.
A collider enables collisions between rigid bodies/game objects. It defines the shape of a rigid body/game object for the purpose of physical collisions. Usually a collider is attached to a rigid Body which is then attached to a GameObject. A game object that has a rigid body attached to it which does not have a collider will be affected by physics (impulses, gravity, friction etc), but the game object cannot react/respond to collisions.
Colliders are invisible however they may be drawn on the render window during debug mode by enabling debug drawing of the physics World.
Rigid bodies enable physics while colliders enable collisions
Definition at line 119 of file Collider.h.
using ime::Collider::CollisionCallback = std::function<void(Collider*, Collider*)> |
RigidBody collision callback.
Definition at line 122 of file Collider.h.
using ime::Collider::Ptr = std::unique_ptr<Collider> |
Unique collider pointer.
Definition at line 121 of file Collider.h.
|
strong |
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.
|
explicit |
Construct a collider.
type | Type of the collider |
|
noexcept |
Move constructor.
|
overridepure virtual |
Destructor.
|
protected |
Copy constructor.
|
pure virtual |
Create a copy of the collider.
Implemented in ime::BoxCollider, ime::CircleCollider, ime::EdgeCollider, and ime::PolygonCollider.
bool ime::Collider::containsPoint | ( | const Vector2f & | point | ) | const |
Check if the collider contains a point or not.
point | The point to be checked in world coordinates |
|
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.
RigidBody * ime::Collider::getBody | ( | ) |
Get the body the collider is attached to.
|
pure virtualinherited |
Get the name of the objects concrete class.
This function is implemented by all internal classes that inherit from this class (either directly or indirectly). Example:
Implemented in ime::audio::Music, ime::audio::SoundEffect, ime::GameObject, ime::GridObject, ime::CyclicGridMover, ime::GridMover, ime::KeyboardGridMover, ime::RandomGridMover, ime::TargetGridMover, ime::AABB, ime::BoxCollider, ime::CircleCollider, ime::EdgeCollider, ime::PolygonCollider, ime::DistanceJoint, ime::RigidBody, ime::RenderLayer, ime::RenderLayerContainer, ime::Scene, ime::Camera, ime::CircleShape, ime::ConvexShape, ime::RectangleShape, ime::Sprite, ime::SpriteSheet, and ime::Tile.
|
overridevirtual |
Get 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
Reimplemented from ime::Object.
const CollisionFilterData & ime::Collider::getCollisionFilterData | ( | ) | const |
Get the collision filter data for the collider.
float ime::Collider::getDensity | ( | ) | const |
Get the density of the collider.
float ime::Collider::getFriction | ( | ) | const |
Get the coefficient of friction.
|
inherited |
Get the unique id of the object.
Note that each instance of ime::Object has a unique id
float ime::Collider::getRestitution | ( | ) | const |
Get the coefficient of restitution of the collider.
float ime::Collider::getRestitutionThreshold | ( | ) | const |
Get the restitution velocity threshold of the collider.
|
inherited |
Type ime::Collider::getType | ( | ) | const |
Get the type of the collider.
PropertyContainer & ime::Collider::getUserData | ( | ) |
Get the user data extracted from the collider definition.
bool ime::Collider::isAttachedToBody | ( | ) | const |
Check if the collider is attached to a rigid body or not.
|
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
|
inherited |
Check if another object is the same instance as this object.
other | The object to compare against this object |
bool ime::Collider::isSensor | ( | ) | const |
Check whether or not the collider is a sensor.
void ime::Collider::onContactBegin | ( | const CollisionCallback & | callback | ) |
Add an event listener to a collision start event.
callback | The 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
void ime::Collider::onContactEnd | ( | const CollisionCallback & | callback | ) |
Add an event listener to a collision end event.
callback | The 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
void ime::Collider::onContactStay | ( | const CollisionCallback & | callback | ) |
Add an event listener to a contact stay event.
callback | The 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
|
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.
|
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::Collider::resetCollisionFilterData | ( | ) |
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
void ime::Collider::setCollisionFilter | ( | const CollisionFilterData & | filterData | ) |
Set the collision filter data of the collider.
filterData | The 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
void ime::Collider::setDensity | ( | float | density | ) |
Set the density of the collider.
density | The 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
void ime::Collider::setEnable | ( | bool | enable | ) |
Enable or disable the collider.
enable | True 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:
By default, the collider is enabled
void ime::Collider::setFriction | ( | float | friction | ) |
Set the coefficient of friction.
friction | The 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
By default, the friction is 0.2f
void ime::Collider::setRestitution | ( | float | restitution | ) |
Set the coefficient of restitution.
restitution | The 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
By default, the restitution is 0.0f
void ime::Collider::setRestitutionThreshold | ( | float | threshold | ) |
Set the restitution threshold of the collider.
threshold | The new restitution threshold |
By default, the restitution threshold is 1.0f
void ime::Collider::setSensor | ( | bool | sensor | ) |
Set whether or not the collider acts as a sensor.
sensor | True 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
|
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
|
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 499 of file Collider.h.
|
protectedinherited |