![]() |
CUGL 4.0
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) |
| virtual void | detachWorld (const ObstacleWorld *world) override |
| const Vec2 & | getPosition () const |
| void | setPosition (float x, float y) |
| void | setPosition (const Vec2 &pos) |
| const Vec2 | getWindowPosition () const |
| void | setWindowPosition (float x, float y) |
| void | setWindowPosition (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) |
| float | getPhysicsUnits () const |
| void | setPhysicsUnits (float units) |
| const graphics::Mesh< graphics::SpriteVertex > & | getDebugMesh () |
| const Affine2 | getGraphicsTransform () |
| virtual void | detachWorld (const ObstacleWorld *world) |
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 | resetMesh () |
| 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 |
| graphics::Mesh< graphics::SpriteVertex > | _mesh |
| float | _units |
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.
|
overridevirtual |
Detatches the given obstacle world from this object.
This method ensures safe clean-up.
| world | The world to detatch. |
Reimplemented from cugl::physics2::WorldObject.
| 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.
| const graphics::Mesh< graphics::SpriteVertex > & cugl::physics2::ObstacleSelector::getDebugMesh | ( | ) |
Returns a wireframe mesh representing this selector.
This mesh can be drawn by a graphics::SpriteBatch to help with debugging. The wireframe will consist of the selector AABB (as a cross-hair) plus a line from the selector to the selected body (if any).
|
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.
| const Affine2 cugl::physics2::ObstacleSelector::getGraphicsTransform | ( | ) |
Returns a transform for displaying graphics attached to this obstacle.
Obstacles are generally used to transform a 2d graphics object on the screen. This includes getDebugMesh. Passing this transform to a graphics::SpriteBatch will display the object in its proper location.
|
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 physics units for this obstacle selector
Physics units are the number of pixels per box2d unit. These values are used to create the debug wireframe. They also allow us to use this selector to pick an object in screen coordinates.
|
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 the current position of this selector in pixels.
This value is the position multiplied by getPhysicsUnits.
|
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
| 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 selector as a wireframe mesh.
The debug mesh 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). This mesh can then be passed to a graphics::SpriteBatch for drawing.
| 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 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 |
| void cugl::physics2::ObstacleSelector::setPhysicsUnits | ( | float | units | ) |
Sets the physics units for this obstacle selector
Physics units are the number of pixels per box2d unit. These values are used to create the debug wireframe. However, they are also useful for any other object that you wish to attach to this obstacle.
| units | The physics units for this obstacle selector |
|
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 the current position of this selector in pixels.
This value is the position multiplied by getPhysicsUnits.
| pos | the position of the selector |
|
inline |
Sets the current position of this selector in pixels.
This value is the position multiplied by getPhysicsUnits.
| x | the x-coordinate of the selector |
| y | the y-coordinate of the selector |
|
protected |
Repositions the tail of the debug wireframe
The last vertex in the debug wireframe is the offset from the selector position to the selected target
| obstacle | The obstacle to match |
|
protected |
The ObstacleWorld associated with this selection
|
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 |
A reusable definition for creating a mouse joint
|
protected |
A sprite mesh for debugging purposes
|
protected |
The current mouse joint, if an item is selected
|
protected |
The location in world space of this selector
|
protected |
The current fixture selected by this tool (may be nullptr)
|
protected |
The size of the selection region (for accuracy)
|
protected |
The physics units for this selector