CUGL 2.1
Cornell University Game Library
|
#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 () |
Obstacle * | getObstacle () |
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::SceneNode * | getDebugScene () 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< ObstacleSelector > | alloc (const std::shared_ptr< ObstacleWorld > &world) |
static std::shared_ptr< ObstacleSelector > | alloc (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 |
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.
|
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.
|
inline |
Disposes of this selector, releasing all resources.
|
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.
world | the physics controller |
|
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.
world | the physics controller |
mouseSize | the mouse size |
void cugl::physics2::ObstacleSelector::deselect | ( | ) |
Deselects the physics body, discontinuing any mouse movement.
The body may still continue to move of its own accord.
void cugl::physics2::ObstacleSelector::dispose | ( | ) |
Disposes all of the resources used by this selector.
A disposed selector can be safely reinitialized.
|
inline |
Returns the damping ratio of the mouse joint
See the documentation of b2JointDef for more information on the damping ratio.
|
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.
|
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.
|
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.
|
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.
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.
|
inline |
Returns the current position of this selector (in World space)
|
inline |
Returns the stiffness of the mouse joint
See the documentation of b2JointDef for more information on the response speed.
|
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.
|
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.
world | the physics controller |
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.
world | the physics controller |
mouseSize | the mouse size |
|
inline |
Returns true if a physics body is currently selected
|
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.
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.
fixture | the fixture selected |
|
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.
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.
|
inline |
Sets the damping ratio of the mouse joint
See the documentation of b2JointDef for more information on the damping ratio.
ratio | the damping ratio of the mouse joint |
|
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.
color | the color of the debug wireframe. |
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.
node | he parent scene graph node for the debug wireframe |
|
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.
force | the force multiplier of the mouse joint |
|
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.
size | the size of the mouse pointer |
|
inline |
Sets the current position of this selector (in World space)
pos | the position of the selector |
void cugl::physics2::ObstacleSelector::setPosition | ( | float | x, |
float | y | ||
) |
Sets the current position of this selector (in World space)
x | the x-coordinate of the selector |
y | the y-coordinate of the selector |
|
inline |
Sets the stiffness of the mouse joint
See the documentation of b2JointDef for more information on the response speed.
stiffness | the stiffness of the mouse joint |
|
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.
flag | whether the debug wireframe for this object is visible |
|
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.
|
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.
|
protected |
The wireframe node for the connection
|
protected |
The ObstacleWorld associated with this selection
|
protected |
The wireframe color for debugging
|
protected |
The conversion rate between physics units and drawing units Whether or not to display the debug wireframe.
|
protected |
The amount to multiply by the mass to move the object
|
protected |
A default body used as the other half of the mouse joint
|
protected |
The wireframe node for debugging.
|
protected |
A reusable definition for creating a mouse joint
|
protected |
The current mouse joint, if an item is selected
|
protected |
The location in world space of this selector
|
protected |
The wireframe parent for debugging.
|
protected |
The current fixture selected by this tool (may be nullptr)
|
protected |
The size of the selection region (for accuracy)