CUGL 2.5
Cornell University Game Library
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | List of all members
cugl::physics2::ObstacleWorld Class Reference

#include <CUObstacleWorld.h>

Inheritance diagram for cugl::physics2::ObstacleWorld:
cugl::physics2::net::NetWorld

Public Member Functions

 ObstacleWorld ()
 
 ~ObstacleWorld ()
 
void dispose ()
 
bool init (const Rect bounds)
 
bool init (const Rect bounds, const Vec2 gravity)
 
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::unordered_set< std::shared_ptr< Obstacle > > & getObstacles () const
 
virtual void addObstacle (const std::shared_ptr< Obstacle > &obj)
 
virtual void removeObstacle (const std::shared_ptr< Obstacle > &obj)
 
const std::unordered_set< std::shared_ptr< Joint > > getJoints () const
 
virtual void addJoint (const std::shared_ptr< Joint > &joint)
 
virtual void removeJoint (const std::shared_ptr< Joint > &joint)
 
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
 
virtual void SayGoodbye (b2Joint *joint) override
 
virtual 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
 

Static Public Member Functions

static std::shared_ptr< ObstacleWorldalloc (const Rect bounds)
 
static std::shared_ptr< ObstacleWorldalloc (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
 

Protected Attributes

b2World * _world
 
bool _lockstep
 
float _stepssize
 
int _itvelocity
 
int _itposition
 
Vec2 _gravity
 
std::unordered_set< std::shared_ptr< Obstacle > > _obstacles
 
std::unordered_map< b2Joint *, std::shared_ptr< Joint > > _joints
 
Rect _bounds
 
bool _collide
 
bool _filters
 
bool _destroy
 

Detailed Description

A CUGL wrapper for a Box2d world.

This module provides a wrapper to Box2d that for use with CUGL obstacle heirarchy. Obstacles provide a simple and direct way to create physics objects that does not require the multi-step approach of Box2D. It also supports shared pointers for simply memory management.

In addition, 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

◆ ObstacleWorld()

cugl::physics2::ObstacleWorld::ObstacleWorld ( )

Creates a new degenerate ObstacleWorld on the stack.

The scene has no backing Box2d world and must be initialized.

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.

◆ ~ObstacleWorld()

cugl::physics2::ObstacleWorld::~ObstacleWorld ( )
inline

Deletes this world, disposing all resources

Member Function Documentation

◆ activateCollisionCallbacks()

void cugl::physics2::ObstacleWorld::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.

◆ activateDestructionCallbacks()

void cugl::physics2::ObstacleWorld::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.

◆ activateFilterCallbacks()

void cugl::physics2::ObstacleWorld::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.

◆ addJoint()

virtual void cugl::physics2::ObstacleWorld::addJoint ( const std::shared_ptr< Joint > &  joint)
virtual

Immediately adds a joint to the physics world

This method will fail if the joint obstacles are not in this world. The joint will be activated so that it contains those two obstacles. The physics world will include the joint in its next call to update.

The joint will be retained by this world, preventing it from being garbage collected.

Parameters
jointThe joint to add

Reimplemented in cugl::physics2::net::NetWorld.

◆ addObstacle()

virtual void cugl::physics2::ObstacleWorld::addObstacle ( const std::shared_ptr< Obstacle > &  obj)
virtual

Immediately adds the obstacle to the physics world

Adding an obstacle activates the underlying physics. It will now have a body. The physics world will include the obstacle in its next call to update.

The obstacle will be retained by this world, preventing it from being garbage collected.

Parameters
objThe obstacle to add

Reimplemented in cugl::physics2::net::NetWorld.

◆ alloc() [1/2]

static std::shared_ptr< ObstacleWorld > cugl::physics2::ObstacleWorld::alloc ( const Rect  bounds)
inlinestatic

Returns a newly allocated physics world

The specified bounds are in terms of the Box2d world, not the screen. A view 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
Returns
a newly allocated physics world

◆ alloc() [2/2]

static std::shared_ptr< ObstacleWorld > cugl::physics2::ObstacleWorld::alloc ( const Rect  bounds,
const Vec2  gravity 
)
inlinestatic

Returns a newly allocated physics world

The specified bounds are in terms of the Box2d world, not the screen. A view 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
a newly allocated physics world

◆ BeginContact()

void cugl::physics2::ObstacleWorld::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

◆ clear()

void cugl::physics2::ObstacleWorld::clear ( )

Remove all objects, emptying this physics world.

This method is different from dispose() in that the world can still receive new objects.

◆ dispose()

void cugl::physics2::ObstacleWorld::dispose ( )

Disposes all of the resources used by this world.

A disposed ObstacleWorld can be safely reinitialized. Any obstacles owned by this world will be deactivates. They will be deleted if no other object owns them.

◆ enabledCollisionCallbacks()

bool cugl::physics2::ObstacleWorld::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

◆ enabledDestructionCallbacks()

bool cugl::physics2::ObstacleWorld::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

◆ enabledFilterCallbacks()

bool cugl::physics2::ObstacleWorld::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

◆ EndContact()

void cugl::physics2::ObstacleWorld::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

◆ garbageCollect()

void cugl::physics2::ObstacleWorld::garbageCollect ( )

Remove all objects (obstacles and joints) marked for removal.

The objects will be released immediately. The physics will be deactivated and they will be removed from the Box2D world.

Removing an obstacle or joint does not automatically delete the obstacle itself. However, this world releases ownership, which may lead to it being garbage collected.

This method is the efficient, preferred way to remove obstacles or joints.

◆ getBounds()

const Rect cugl::physics2::ObstacleWorld::getBounds ( ) const
inline

Returns the bounds for the world controller.

Returns
the bounds for the world controller.

◆ getGravity()

const Vec2 cugl::physics2::ObstacleWorld::getGravity ( ) const
inline

Returns the global gravity vector.

Returns
the global gravity vector.

◆ getJoints()

const std::unordered_set< std::shared_ptr< Joint > > cugl::physics2::ObstacleWorld::getJoints ( ) const

Returns a read-only reference to the set of active joints.

Returns
a read-only reference to the set of active joints.

◆ getObstacles()

const std::unordered_set< std::shared_ptr< Obstacle > > & cugl::physics2::ObstacleWorld::getObstacles ( ) const
inline

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

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

◆ getPositionIterations()

int cugl::physics2::ObstacleWorld::getPositionIterations ( ) const
inline

Returns number of position iterations for the constrain solvers

Returns
number of position iterations for the constrain solvers

◆ getStepsize()

float cugl::physics2::ObstacleWorld::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.

◆ getVelocityIterations()

int cugl::physics2::ObstacleWorld::getVelocityIterations ( ) const
inline

Returns number of velocity iterations for the constrain solvers

Returns
number of velocity iterations for the constrain solvers

◆ getWorld()

b2World * cugl::physics2::ObstacleWorld::getWorld ( )
inline

Returns a (weak) 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.

As a weak reference, this physics world does not transfer ownership of this object. In addition, the value may be a nullptr.

Returns
a reference to the Box2d world.

◆ inBounds()

bool cugl::physics2::ObstacleWorld::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.

◆ init() [1/2]

bool cugl::physics2::ObstacleWorld::init ( const Rect  bounds)

Initializes a new physics world

The specified bounds are in terms of the Box2d world, not the screen. A view 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
Returns
true if the controller is initialized properly, false otherwise.

◆ init() [2/2]

bool cugl::physics2::ObstacleWorld::init ( const Rect  bounds,
const Vec2  gravity 
)

Initializes a new physics world

The specified bounds are in terms of the Box2d world, not the screen. A view 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.

◆ isLockStep()

bool cugl::physics2::ObstacleWorld::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.

◆ PostSolve()

void cugl::physics2::ObstacleWorld::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

◆ PreSolve()

void cugl::physics2::ObstacleWorld::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

◆ queryAABB()

void cugl::physics2::ObstacleWorld::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.
aabbThe axis-aligned bounding box

◆ rayCast()

void cugl::physics2::ObstacleWorld::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

◆ removeJoint()

virtual void cugl::physics2::ObstacleWorld::removeJoint ( const std::shared_ptr< Joint > &  joint)
virtual

Immediately removes a joint from the physics world

The joint will be released immediately. The physics will be deactivated and it will be removed from the Box2D world. Note that only the joint is removed. The bodies attached to the joint will still be present.

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

Removing a joint does not automatically delete the joint itself. However, this world releases ownership, which may lead to it being garbage collected.

Parameters
jointThe joint to remove

Reimplemented in cugl::physics2::net::NetWorld.

◆ removeObstacle()

virtual void cugl::physics2::ObstacleWorld::removeObstacle ( const std::shared_ptr< Obstacle > &  obj)
virtual

Immediately removes an obstacle from the physics world

The obstacle will be released immediately. The physics will be deactivated and it will be removed from the Box2D world. This method of removing obstacles is very heavy weight, and should only be used for single object removal. If you want to remove multiple obstacles, then you should mark them for removal and call garbageCollect.

Removing an obstacle does not automatically delete the obstacle itself. However, this world releases ownership, which may lead to it being garbage collected.

Parameters
objThe obstacle to remove

Reimplemented in cugl::physics2::net::NetWorld.

◆ SayGoodbye() [1/2]

virtual void cugl::physics2::ObstacleWorld::SayGoodbye ( b2Fixture *  fixture)
inlineoverridevirtual

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

◆ SayGoodbye() [2/2]

virtual void cugl::physics2::ObstacleWorld::SayGoodbye ( b2Joint *  joint)
overridevirtual

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

Reimplemented in cugl::physics2::net::NetWorld.

◆ setGravity()

void cugl::physics2::ObstacleWorld::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.

◆ setLockStep()

void cugl::physics2::ObstacleWorld::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.

◆ setPositionIterations()

void cugl::physics2::ObstacleWorld::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

◆ setStepsize()

void cugl::physics2::ObstacleWorld::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.

◆ setVelocityIterations()

void cugl::physics2::ObstacleWorld::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

◆ ShouldCollide()

bool cugl::physics2::ObstacleWorld::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.

◆ update()

void cugl::physics2::ObstacleWorld::update ( float  dt)

Executes a single step of the physics engine.

If isLockStep is true, then this method will run the physics simulation for getStepsize time, no matter the value of dt. Otherwise, it will run the simulation for dt seconds.

Parameters
dtNumber of seconds to run the (non-lockstep) simulation

Member Data Documentation

◆ _bounds

Rect cugl::physics2::ObstacleWorld::_bounds
protected

The boundary of the world

◆ _collide

bool cugl::physics2::ObstacleWorld::_collide
protected

Whether or not to activate the collision listener

◆ _destroy

bool cugl::physics2::ObstacleWorld::_destroy
protected

Whether or not to activate the destruction listener

◆ _filters

bool cugl::physics2::ObstacleWorld::_filters
protected

Whether or not to activate the filter listener

◆ _gravity

Vec2 cugl::physics2::ObstacleWorld::_gravity
protected

The current gravitational value of the world

◆ _itposition

int cugl::physics2::ObstacleWorld::_itposition
protected

The number of position iterations for the constrain solvers

◆ _itvelocity

int cugl::physics2::ObstacleWorld::_itvelocity
protected

The number of velocity iterations for the constrain solvers

◆ _joints

std::unordered_map<b2Joint*,std::shared_ptr<Joint> > cugl::physics2::ObstacleWorld::_joints
protected

The set of joints in this world

We map from the b2_joint pointer for purposes of cleanup.

◆ _lockstep

bool cugl::physics2::ObstacleWorld::_lockstep
protected

Whether to lock the physic timestep to a constant amount

◆ _obstacles

std::unordered_set<std::shared_ptr<Obstacle> > cugl::physics2::ObstacleWorld::_obstacles
protected

The set of obstacles in this world

◆ _stepssize

float cugl::physics2::ObstacleWorld::_stepssize
protected

The amount of time for a single engine step

◆ _world

b2World* cugl::physics2::ObstacleWorld::_world
protected

Reference to the Box2D world

◆ afterSolve

std::function<void(b2Contact* contact, const b2ContactImpulse* impulse)> cugl::physics2::ObstacleWorld::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

◆ beforeSolve

std::function<void(b2Contact* contact, const b2Manifold* oldManifold)> cugl::physics2::ObstacleWorld::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

◆ destroyFixture

std::function<void(b2Fixture* fixture)> cugl::physics2::ObstacleWorld::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

◆ destroyJoint

std::function<void(b2Joint* joint)> cugl::physics2::ObstacleWorld::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

◆ onBeginContact

std::function<void(b2Contact* contact)> cugl::physics2::ObstacleWorld::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

◆ onEndContact

std::function<void(b2Contact* contact)> cugl::physics2::ObstacleWorld::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

◆ shouldCollide

std::function<bool(b2Fixture* fixtureA, b2Fixture* fixtureB)> cugl::physics2::ObstacleWorld::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 file: