CUGL 2.1
Cornell University Game Library
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cugl::physics2::ObstacleSelector Class Reference

#include <CUObstacleSelector.h>

Public Member Functions

 ObstacleSelector ()
 
 ~ObstacleSelector ()
 
void dispose ()
 
bool init (const std::shared_ptr< ObstacleWorld > &world)
 
bool init (const std::shared_ptr< ObstacleWorld > &world, const Size mouseSize)
 
const Vec2 getPosition ()
 
void setPosition (float x, float y)
 
void setPosition (const Vec2 pos)
 
bool isSelected () const
 
bool select ()
 
void deselect ()
 
ObstaclegetObstacle ()
 
bool onQuery (b2Fixture *fixture)
 
float getStiffness () const
 
void setStiffness (float stiffness)
 
float getDamping () const
 
void setDamping (float ratio)
 
float getForce () const
 
void setForce (float force)
 
const Size getMouseSize () const
 
void setMouseSize (const Size size)
 
Color4 getDebugColor () const
 
void setDebugColor (Color4 color)
 
scene2::SceneNodegetDebugScene () const
 
void setDebugScene (const std::shared_ptr< scene2::SceneNode > &node)
 
void setVisible (bool flag)
 
bool isVisible ()
 
bool hasDebug ()
 

Static Public Member Functions

static std::shared_ptr< ObstacleSelectoralloc (const std::shared_ptr< ObstacleWorld > &world)
 
static std::shared_ptr< ObstacleSelectoralloc (const std::shared_ptr< ObstacleWorld > &world, const Size mouseSize)
 

Protected Member Functions

void resetDebug ()
 
void updateDebug ()
 
void updateTarget (Obstacle *obstacle)
 

Protected Attributes

std::shared_ptr< ObstacleWorld_controller
 
Vec2 _position
 
Size _size
 
float _force
 
b2Fixture * _selection
 
b2Body * _ground
 
b2MouseJointDef _jointDef
 
b2MouseJoint * _mouseJoint
 
std::shared_ptr< scene2::SceneNode_scene
 
std::shared_ptr< scene2::WireNode_hatch
 
std::shared_ptr< scene2::WireNode_connect
 
bool _dvisible
 
Color4 _dcolor
 

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 ObstacleWorld, 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()

cugl::physics2::ObstacleSelector::ObstacleSelector ( )
inline

Creates a new ObstacleSelector

The selector created is not usable. This constructor only initializes default values.

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

◆ ~ObstacleSelector()

cugl::physics2::ObstacleSelector::~ObstacleSelector ( )
inline

Disposes of this selector, releasing all resources.

Member Function Documentation

◆ alloc() [1/2]

static std::shared_ptr<ObstacleSelector> cugl::physics2::ObstacleSelector::alloc ( const std::shared_ptr< ObstacleWorld > &  world)
inlinestatic

Returns a newly allocated ObstacleSelector for the given ObstacleWorld

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

This constructor uses the default mouse size.

Parameters
worldthe physics controller
Returns
a newly allocated ObstacleSelector for the given ObstacleWorld

◆ alloc() [2/2]

static std::shared_ptr<ObstacleSelector> cugl::physics2::ObstacleSelector::alloc ( const std::shared_ptr< ObstacleWorld > &  world,
const Size  mouseSize 
)
inlinestatic

Returns a newly allocated ObstacleSelector for the given world and mouse size.

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

Parameters
worldthe physics controller
mouseSizethe mouse size
Returns
an autoreleased selector object

◆ deselect()

void cugl::physics2::ObstacleSelector::deselect ( )

Deselects the physics body, discontinuing any mouse movement.

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

◆ dispose()

void cugl::physics2::ObstacleSelector::dispose ( )

Disposes all of the resources used by this selector.

A disposed selector can be safely reinitialized.

◆ getDamping()

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

◆ getDebugColor()

Color4 cugl::physics2::ObstacleSelector::getDebugColor ( ) const
inline

Returns the color of the debug wireframe.

The default color is white, which means that the objects will be shown with a white wireframe.

Returns
the color of the debug wireframe.

◆ getDebugScene()

scene2::SceneNode* cugl::physics2::ObstacleSelector::getDebugScene ( ) const
inline

Returns the parent scene graph node for the debug wireframe

The returned node is the parent coordinate space for drawing physics. All debug nodes for physics objects are drawn within this coordinate space. Setting the visibility of this node to false will disable any debugging.

The wireframe will be drawn using physics coordinates, which is possibly much smaller than your drawing coordinates (e.g. 1 Box2D unit = 1 pixel). If you want the wireframes to be larger, you should scale the parent parent coordinate space to match the rest of the application.

This scene graph node is intended for debugging purposes only. If you want a physics body to update a proper texture image, you should either use the method Obstacle#update(float) for subclasses or Obstacle#setListener for decoupled classes.

Returns
the scene graph node for the debug wireframe

◆ getForce()

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

◆ getMouseSize()

const Size cugl::physics2::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

◆ getObstacle()

Obstacle* cugl::physics2::ObstacleSelector::getObstacle ( )

Returns a (weak) reference to 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
a (weak) reference to the Obstacle selected (if any)

◆ getPosition()

const Vec2 cugl::physics2::ObstacleSelector::getPosition ( )
inline

Returns the current position of this selector (in World space)

Returns
the current position of this selector (in World space)

◆ getStiffness()

float cugl::physics2::ObstacleSelector::getStiffness ( ) const
inline

Returns the stiffness of the mouse joint

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

Returns
the response speed of the mouse joint

◆ hasDebug()

bool cugl::physics2::ObstacleSelector::hasDebug ( )
inline

Returns true if the obstacle has a wireframe for debugging.

This method will return false if there is no active parent scene for the wireframe.

Returns
true if the obstacle has a wireframe for debugging.

◆ init() [1/2]

bool cugl::physics2::ObstacleSelector::init ( const std::shared_ptr< ObstacleWorld > &  world)
inline

Initializes a new selector for the given ObstacleWorld

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

This initializer uses the default mouse size.

Parameters
worldthe physics controller
Returns
true if the obstacle is initialized properly, false otherwise.

◆ init() [2/2]

bool cugl::physics2::ObstacleSelector::init ( const std::shared_ptr< ObstacleWorld > &  world,
const Size  mouseSize 
)

Initializes a new selector for the given ObstacleWorld and mouse size.

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

Parameters
worldthe physics controller
mouseSizethe mouse size
Returns
true if the obstacle is initialized properly, false otherwise.

◆ isSelected()

bool cugl::physics2::ObstacleSelector::isSelected ( ) const
inline

Returns true if a physics body is currently selected

Returns
true if a physics body is currently selected

◆ isVisible()

bool cugl::physics2::ObstacleSelector::isVisible ( )
inline

Returns true if the debug wireframe for this object is visible

This method is necessary for touch screen devices, where we cannot track the selector if there is no active touch.

Returns
true if the debug wireframe for this object is visible

◆ onQuery()

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

◆ resetDebug()

void cugl::physics2::ObstacleSelector::resetDebug ( )
protected

Creates the outline of the physics fixtures in the debug wireframe

The debug wireframe is use to outline the fixtures attached to this selector. It is useful when you want to visualize the relationship between the mouse and the selected shape.

◆ select()

bool cugl::physics2::ObstacleSelector::select ( )

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

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

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

◆ setDamping()

void cugl::physics2::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
ratiothe damping ratio of the mouse joint

◆ setDebugColor()

void cugl::physics2::ObstacleSelector::setDebugColor ( Color4  color)
inline

Sets the color of the debug wireframe.

The default color is white, which means that the objects will be shown with a white wireframe.

Parameters
colorthe color of the debug wireframe.

◆ setDebugScene()

void cugl::physics2::ObstacleSelector::setDebugScene ( const std::shared_ptr< scene2::SceneNode > &  node)

Sets the parent scene graph node for the debug wireframe

The given node is the parent coordinate space for drawing physics. All debug nodes for physics objects are drawn within this coordinate space. Setting the visibility of this node to false will disable any debugging. Similarly, setting this value to nullptr will disable any debugging.

The wireframe will be drawn using physics coordinates, which is possibly much smaller than your drawing coordinates (e.g. 1 Box2D unit = 1 pixel). If you want the wireframes to be larger, you should scale the parent parent coordinate space to match the rest of the application.

This scene graph node is intended for debugging purposes only. If you want a physics body to update a proper texture image, you should either use the method Obstacle#update(float) for subclasses or Obstacle#setListener for decoupled classes.

Parameters
nodehe parent scene graph node for the debug wireframe

◆ setForce()

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

◆ setMouseSize()

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

◆ setPosition() [1/2]

void cugl::physics2::ObstacleSelector::setPosition ( const Vec2  pos)
inline

Sets the current position of this selector (in World space)

Parameters
posthe position of the selector

◆ setPosition() [2/2]

void cugl::physics2::ObstacleSelector::setPosition ( float  x,
float  y 
)

Sets the current position of this selector (in World space)

Parameters
xthe x-coordinate of the selector
ythe y-coordinate of the selector

◆ setStiffness()

void cugl::physics2::ObstacleSelector::setStiffness ( float  stiffness)
inline

Sets the stiffness of the mouse joint

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

Parameters
stiffnessthe stiffness of the mouse joint

◆ setVisible()

void cugl::physics2::ObstacleSelector::setVisible ( bool  flag)
inline

Sets whether the debug wireframe for this object is visible

This method is necessary for touch screen devices, where we cannot track the selector if there is no active touch.

Parameters
flagwhether the debug wireframe for this object is visible

◆ updateDebug()

void cugl::physics2::ObstacleSelector::updateDebug ( )
protected

Repositions the debug wireframe so that it agrees with the physics object.

The debug wireframe is use to outline the fixtures attached to this selector. It is useful when you want to visualize the relationship between the mouse and the selected shape.

◆ updateTarget()

void cugl::physics2::ObstacleSelector::updateTarget ( Obstacle obstacle)
protected

Repositions the scene node so that it agrees with the physics object.

By default, the position of a node should be the body position times the draw scale. However, for some obstacles (particularly complex obstacles), it may be desirable to turn the default functionality off. Hence we have made this virtual.

Member Data Documentation

◆ _connect

std::shared_ptr<scene2::WireNode> cugl::physics2::ObstacleSelector::_connect
protected

The wireframe node for the connection

◆ _controller

std::shared_ptr<ObstacleWorld> cugl::physics2::ObstacleSelector::_controller
protected

The ObstacleWorld associated with this selection

◆ _dcolor

Color4 cugl::physics2::ObstacleSelector::_dcolor
protected

The wireframe color for debugging

◆ _dvisible

bool cugl::physics2::ObstacleSelector::_dvisible
protected

The conversion rate between physics units and drawing units Whether or not to display the debug wireframe.

◆ _force

float cugl::physics2::ObstacleSelector::_force
protected

The amount to multiply by the mass to move the object

◆ _ground

b2Body* cugl::physics2::ObstacleSelector::_ground
protected

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

◆ _hatch

std::shared_ptr<scene2::WireNode> cugl::physics2::ObstacleSelector::_hatch
protected

The wireframe node for debugging.

◆ _jointDef

b2MouseJointDef cugl::physics2::ObstacleSelector::_jointDef
protected

A reusable definition for creating a mouse joint

◆ _mouseJoint

b2MouseJoint* cugl::physics2::ObstacleSelector::_mouseJoint
protected

The current mouse joint, if an item is selected

◆ _position

Vec2 cugl::physics2::ObstacleSelector::_position
protected

The location in world space of this selector

◆ _scene

std::shared_ptr<scene2::SceneNode> cugl::physics2::ObstacleSelector::_scene
protected

The wireframe parent for debugging.

◆ _selection

b2Fixture* cugl::physics2::ObstacleSelector::_selection
protected

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

◆ _size

Size cugl::physics2::ObstacleSelector::_size
protected

The size of the selection region (for accuracy)


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