Cornell Cocos
Cornell Extensions to Cocos2d
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
PolygonObstacle Class Reference

#include <CUPolygonObstacle.h>

Inheritance diagram for PolygonObstacle:
SimpleObstacle Obstacle

Public Member Functions

const Size & getSize () const
 
void setSize (const Size &value)
 
void setSize (float width, float height)
 
float getWidth () const
 
void setWidth (float value)
 
float getHeight () const
 
void setHeight (float value)
 
const Vec2 & getAnchor () const
 
void setAnchor (const Vec2 &value)
 
void setAnchor (float x, float y)
 
const Poly2getPolygon () const
 
void setPolygon (const Poly2 &)
 
virtual void createFixtures () override
 
virtual void releaseFixtures () override
 
CC_CONSTRUCTOR_ACCESS _shapes (nullptr)
 
CC_CONSTRUCTOR_ACCESS _geoms (nullptr)
 
virtual ~PolygonObstacle ()
 
virtual bool init (const Poly2 &poly)
 
virtual bool init (const Poly2 &poly, const Vec2 &anchor)
 
- Public Member Functions inherited from SimpleObstacle
virtual b2BodyType getBodyType () const override
 
virtual void setBodyType (b2BodyType value) override
 
virtual Vec2 getPosition () const override
 
virtual void setPosition (const Vec2 &value) override
 
virtual void setPosition (float x, float y) override
 
virtual float getX () const override
 
virtual void setX (float value) override
 
virtual float getY () const override
 
virtual void setY (float value) override
 
virtual float getAngle () const override
 
virtual void setAngle (float value) override
 
virtual Vec2 getLinearVelocity () const override
 
virtual void setLinearVelocity (const Vec2 &value) override
 
virtual void setLinearVelocity (float x, float y) override
 
virtual float getVX () const override
 
virtual void setVX (float value) override
 
virtual float getVY () const override
 
virtual void setVY (float value) override
 
virtual float getAngularVelocity () const override
 
virtual void setAngularVelocity (float value) override
 
virtual bool isActive () const override
 
virtual void setActive (bool value) override
 
virtual bool isAwake () const override
 
virtual void setAwake (bool value) override
 
virtual bool isSleepingAllowed () const override
 
virtual void setSleepingAllowed (bool value) override
 
virtual bool isBullet () const override
 
virtual void setBullet (bool value) override
 
virtual bool isFixedRotation () const override
 
virtual void setFixedRotation (bool value) override
 
virtual float getGravityScale () const override
 
virtual void setGravityScale (float value) override
 
virtual float getLinearDamping () const override
 
virtual void setLinearDamping (float value) override
 
virtual float getAngularDamping () const override
 
virtual void setAngularDamping (float value) override
 
virtual void setDensity (float value) override
 
virtual void setFriction (float value) override
 
virtual void setRestitution (float value) override
 
virtual void setSensor (bool value) override
 
virtual void setFilterData (b2Filter value) override
 
virtual Vec2 getCentroid () const override
 
virtual void setCentroid (const Vec2 &value) override
 
virtual void setCentroid (float x, float y) override
 
virtual float getInertia () const override
 
virtual void setInertia (float value) override
 
virtual float getMass () const override
 
virtual void setMass (float value) override
 
virtual void resetMass () override
 
virtual b2Body * getBody () const override
 
virtual bool activatePhysics (b2World &world) override
 
virtual void deactivatePhysics (b2World &world) override
 
virtual void update (float delta) override
 
int getPositionSnap ()
 
void setPositionSnap (unsigned int snap)
 
int getAngleSnap ()
 
void setAngleSnap (unsigned int snap)
 
virtual void positionSceneNode () override
 
virtual void positionDebugNode () override
 
CC_CONSTRUCTOR_ACCESS _body (nullptr)
 
virtual ~SimpleObstacle ()
 
- Public Member Functions inherited from Obstacle
void setBodyState (const b2Body &body)
 
float getDensity () const
 
float getFriction () const
 
float getRestitution () const
 
bool isSensor () const
 
b2Filter getFilterData () const
 
bool isRemoved () const
 
void markRemoved (bool value)
 
bool isDirty () const
 
void markDirty (bool value)
 
const Vec2 & getDrawScale () const
 
virtual void setDrawScale (const Vec2 &value)
 
virtual void setDrawScale (float x, float y)
 
Node * getSceneNode () const
 
void setSceneNode (Node *node)
 
WireNodegetDebugNode () const
 
void setDebugNode (WireNode *node)
 
string getName () const
 
void setName (string value)
 
string toString () const
 
virtual ~Obstacle ()
 
virtual bool init ()
 
virtual bool init (const Vec2 &vec)
 

Static Public Member Functions

static PolygonObstaclecreate (const Poly2 &poly)
 
static PolygonObstaclecreate (const Poly2 &poly, const Vec2 &anchor)
 

Public Attributes

CC_CONSTRUCTOR_ACCESS __pad0__: PolygonObstacle(void) : SimpleObstacle()
 
- Public Attributes inherited from SimpleObstacle
CC_CONSTRUCTOR_ACCESS __pad0__: SimpleObstacle() : Obstacle()
 
- Public Attributes inherited from Obstacle
CC_CONSTRUCTOR_ACCESS __pad0__: Obstacle(void)
 

Protected Member Functions

void resize (const Size &size)
 
virtual void resetSceneNode () override
 
virtual void resetDebugNode () override
 
void resetShapes ()
 

Protected Attributes

Poly2 _polygon
 
b2PolygonShape * _shapes
 
b2Fixture ** _geoms
 
Vec2 _anchor
 
int _fixCount
 
- Protected Attributes inherited from SimpleObstacle
b2Body * _body
 
int _posSnap
 
unsigned long _posFact
 
int _angSnap
 
unsigned long _angFact
 
- Protected Attributes inherited from Obstacle
b2BodyDef _bodyinfo
 
b2FixtureDef _fixture
 
b2MassData _massdata
 
Vec2 _drawScale
 
bool _masseffect
 
Node * _node
 
WireNode_debug
 
string _tag
 

Detailed Description

Arbitrary polygonal-shaped model to support collisions.

The polygon can be any one that is representable by a Poly2 object. That means that it does not need to be convex, but it cannot have holes or self intersections.

Constructor & Destructor Documentation

PolygonObstacle::~PolygonObstacle ( )
virtual

Deletes this physics object and all of its resources.

A non-default destructor is necessary since we must release all the fixture pointers for the polygons.

Member Function Documentation

NS_CC_BEGIN PolygonObstacle * PolygonObstacle::create ( const Poly2 poly)
static

Creates a (not necessarily convex) polygon

The anchor point (the rotational center) of the polygon is at the center of the polygons bounding box.

Parameters
polyThe polygon vertices
Returns
An autoreleased physics object
PolygonObstacle * PolygonObstacle::create ( const Poly2 poly,
const Vec2 &  anchor 
)
static

Creates a (not necessarily convex) polygon

The anchor point (the rotational center) of the polygon is specified as a ratio of the bounding box. An anchor point of (0,0) is the bottom left of the bounding box. An anchor point of (1,1) is the top right of the bounding box. The anchor point does not need to be contained with the bounding box.

Parameters
polyThe polygon vertices
anchorThe rotational center of the polygon
Returns
An autoreleased physics object
void PolygonObstacle::createFixtures ( )
overridevirtual

Create new fixtures for this body, defining the shape

This is the primary method to override for custom physics objects

Reimplemented from SimpleObstacle.

const Vec2& PolygonObstacle::getAnchor ( ) const
inline

Returns the rotational center of this polygon

The anchor point of the polygon is specified as ratio of the bounding box. An anchor point of (0,0) is the bottom left of the bounding box. An anchor point of (1,1) is the top right of the bounding box. The anchorpoint does not need to be contained with the bounding box.

Returns
the rotational center of this polygon
float PolygonObstacle::getHeight ( ) const
inline

Returns the bounding box height

Returns
the bounding box height
const Poly2& PolygonObstacle::getPolygon ( ) const
inline

Returns the polygon defining this object

Returns
the polygon defining this object
const Size& PolygonObstacle::getSize ( ) const
inline

Returns the dimensions of the bounding box

Returns
the dimensions of the bounding box
float PolygonObstacle::getWidth ( ) const
inline

Returns the bounding box width

Returns
the bounding box width
virtual bool PolygonObstacle::init ( const Poly2 poly)
inlinevirtual

Initializes a (not necessarily convex) polygon

The anchor point (the rotational center) of the polygon is at the center of the polygons bounding box.

Parameters
polyThe polygon vertices
Returns
true if the obstacle is initialized properly, false otherwise.
bool PolygonObstacle::init ( const Poly2 poly,
const Vec2 &  anchor 
)
virtual

Initializes a (not necessarily convex) polygon

The anchor point (the rotational center) of the polygon is specified as a ratio of the bounding box. An anchor point of (0,0) is the bottom left of the bounding box. An anchor point of (1,1) is the top right of the bounding box. The anchor point does not need to be contained with the bounding box.

Parameters
polyThe polygon vertices
anchorThe rotational center of the polygon
Returns
true if the obstacle is initialized properly, false otherwise.
void PolygonObstacle::releaseFixtures ( )
overridevirtual

Release the fixtures for this body, reseting the shape

This is the primary method to override for custom physics objects

Reimplemented from SimpleObstacle.

void PolygonObstacle::resetDebugNode ( )
overrideprotectedvirtual

Redraws the outline of the physics fixtures to the debug node

The debug node is use to outline the fixtures attached to this object. This is very useful when the fixtures have a very different shape than the texture (e.g. a circular shape attached to a square texture).

Redraws the outline of the physics fixtures to the debug node

The debug node is use to outline the fixtures attached to this object. This is very useful when the fixtures have a very different shape than the texture (e.g. a circular shape attached to a square texture).

Unfortunately, the current implementation is very inefficient. Cocos2d does not batch drawnode commands like it does Sprites or PolygonSprites. Therefore, every distinct DrawNode is a distinct OpenGL call. This can really hurt framerate when debugging mode is on. Ideally, we would refactor this so that we only draw to a single, master draw node. However, this means that we would have to handle our own vertex transformations, instead of relying on the transforms in the scene graph.

Reimplemented from Obstacle.

void PolygonObstacle::resetSceneNode ( )
overrideprotectedvirtual

Performs any necessary additions to the scene graph node.

This method is necessary for custom physics objects that are composed of multiple scene graph nodes.

Reimplemented from Obstacle.

void PolygonObstacle::resetShapes ( )
protected

Recreates the shape objects attached to this polygon.

This must be called whenever the polygon is resized.

void PolygonObstacle::resize ( const Size &  size)
protected

Resets the polygon vertices in the shape to match the dimension.

This is an internal method and it does not mark the physics object as dirty.

Parameters
sizeThe new dimension (width and height)
void PolygonObstacle::setAnchor ( const Vec2 &  value)
inline

Sets the rotational center of this polygon

The anchor point of the polygon is specified as ratio of the bounding box. An anchor point of (0,0) is the bottom left of the bounding box. An anchor point of (1,1) is the top right of the bounding box. The anchorpoint does not need to be contained with the bounding box.

Parameters
valuethe rotational center of this polygon
void PolygonObstacle::setAnchor ( float  x,
float  y 
)

Sets the rotational center of this polygon

The anchor point of the polygon is specified as ratio of the bounding box. An anchor point of (0,0) is the bottom left of the bounding box. An anchor point of (1,1) is the top right of the bounding box. The anchorpoint does not need to be contained with the bounding box.

Parameters
xthe x-coordinate of the rotational center
ythe y-coordinate of the rotational center
void PolygonObstacle::setHeight ( float  value)
inline

Sets the bounding box height

The vertices are rescaled according to their vertex origin. This change cannot happen immediately. It must wait until the next update is called. The current anchor point will be preserved.

Parameters
valuethe bounding box height
void PolygonObstacle::setPolygon ( const Poly2 poly)

Sets the polygon defining this object

This change cannot happen immediately. It must wait until the next update is called. The current anchor point will be preserved.

Parameters
valuethe polygon defining this object

Sets the polygon defining this object

This change cannot happen immediately. It must wait until the next update is called.

Parameters
valuethe polygon defining this object
void PolygonObstacle::setSize ( const Size &  value)
inline

Sets the dimensions of the bounding box

The vertices are rescaled according to their vertex origin. This change cannot happen immediately. It must wait until the next update is called. The current anchor point will be preserved.

Parameters
valuethe dimensions of the bounding box
void PolygonObstacle::setSize ( float  width,
float  height 
)
inline

Sets the dimensions of this box

Parameters
widthThe width of this box
heightThe height of this box
void PolygonObstacle::setWidth ( float  value)
inline

Sets the bounding box width

The vertices are rescaled according to their vertex origin. This change cannot happen immediately. It must wait until the next update is called. The current anchor point will be preserved.

Parameters
valuethe bounding box width

Member Data Documentation

Vec2 PolygonObstacle::_anchor
protected

Anchor point to synchronize with the scene graph

int PolygonObstacle::_fixCount
protected

In case the number of polygons changes

b2Fixture** PolygonObstacle::_geoms
protected

A cache value for the fixtures (for resizing)

Poly2 PolygonObstacle::_polygon
protected

The polygon vertices (for resizing)

b2PolygonShape* PolygonObstacle::_shapes
protected

Shape information for this physics object


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