Cornell Cocos
Cornell Extensions to Cocos2d
All Classes Functions Variables Enumerations Enumerator Friends
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | List of all members
WorldController Class Reference

#include <CUWorldController.h>

Inheritance diagram for WorldController:

Public Member Functions

b2World * getWorld ()
 
bool isLockStep () const
 
void setLockStep (bool flag)
 
float getStepsize () const
 
void setStepsize (float step)
 
int getVelocityIterations () const
 
void setVelocityIterations (int velocity)
 
int getPositionIterations () const
 
void setPositionIterations (int position)
 
const Vec2 & getGravity () const
 
void setGravity (const Vec2 &gravity)
 
void update (float dt)
 
const Rect & getBounds () const
 
bool inBounds (Obstacle *obj)
 
const std::vector< Obstacle * > & getObstacles ()
 
void addObstacle (Obstacle *obj)
 
void removeObstacle (Obstacle *obj)
 
void garbageCollect ()
 
void clear ()
 
void activateCollisionCallbacks (bool flag)
 
bool enabledCollisionCallbacks () const
 
void BeginContact (b2Contact *contact) override
 
void EndContact (b2Contact *contact) override
 
void PreSolve (b2Contact *contact, const b2Manifold *oldManifold) override
 
void PostSolve (b2Contact *contact, const b2ContactImpulse *impulse) override
 
void activateFilterCallbacks (bool flag)
 
bool enabledFilterCallbacks () const
 
bool ShouldCollide (b2Fixture *fixtureA, b2Fixture *fixtureB) override
 
void activateDestructionCallbacks (bool flag)
 
bool enabledDestructionCallbacks () const
 
void SayGoodbye (b2Joint *joint) override
 
void SayGoodbye (b2Fixture *fixture) override
 
void queryAABB (std::function< bool(b2Fixture *fixture)> callback, const Rect &aabb) const
 
void rayCast (std::function< float(b2Fixture *fixture, const Vec2 &point, const Vec2 &normal, float fraction)> callback, const Vec2 &point1, const Vec2 &point2) const
 
 ~WorldController ()
 
virtual bool init (const Rect &bounds)
 
virtual bool init (const Rect &bounds, const Vec2 &gravity)
 

Static Public Member Functions

static WorldControllercreate (const Rect &bounds)
 
static WorldControllercreate (const Rect &bounds, const Vec2 &gravity)
 

Public Attributes

std::function< void(b2Contact *contact)> onBeginContact
 
std::function< void(b2Contact *contact)> onEndContact
 
std::function< void(b2Contact *contact, const b2Manifold *oldManifold)> beforeSolve
 
std::function< void(b2Contact *contact, const b2ContactImpulse *impulse)> afterSolve
 
std::function< bool(b2Fixture *fixtureA, b2Fixture *fixtureB)> shouldCollide
 
std::function< void(b2Fixture *fixture)> destroyFixture
 
std::function< void(b2Joint *joint)> destroyJoint
 
CC_CONSTRUCTOR_ACCESS __pad0__: WorldController()
 

Protected Attributes

b2World * _world
 
bool _lockstep
 
float _stepssize
 
int _itvelocity
 
int _itposition
 
Vec2 _gravity
 
std::vector< Obstacle * > _objects
 
Rect _bounds
 
bool _collide
 
bool _filters
 
bool _destroy
 

Detailed Description

A Cocos2d wrapper for a Box2d world.

This class is superior to the Box2d support in Cocos2d. The existing implementation is coupled with the scene graph, which is bad, bad bad. This is a way to handle Box2d with proper reference counting and garbage collection that does not require the scene graph.

This class provides a modern callback approach supporting closures assigned to attributes. This allows you to modify the callback functions while the program is running.

Constructor & Destructor Documentation

WorldController::~WorldController ( )

Dispose of all resources allocated to this controller.

Member Function Documentation

void WorldController::activateCollisionCallbacks ( bool  flag)

Activates the collision callbacks.

If flag is false, then the collision callbacks (even if defined) will be ignored. Otherwise, the callbacks will be executed (on collision) if they are defined.

Parameters
flagwhether to activate the collision callbacks.
void WorldController::activateDestructionCallbacks ( bool  flag)

Activates the destruction callbacks.

If flag is false, then the destruction callbacks (even if defined) will be ignored. Otherwise, the callbacks will be executed (on body destruction) if they are defined.

Parameters
flagwhether to activate the collision callbacks.
void WorldController::activateFilterCallbacks ( bool  flag)

Activates the collision filter callbacks.

If flag is false, then the collision filter callbacks (even if defined) will be ignored. Otherwise, the callbacks will be executed (to test a collision) if they are defined.

Parameters
flagwhether to activate the collision callbacks.
void WorldController::addObstacle ( Obstacle obj)

Immediately adds the object to the physics world

The object will be available at the next call to update. The object will be retained, preventing it from being garbage collected.

param obj The object to add

a reference to the obstacle

void WorldController::BeginContact ( b2Contact *  contact)
inlineoverride

Called when two fixtures begin to touch

This method is the static callback required by the Box2d API. It should not be altered.

Parameters
contactthe contact information
void WorldController::clear ( )

Remove all objects, emptying this controller.

This method is different from a constructor in that the controller can still receive new objects.

WorldController * WorldController::create ( const Rect &  bounds)
static

Creates a new physics world

The specified bounds are in terms of the Box2d world, not the screen. A few attached to this Box2d world should have ways to convert between the coordinate systems.

This constructor will use the default gravitational value.

Parameters
boundsThe game bounds in Box2d coordinates
gravityThe gravitational force on this Box2d world
Returns
An autoreleased physics controller
WorldController * WorldController::create ( const Rect &  bounds,
const Vec2 &  gravity 
)
static

Creates a new physics world

The specified bounds are in terms of the Box2d world, not the screen. A few attached to this Box2d world should have ways to convert between the coordinate systems.

Parameters
boundsThe game bounds in Box2d coordinates
gravityThe gravitational force on this Box2d world
Returns
An autoreleased physics controller
bool WorldController::enabledCollisionCallbacks ( ) const
inline

Returns true if the collision callbacks are active

If this value is false, then the collision callbacks (even if defined) will be ignored. Otherwise, the callbacks will be executed (on collision) if they are defined.

Returns
true if the collision callbacks are active
bool WorldController::enabledDestructionCallbacks ( ) const
inline

Returns true if the destruction callbacks are active

If this value is false, then the destruction callbacks (even if defined) will be ignored. Otherwise, the callbacks will be executed (on body destruction) if they are defined.

Returns
true if the destruction callbacks are active
bool WorldController::enabledFilterCallbacks ( ) const
inline

Returns true if the collision filter callbacks are active

If this value is false, then the collision filter callbacks (even if defined) will be ignored. Otherwise, the callbacks will be executed (to test a collision) if they are defined.

Returns
true if the collision filter callbacks are active
void WorldController::EndContact ( b2Contact *  contact)
inlineoverride

Called when two fixtures cease to touch

This method is the static callback required by the Box2d API. It should not be altered.

Parameters
contactthe contact information
void WorldController::garbageCollect ( )

Remove all objects marked for removal.

The objects will be released immediately. If no more objects assert ownership, then the objects will be garbage collected.

This method is the efficient, preferred way to remove objects.

const Rect& WorldController::getBounds ( ) const
inline

Returns the bounds for the world controller.

Returns
the bounds for the world controller.
const Vec2& WorldController::getGravity ( ) const
inline

Returns the global gravity vector.

Returns
the global gravity vector.
const std::vector<Obstacle*>& WorldController::getObstacles ( )
inline

Returns a read-only reference to the list of active obstacles.

Returns
a read-only reference to the list of active obstacles.
int WorldController::getPositionIterations ( ) const
inline

Returns number of position iterations for the constrain solvers

Returns
number of position iterations for the constrain solvers
float WorldController::getStepsize ( ) const
inline

Returns the amount of time for a single engine step.

This attribute is only relevant if isLockStep() is true.

Returns
the amount of time for a single engine step.
int WorldController::getVelocityIterations ( ) const
inline

Returns number of velocity iterations for the constrain solvers

Returns
number of velocity iterations for the constrain solvers
b2World* WorldController::getWorld ( )
inline

Returns a reference to the Box2d world.

This accessor is for any world methods that are not encapsulated by this constroller. We have largely limited the controller to functionality that requires b2WorldCallbacks, as those classes are antiquated in the face of modern closures.

Returns
a reference to the Box2d world.
bool WorldController::inBounds ( Obstacle obj)

Returns true if the object is in bounds.

This assertion is useful for debugging the physics.

Parameters
objThe object to check.
Returns
true if the object is in bounds.
bool WorldController::init ( const Rect &  bounds)
virtual

Initializes a new physics world

The specified bounds are in terms of the Box2d world, not the screen. A few attached to this Box2d world should have ways to convert between the coordinate systems.

This constructor will use the default gravitational value.

Parameters
boundsThe game bounds in Box2d coordinates
gravityThe gravitational force on this Box2d world
Returns
true if the controller is initialized properly, false otherwise.
bool WorldController::init ( const Rect &  bounds,
const Vec2 &  gravity 
)
virtual

Initializes a new physics world

The specified bounds are in terms of the Box2d world, not the screen. A few attached to this Box2d world should have ways to convert between the coordinate systems.

Parameters
boundsThe game bounds in Box2d coordinates
gravityThe gravitational force on this Box2d world
Returns
true if the controller is initialized properly, false otherwise.
bool WorldController::isLockStep ( ) const
inline

Returns true if the physics is locked to a constant timestep.

If this is false, the physics timestep will vary with the graphics framerate.

Returns
true if the physics is locked to a constant timestep.
void WorldController::PostSolve ( b2Contact *  contact,
const b2ContactImpulse *  impulse 
)
inlineoverride

Called after the solver is finished.

This callback lets you inspect a contact after the solver is finished. This is useful for inspecting impulses.

Note: the contact manifold does not include time of impact impulses, which can be arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly in a separate data structure. Note: this is only called for contacts that are touching, solid, and awake.

This method is the static callback required by the Box2d API. It should not be altered.

Parameters
contactthe contact information
impulsethe impulse produced by the solver
void WorldController::PreSolve ( b2Contact *  contact,
const b2Manifold *  oldManifold 
)
inlineoverride

Called after a contact is updated.

This callback allows you to inspect a contact before it goes to the solver. If you are careful, you can modify the contact manifold (e.g. disable contact).

A copy of the old manifold is provided so that you can detect changes.

Note: this is called only for awake bodies. Note: this is called even when the number of contact points is zero. Note: this is not called for sensors. Note: if you set the number of contact points to zero, you will not get an EndContact callback. However, you may get a BeginContact callback the next step.

This method is the static callback required by the Box2d API. It should not be altered.

Parameters
contactthe contact information
oldManifoldthe contact manifold last iteration
void WorldController::queryAABB ( std::function< bool(b2Fixture *fixture)>  callback,
const Rect &  aabb 
) const

Query the world for all fixtures that potentially overlap the provided AABB.

The AABB is specified by a Cocos2d rectangle.

Parameters
callbacka user implemented callback function.
rectthe axis-aligned bounding box
void WorldController::rayCast ( std::function< float(b2Fixture *fixture, const Vec2 &point, const Vec2 &normal, float fraction)>  callback,
const Vec2 &  point1,
const Vec2 &  point2 
) const

Ray-cast the world for all fixtures in the path of the ray.

The callback controls whether you get the closest point, any point, or n-points. The ray-cast ignores shapes that contain the starting point.

Parameters
callbacka user implemented callback function.
point1the ray starting point
point2the ray ending point
void WorldController::removeObstacle ( Obstacle obj)

Immediately removes object to the physics world

The object will be released immediately. If no more objects assert ownership, then the object will be garbage collected.

This method of removing objects is very heavy weight, and should only be used for single object removal. If you want to remove multiple objects, then you should mark them for removal and call garbageCollect.

param obj The object to remove

a reference to the obstacle

void WorldController::SayGoodbye ( b2Joint *  joint)
inlineoverride

Called when a joint is about to be destroyed.

This function is only called when the destruction is the result of the destruction of one of its attached bodies.

Parameters
jointthe joint to be destroyed
void WorldController::SayGoodbye ( b2Fixture *  fixture)
inlineoverride

Called when a fixture is about to be destroyed.

This function is only called when the destruction is the result of the destruction of its parent body.

Parameters
fixturethe fixture to be destroyed
void WorldController::setGravity ( const Vec2 &  gravity)

Sets the global gravity vector.

Any change will take effect at the time of the next call to update.

Parameters
gravitythe global gravity vector.
void WorldController::setLockStep ( bool  flag)
inline

Sets whether the physics is locked to a constant timestep.

If this is false, the physics timestep will vary with the graphics framerate. Any change will take effect at the time of the next call to update.

Parameters
flagwhether the physics is locked to a constant timestep.
void WorldController::setPositionIterations ( int  position)
inline

Sets number of position iterations for the constrain solvers

Any change will take effect at the time of the next call to update.

Parameters
positionnumber of position iterations for the constrain solvers
void WorldController::setStepsize ( float  step)
inline

Sets the amount of time for a single engine step.

This attribute is only relevant if isLockStep() is true. Any change will take effect at the time of the next call to update.

Parameters
stepthe amount of time for a single engine step.
void WorldController::setVelocityIterations ( int  velocity)
inline

Sets number of velocity iterations for the constrain solvers

Any change will take effect at the time of the next call to update.

Parameters
velocitynumber of velocity iterations for the constrain solvers
bool WorldController::ShouldCollide ( b2Fixture *  fixtureA,
b2Fixture *  fixtureB 
)
inlineoverride

Return true if contact calculations should be performed between these two shapes.

For performance reasons this is only called when the AABBs begin to overlap.

Parameters
fixtureAthe first colliding shape
fixtureBthe second colliding shape
Returns
true if contact calculations should be performed between these two shapes.
void WorldController::update ( float  dt)

Executes a single step of the physics engine.

This method contains the specific update code for this mini-game. It does not handle collisions, as those are managed by the parent class WorldController. This method is called after input is read, but before collisions are resolved. The very last thing that it should do is apply forces to the appropriate objects.

Once the update phase is over, but before we draw, we are ready to handle physics. The primary method is the step() method in world. This implementation works for all applications and should not need to be overwritten.

Parameters
deltaNumber of seconds since last animation frame

Member Data Documentation

Rect WorldController::_bounds
protected

The boundary of the world

bool WorldController::_collide
protected

Whether or not to activate the collision listener

bool WorldController::_destroy
protected

Whether or not to activate the destruction listener

bool WorldController::_filters
protected

Whether or not to activate the filter listener

Vec2 WorldController::_gravity
protected

The current gravitational value of the world

int WorldController::_itposition
protected

The number of position iterations for the constrain solvers

int WorldController::_itvelocity
protected

The number of velocity iterations for the constrain solvers

bool WorldController::_lockstep
protected

Whether to lock the physic timestep to a constant amount

std::vector<Obstacle*> WorldController::_objects
protected

The list of objects in this world

float WorldController::_stepssize
protected

The amount of time for a single engine step

b2World* WorldController::_world
protected

Reference to the Box2D world

std::function<void(b2Contact* contact, const b2ContactImpulse* impulse)> WorldController::afterSolve

Called after the solver is finished.

This callback lets you inspect a contact after the solver is finished. This is useful for inspecting impulses.

Note: the contact manifold does not include time of impact impulses, which can be arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly in a separate data structure. Note: this is only called for contacts that are touching, solid, and awake.

This attribute is a dynamically assignable callback and may be changed at any given time.

Parameters
contactthe contact information
impulsethe impulse produced by the solver
std::function<void(b2Contact* contact, const b2Manifold* oldManifold)> WorldController::beforeSolve

Called after a contact is updated.

This callback allows you to inspect a contact before it goes to the solver. If you are careful, you can modify the contact manifold (e.g. disable contact).

A copy of the old manifold is provided so that you can detect changes.

Note: this is called only for awake bodies. Note: this is called even when the number of contact points is zero. Note: this is not called for sensors. Note: if you set the number of contact points to zero, you will not get an EndContact callback. However, you may get a BeginContact callback the next step.

This attribute is a dynamically assignable callback and may be changed at any given time.

Parameters
contactthe contact information
oldManifoldthe contact manifold last iteration
std::function<void(b2Fixture* fixture)> WorldController::destroyFixture

Called when a fixture is about to be destroyed.

This function is only called when the destruction is the result of the destruction of its parent body.

Parameters
fixturethe fixture to be destroyed
std::function<void(b2Joint* joint)> WorldController::destroyJoint

Called when a joint is about to be destroyed.

This function is only called when the destruction is the result of the destruction of one of its attached bodies.

Parameters
jointthe joint to be destroyed
std::function<void(b2Contact* contact)> WorldController::onBeginContact

Called when two fixtures begin to touch

This attribute is a dynamically assignable callback and may be changed at any given time.

Parameters
contactthe contact information
std::function<void(b2Contact* contact)> WorldController::onEndContact

Called when two fixtures cease to touch

This attribute is a dynamically assignable callback and may be changed at any given time.

Parameters
contactthe contact information
std::function<bool(b2Fixture* fixtureA, b2Fixture* fixtureB)> WorldController::shouldCollide

Return true if contact calculations should be performed between these two shapes.

For performance reasons this is only called when the AABBs begin to overlap.

Parameters
fixtureAthe first colliding shape
fixtureBthe second colliding shape
Returns
true if contact calculations should be performed between these two shapes.

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