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

#include <CUObstacleSelector.h>

Inheritance diagram for ObstacleSelector:

Public Member Functions

bool isSelected () const
 
ObstaclegetObstacle ()
 
bool select (const Vec2 &pos)
 
void moveTo (const Vec2 &pos)
 
void deselect ()
 
bool onQuery (b2Fixture *fixture)
 
float getFrequency () const
 
void setFrequency (float speed)
 
float getDamping () const
 
void setDamping (float ratio)
 
float getForce () const
 
void setForce (float force)
 
const Size & getMouseSize () const
 
void setMouseSize (const Size &size)
 
CC_CONSTRUCTOR_ACCESS _selection (nullptr)
 
CC_CONSTRUCTOR_ACCESS _ground (nullptr)
 
CC_CONSTRUCTOR_ACCESS _mouseJoint (nullptr)
 
 ~ObstacleSelector (void)
 
bool init (WorldController *controller)
 
bool init (WorldController *controller, const Size &mouseSize)
 

Static Public Member Functions

static ObstacleSelectorcreate (WorldController *controller)
 
static ObstacleSelectorcreate (WorldController *controller, const Size &mouseSize)
 

Public Attributes

CC_CONSTRUCTOR_ACCESS __pad0__: ObstacleSelector(void) : _controller(nullptr)
 

Protected Attributes

WorldController_controller
 
b2Fixture * _selection
 
b2Body * _ground
 
b2MouseJointDef _jointDef
 
b2MouseJoint * _mouseJoint
 
Rect _pointer
 
float _force
 

Detailed Description

Selection tool to move and drag physics obstacles

This class is essentially an instance of b2MouseJoint, but with an API that makes it a lot easier to use. It must be attached to a WorldController on creation, and this controller can never change. If you want a selector for a different WorldController, make a new instance.

As with all instances of b2MouseJoint, there will be some lag in the drag (though this is true on touch devices in general). You can adjust the degree of this lag by adjusting the force. However, larger forces can cause artifacts when dragging an obstacle through other obstacles.

Constructor & Destructor Documentation

ObstacleSelector::~ObstacleSelector ( void  )

Disposes of this selector, releasing all resources.

Member Function Documentation

NS_CC_BEGIN ObstacleSelector * ObstacleSelector::create ( WorldController controller)
static

Creates a new ObstacleSelector for the given WorldController

This controller can never change. If you want a selector for a different WorldController, make a new instance.

This constructor uses the default mouse size.

Parameters
controllerthe physics controller

a reference to the world controller

Returns
an autoreleased selector object
ObstacleSelector * ObstacleSelector::create ( WorldController controller,
const Size &  mouseSize 
)
static

Creates a new ObstacleSelector for the given WorldController and mouse size.

This controller can never change. If you want a selector for a different WorldController, make a new instance. However, the mouse size can be changed at any time.

Parameters
controllerthe physics controller

a reference to the world controller

Returns
an autoreleased selector object
void ObstacleSelector::deselect ( )

Deselects the physics body, discontinuing any mouse movement.

The body may still continue to move of its own accord.

float ObstacleSelector::getDamping ( ) const
inline

Returns the damping ratio of the mouse joint

See the documentation of b2JointDef for more information on the damping ratio.

Returns
the damping ratio of the mouse joint
float ObstacleSelector::getForce ( ) const
inline

Returns the force multiplier of the mouse joint

The mouse joint will move the attached fixture with a force of this value times the object mass.

Returns
the force multiplier of the mouse joint
float ObstacleSelector::getFrequency ( ) const
inline

Returns the response speed of the mouse joint

See the documentation of b2JointDef for more information on the response speed.

Returns
the response speed of the mouse joint
const Size& ObstacleSelector::getMouseSize ( ) const
inline

Returns the size of the mouse pointer

When a selection is made, this selector will create an axis-aligned bounding box centered at the mouse position. Any fixture overlapping this box will be selected. The size of this box is determined by this value.

Returns
the size of the mouse pointer
Obstacle * ObstacleSelector::getObstacle ( )

Returns the Obstacle selected (if any)

Just because a physics body was selected does not mean that an Obstacle was selected. The body could be a basic Box2d body generated by other means. If the body is not an Obstacle, this method returns nullptr.

Returns
the Obstacle selected (if any)
bool ObstacleSelector::init ( WorldController controller)
inline

Initializes a new ObstacleSelector for the given WorldController

This controller can never change. If you want a selector for a different WorldController, make a new instance.

This initializer uses the default mouse size.

Parameters
controllerthe physics controller

a reference to the world controller

Returns
true if the obstacle is initialized properly, false otherwise.
bool ObstacleSelector::init ( WorldController controller,
const Size &  mouseSize 
)

Initializes a new ObstacleSelector for the given WorldController and mouse size.

This controller can never change. If you want a selector for a different WorldController, make a new instance. However, the mouse size can be changed at any time.

Parameters
controllerthe physics controller

a reference to the world controller

Returns
true if the obstacle is initialized properly, false otherwise.
bool ObstacleSelector::isSelected ( ) const
inline

Returns true if a physics body is currently selected

Returns
true if a physics body is currently selected
void ObstacleSelector::moveTo ( const Vec2 &  pos)

Moves the selected body to the given position.

Parameters
posthe position (in physics space) to move to

If nothing is selected, this method does nothing.

bool ObstacleSelector::onQuery ( b2Fixture *  fixture)

Callback function for mouse selection.

This is the callback function used by the method queryAABB to select a physics body at the current mouse location.

Parameters
fixturethe fixture selected
Returns
false to terminate the query.
bool ObstacleSelector::select ( const Vec2 &  pos)

Returns true if a physics body was selected at the given position.

This method contructs and AABB the size of the mouse pointer, centered at the given position. If any part of the AABB overlaps a fixture, it is selected.

Parameters
posthe position (in physics space) to select
Returns
true if a physics body was selected at the given position.
void ObstacleSelector::setDamping ( float  ratio)
inline

Sets the damping ratio of the mouse joint

See the documentation of b2JointDef for more information on the damping ratio.

Parameters
rationthe damping ratio of the mouse joint
void ObstacleSelector::setForce ( float  force)
inline

Sets the force multiplier of the mouse joint

The mouse joint will move the attached fixture with a force of this value times the object mass.

Parameters
forcethe force multiplier of the mouse joint
void ObstacleSelector::setFrequency ( float  speed)
inline

Sets the response speed of the mouse joint

See the documentation of b2JointDef for more information on the response speed.

Parameters
speedthe response speed of the mouse joint
void ObstacleSelector::setMouseSize ( const Size &  size)
inline

Sets the size of the mouse pointer

When a selection is made, this selector will create an axis-aligned bounding box centered at the mouse position. Any fixture overlapping this box will be selected. The size of this box is determined by this value.

Parameters
sizethe size of the mouse pointer

Member Data Documentation

WorldController* ObstacleSelector::_controller
protected

The WorldController associated with this selection

float ObstacleSelector::_force
protected

The amount to multiply by the mass to move the object

b2Body* ObstacleSelector::_ground
protected

A default body used as the other half of the mouse joint

b2MouseJointDef ObstacleSelector::_jointDef
protected

A reusable definition for creating a mouse joint

b2MouseJoint* ObstacleSelector::_mouseJoint
protected

The current mouse joint, if an item is selected

Rect ObstacleSelector::_pointer
protected

The region of world space to select an object from

b2Fixture* ObstacleSelector::_selection
protected

The current fixture selected by this tool (may be nullptr)


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