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

#include <CUObstacle.h>

Inheritance diagram for cugl::physics2::Obstacle:
cugl::physics2::ComplexObstacle cugl::physics2::SimpleObstacle cugl::physics2::BoxObstacle cugl::physics2::CapsuleObstacle cugl::physics2::PolygonObstacle cugl::physics2::WheelObstacle

Public Member Functions

 Obstacle (void)
 
virtual ~Obstacle ()
 
virtual bool init ()
 
virtual bool init (const Vec2 vec)
 
virtual b2BodyType getBodyType () const
 
virtual void setBodyType (b2BodyType value)
 
virtual Vec2 getPosition () const
 
virtual void setPosition (const Vec2 value)
 
virtual void setPosition (float x, float y)
 
virtual float getX () const
 
virtual void setX (float value)
 
virtual float getY () const
 
virtual void setY (float value)
 
virtual float getAngle () const
 
virtual void setAngle (float value)
 
virtual Vec2 getLinearVelocity () const
 
virtual void setLinearVelocity (const Vec2 value)
 
virtual void setLinearVelocity (float x, float y)
 
virtual float getVX () const
 
virtual void setVX (float value)
 
virtual float getVY () const
 
virtual void setVY (float value)
 
virtual float getAngularVelocity () const
 
virtual void setAngularVelocity (float value)
 
virtual bool isEnabled () const
 
virtual void setEnabled (bool value)
 
virtual bool isAwake () const
 
virtual void setAwake (bool value)
 
virtual bool isSleepingAllowed () const
 
virtual void setSleepingAllowed (bool value)
 
virtual bool isBullet () const
 
virtual void setBullet (bool value)
 
virtual bool isFixedRotation () const
 
virtual void setFixedRotation (bool value)
 
virtual float getGravityScale () const
 
virtual void setGravityScale (float value)
 
virtual float getLinearDamping () const
 
virtual void setLinearDamping (float value)
 
virtual float getAngularDamping () const
 
virtual void setAngularDamping (float value)
 
void setBodyState (const b2Body &body)
 
float getDensity () const
 
virtual void setDensity (float value)
 
float getFriction () const
 
virtual void setFriction (float value)
 
float getRestitution () const
 
virtual void setRestitution (float value)
 
bool isSensor () const
 
virtual void setSensor (bool value)
 
b2Filter getFilterData () const
 
virtual void setFilterData (b2Filter value)
 
virtual Vec2 getCentroid () const
 
virtual void setCentroid (const Vec2 value)
 
virtual void setCentroid (float x, float y)
 
virtual float getInertia () const
 
virtual void setInertia (float value)
 
virtual float getMass () const
 
virtual void setMass (float value)
 
virtual void resetMass ()
 
bool isRemoved () const
 
void markRemoved (bool value)
 
bool isDirty () const
 
void markDirty (bool value)
 
virtual b2Body * getBody ()
 
virtual bool activatePhysics (b2World &world)
 
virtual void deactivatePhysics (b2World &world)
 
virtual void update (float delta)
 
const std::function< void(Obstacle *obstacle)> & getListener () const
 
void setListener (const std::function< void(Obstacle *obstacle)> &listener)
 
std::string getName () const
 
void setName (std::string value)
 
std::string toString () const
 
Color4 getDebugColor () const
 
virtual void setDebugColor (Color4 color)
 
scene2::SceneNodegetDebugScene () const
 
scene2::WireNodegetDebugNode () const
 
virtual void setDebugScene (const std::shared_ptr< scene2::SceneNode > &node)
 
bool hasDebug ()
 

Protected Member Functions

virtual void resetDebug ()
 
virtual void updateDebug ()
 

Protected Attributes

b2BodyDef _bodyinfo
 
b2FixtureDef _fixture
 
b2MassData _massdata
 
bool _masseffect
 
std::shared_ptr< scene2::SceneNode_scene
 
std::shared_ptr< scene2::WireNode_debug
 
Color4 _dcolor
 
std::string _tag
 
std::function< void(Obstacle *obstacle)> _listener
 

Friends

std::ostream & operator<< (std::ostream &os, const Obstacle &obj)
 

Detailed Description

Base model class to support collisions.

Instances represents a body and/or a group of bodies. There should be NO game controlling logic code in a physics objects. That should reside in the Controllers.

This abstract class has no Body or Shape information and should never be instantiated directly. Instead, you should instantiate either SimpleObstacle or ComplexObstacle. This class only exists to unify common functionality. In particular, it wraps the body and and fixture information into a single interface.

Many of the method comments in this class are taken from the Box2d manual by Erin Catto (2011).

Constructor & Destructor Documentation

◆ Obstacle()

cugl::physics2::Obstacle::Obstacle ( void  )

Creates a new physics object at the origin.

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

◆ ~Obstacle()

virtual cugl::physics2::Obstacle::~Obstacle ( )
virtual

Deletes this physics object and all of its resources.

We have to make the destructor public so that we can polymorphically delete physics objects.

A non-default destructor is necessary since we must release all claims on scene graph nodes.

Member Function Documentation

◆ activatePhysics()

virtual bool cugl::physics2::Obstacle::activatePhysics ( b2World &  world)
inlinevirtual

Creates the physics Body(s) for this object, adding them to the world.

Implementations of this method should NOT retain ownership of the Box2D world. That is a tight coupling that we should avoid.

Parameters
worldBox2D world to store body
Returns
true if object allocation succeeded

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ deactivatePhysics()

virtual void cugl::physics2::Obstacle::deactivatePhysics ( b2World &  world)
inlinevirtual

Destroys the physics Body(s) of this object if applicable.

This removes the body from the Box2D world.

Parameters
worldBox2D world that stores body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getAngle()

virtual float cugl::physics2::Obstacle::getAngle ( ) const
inlinevirtual

Returns the angle of rotation for this body (about the center).

The value returned is in radians

Returns
the angle of rotation for this body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getAngularDamping()

virtual float cugl::physics2::Obstacle::getAngularDamping ( ) const
inlinevirtual

Returns the angular damping for this body.

Angular damping is use to reduce the angular velocity. Damping is different than friction because friction only occurs with contact. Damping is not a replacement for friction and the two effects should be used together.

Damping parameters should be between 0 and infinity, with 0 meaning no damping, and infinity meaning full damping. Normally you will use a damping value between 0 and 0.1.

Returns
the angular damping for this body.

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getAngularVelocity()

virtual float cugl::physics2::Obstacle::getAngularVelocity ( ) const
inlinevirtual

Returns the angular velocity for this physics body

The rate of change is measured in radians per step

Returns
the angular velocity for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getBody()

virtual b2Body* cugl::physics2::Obstacle::getBody ( )
inlinevirtual

Returns a (weak) reference to Box2D body for this obstacle.

You use this body to add joints and apply forces. As a weak reference, this physics obstacle does not transfer ownership of this body. In addition, the value may be a nullptr.

Returns
a (weak) reference to Box2D body for this obstacle.

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getBodyType()

virtual b2BodyType cugl::physics2::Obstacle::getBodyType ( ) const
inlinevirtual

Returns the body type for Box2D physics

If you want to lock a body in place (e.g. a platform) set this value to STATIC. KINEMATIC allows the object to move (and some limited collisions), but ignores external forces (e.g. gravity). DYNAMIC makes this is a full-blown physics object.

Returns
the body type for Box2D physics

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getCentroid()

virtual Vec2 cugl::physics2::Obstacle::getCentroid ( ) const
inlinevirtual

Returns the center of mass of this body

This method converts from a Box2D vector type to a CUGL vector type. This cuts down on the confusion between vector types. It also means that changes to the returned vector will have no effect on this object.

Returns
the center of mass for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getDebugColor()

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

◆ getDebugNode()

scene2::WireNode* cugl::physics2::Obstacle::getDebugNode ( ) const
inline

Returns the scene graph node for the debug wireframe

The returned node draws a wireframe of the physics body. The wireframe consists of the physics fixtures adjusted by the drawing scale. The debug node is positioned in the coordinate space of the parent scene.

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 update(float) for subclasses or setListener for decoupled classes.

Returns
the scene graph node for the debug wireframe

◆ getDebugScene()

scene2::SceneNode* cugl::physics2::Obstacle::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 update(float) for subclasses or setListener for decoupled classes.

Returns
the scene graph node for the debug wireframe

◆ getDensity()

float cugl::physics2::Obstacle::getDensity ( ) const
inline

Returns the density of this body

The density is typically measured in usually in kg/m^2. The density can be zero or positive. You should generally use similar densities for all your fixtures. This will improve stacking stability.

Returns
the density of this body

◆ getFilterData()

b2Filter cugl::physics2::Obstacle::getFilterData ( ) const
inline

Returns the filter data for this object (or null if there is none)

Collision filtering allows you to prevent collision between fixtures. For example, say you make a character that rides a bicycle. You want the bicycle to collide with the terrain and the character to collide with the terrain, but you don't want the character to collide with the bicycle (because they must overlap). Box2D supports such collision filtering using categories and groups.

Returns
the filter data for this object (or null if there is none)

◆ getFriction()

float cugl::physics2::Obstacle::getFriction ( ) const
inline

Returns the friction coefficient of this body

The friction parameter is usually set between 0 and 1, but can be any non-negative value. A friction value of 0 turns off friction and a value of 1 makes the friction strong. When the friction force is computed between two shapes, Box2D must combine the friction parameters of the two parent fixtures. This is done with the geometric mean.

Returns
the friction coefficient of this body

◆ getGravityScale()

virtual float cugl::physics2::Obstacle::getGravityScale ( ) const
inlinevirtual

Returns the gravity scale to apply to this body

This allows isolated objects to float. Be careful with this, since increased gravity can decrease stability.

Returns
the gravity scale to apply to this body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getInertia()

virtual float cugl::physics2::Obstacle::getInertia ( ) const
inlinevirtual

Returns the rotational inertia of this body

For static bodies, the mass and rotational inertia are set to zero. When a body has fixed rotation, its rotational inertia is zero.

Returns
the rotational inertia of this body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getLinearDamping()

virtual float cugl::physics2::Obstacle::getLinearDamping ( ) const
inlinevirtual

Returns the linear damping for this body.

Linear damping is use to reduce the linear velocity. Damping is different than friction because friction only occurs with contact. Damping is not a replacement for friction and the two effects should be used together.

Damping parameters should be between 0 and infinity, with 0 meaning no damping, and infinity meaning full damping. Normally you will use a damping value between 0 and 0.1. Most people avoid linear damping because it makes bodies look floaty.

Returns
the linear damping for this body.

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getLinearVelocity()

virtual Vec2 cugl::physics2::Obstacle::getLinearVelocity ( ) const
inlinevirtual

Returns the linear velocity for this physics body

This method converts from a Box2D vector type to a CUGL vector type. This cuts down on the confusion between vector types. It also means that changes to the returned vector will have no effect on this object.

Returns
the linear velocity for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getListener()

const std::function<void(Obstacle* obstacle)>& cugl::physics2::Obstacle::getListener ( ) const
inline

Returns the active listener to this object.

Listeners are called after every physics update, to notify them of any changes in this object state. For performance reasons, a physics obstacle can have only one listener. If you need multiple objects listening to a single physics obstacle, the listener should handle the dispatch to other objects.

Returns
the active listener to this object.

◆ getMass()

virtual float cugl::physics2::Obstacle::getMass ( ) const
inlinevirtual

Returns the mass of this body

The value is usually in kilograms.

Returns
the mass of this body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getName()

std::string cugl::physics2::Obstacle::getName ( ) const
inline

Returns the physics object tag.

A tag is a string attached to an object, in order to identify it in debugging.

Returns
the physics object tag.

◆ getPosition()

virtual Vec2 cugl::physics2::Obstacle::getPosition ( ) const
inlinevirtual

Returns the current position for this physics body

This method converts from a Box2D vector type to a CUGL vector type. This cuts down on the confusion between vector types. It also means that changes to the returned vector will have no effect on this obstacle.

Returns
the current position for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getRestitution()

float cugl::physics2::Obstacle::getRestitution ( ) const
inline

Returns the restitution of this body

Restitution is used to make objects bounce. The restitution value is usually set to be between 0 and 1. Consider dropping a ball on a table. A value of zero means the ball won't bounce. This is called an inelastic collision. A value of one means the ball's velocity will be exactly reflected. This is called a perfectly elastic collision.

Returns
the restitution of this body

◆ getVX()

virtual float cugl::physics2::Obstacle::getVX ( ) const
inlinevirtual

Returns the x-velocity for this physics body

Returns
the x-velocity for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getVY()

virtual float cugl::physics2::Obstacle::getVY ( ) const
inlinevirtual

Returns the y-velocity for this physics body

Returns
the y-velocity for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getX()

virtual float cugl::physics2::Obstacle::getX ( ) const
inlinevirtual

Returns the x-coordinate for this physics body

Returns
the x-coordinate for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ getY()

virtual float cugl::physics2::Obstacle::getY ( ) const
inlinevirtual

Returns the y-coordinate for this physics body

Returns
the y-coordinate for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ hasDebug()

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

virtual bool cugl::physics2::Obstacle::init ( )
inlinevirtual

Initializes a new physics object at the origin.

Returns
true if the obstacle is initialized properly, false otherwise.

Reimplemented in cugl::physics2::CapsuleObstacle, cugl::physics2::BoxObstacle, and cugl::physics2::WheelObstacle.

◆ init() [2/2]

virtual bool cugl::physics2::Obstacle::init ( const Vec2  vec)
virtual

Initializes a new physics object at the given point

Parameters
vecInitial position in world coordinates
Returns
true if the obstacle is initialized properly, false otherwise.

Reimplemented in cugl::physics2::CapsuleObstacle, cugl::physics2::BoxObstacle, and cugl::physics2::WheelObstacle.

◆ isAwake()

virtual bool cugl::physics2::Obstacle::isAwake ( ) const
inlinevirtual

Returns true if the body is awake

An sleeping body is one that has come to rest and the physics engine has decided to stop simulating it to save CPU cycles. If a body is awake and collides with a sleeping body, then the sleeping body wakes up. Bodies will also wake up if a joint or contact attached to them is destroyed.
You can also wake a body manually.

Returns
true if the body is awake

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ isBullet()

virtual bool cugl::physics2::Obstacle::isBullet ( ) const
inlinevirtual

Returns true if this body is a bullet

By default, Box2D uses continuous collision detection (CCD) to prevent dynamic bodies from tunneling through static bodies. Normally CCD is not used between dynamic bodies. This is done to keep performance reasonable. In some game scenarios you need dynamic bodies to use CCD. For example, you may want to shoot a high speed bullet at a stack of dynamic bricks. Without CCD, the bullet might tunnel through the bricks.

Fast moving objects in Box2D can be labeled as bullets. Bullets will perform CCD with both static and dynamic bodies. You should decide what bodies should be bullets based on your game design.

Returns
true if this body is a bullet

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ isDirty()

bool cugl::physics2::Obstacle::isDirty ( ) const
inline

Returns true if the shape information must be updated.

Attributes tied to the geometry (and not just forces/position) must wait for collisions to complete before they are reset. Shapes (and their properties) are reset in the update method.

Returns
true if the shape information must be updated.

◆ isEnabled()

virtual bool cugl::physics2::Obstacle::isEnabled ( ) const
inlinevirtual

Returns true if the body is enabled

A disabled body not participate in collision or dynamics. This state is similar to sleeping except the body will not be woken by other bodies and the body's fixtures will not be placed in the broad-phase. This means the body will not participate in collisions, ray casts, etc.

Returns
true if the body is enabled

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ isFixedRotation()

virtual bool cugl::physics2::Obstacle::isFixedRotation ( ) const
inlinevirtual

Returns true if this body be prevented from rotating

This is very useful for characters that should remain upright.

Returns
true if this body be prevented from rotating

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ isRemoved()

bool cugl::physics2::Obstacle::isRemoved ( ) const
inline

Returns true if our object has been flagged for garbage collection

A garbage collected object will be removed from the physics world at the next time step.

Returns
true if our object has been flagged for garbage collection

◆ isSensor()

bool cugl::physics2::Obstacle::isSensor ( ) const
inline

Returns true if this object is a sensor.

Sometimes game logic needs to know when two entities overlap yet there should be no collision response. This is done by using sensors. A sensor is an entity that detects collision but does not produce a response.

Returns
true if this object is a sensor.

◆ isSleepingAllowed()

virtual bool cugl::physics2::Obstacle::isSleepingAllowed ( ) const
inlinevirtual

Returns false if this body should never fall asleep

An sleeping body is one that has come to rest and the physics engine has decided to stop simulating it to save CPU cycles. If a body is awake and collides with a sleeping body, then the sleeping body wakes up. Bodies will also wake up if a joint or contact attached to them is destroyed. You can also wake a body manually.

Returns
false if this body should never fall asleep

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ markDirty()

void cugl::physics2::Obstacle::markDirty ( bool  value)
inline

Sets whether the shape information must be updated.

Attributes tied to the geometry (and not just forces/position) must wait for collisions to complete before they are reset. Shapes (and their properties) are reset in the update method.

Parameters
valuewhether the shape information must be updated.

◆ markRemoved()

void cugl::physics2::Obstacle::markRemoved ( bool  value)
inline

Sets whether our object has been flagged for garbage collection

A garbage collected object will be removed from the physics world at the next time step.

Parameters
valuewhether our object has been flagged for garbage collection

◆ resetDebug()

virtual void cugl::physics2::Obstacle::resetDebug ( )
inlineprotectedvirtual

Creates the outline of the physics fixtures in the debug wireframe

The debug wireframe 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).

Reimplemented in cugl::physics2::CapsuleObstacle, cugl::physics2::ComplexObstacle, cugl::physics2::PolygonObstacle, cugl::physics2::BoxObstacle, and cugl::physics2::WheelObstacle.

◆ resetMass()

virtual void cugl::physics2::Obstacle::resetMass ( )
inlinevirtual

Resets this body to use the mass computed from the its shape and density

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setAngle()

virtual void cugl::physics2::Obstacle::setAngle ( float  value)
inlinevirtual

Sets the angle of rotation for this body (about the center).

Parameters
valuethe angle of rotation for this body (in radians)

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setAngularDamping()

virtual void cugl::physics2::Obstacle::setAngularDamping ( float  value)
inlinevirtual

Sets the angular damping for this body.

Angular damping is use to reduce the angular velocity. Damping is different than friction because friction only occurs with contact. Damping is not a replacement for friction and the two effects should be used together.

Damping parameters should be between 0 and infinity, with 0 meaning no damping, and infinity meaning full damping. Normally you will use a damping value between 0 and 0.1.

Parameters
valuethe angular damping for this body.

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setAngularVelocity()

virtual void cugl::physics2::Obstacle::setAngularVelocity ( float  value)
inlinevirtual

Sets the angular velocity for this physics body

Parameters
valuethe angular velocity for this physics body (in radians)

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setAwake()

virtual void cugl::physics2::Obstacle::setAwake ( bool  value)
inlinevirtual

Sets whether the body is awake

An sleeping body is one that has come to rest and the physics engine has decided to stop simulating it to save CPU cycles. If a body is awake and collides with a sleeping body, then the sleeping body wakes up. Bodies will also wake up if a joint or contact attached to them is destroyed. You can also wake a body manually.

Parameters
valuewhether the body is awake

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setBodyState()

void cugl::physics2::Obstacle::setBodyState ( const b2Body &  body)

Copies the state from the given body to the body def.

This is important if you want to save the state of the body before removing it from the world.

◆ setBodyType()

virtual void cugl::physics2::Obstacle::setBodyType ( b2BodyType  value)
inlinevirtual

Sets the body type for Box2D physics

If you want to lock a body in place (e.g. a platform) set this value to STATIC. KINEMATIC allows the object to move (and some limited collisions), but ignores external forces (e.g. gravity). DYNAMIC makes this is a full-blown physics object.

Parameters
valuethe body type for Box2D physics

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setBullet()

virtual void cugl::physics2::Obstacle::setBullet ( bool  value)
inlinevirtual

Sets whether this body is a bullet

By default, Box2D uses continuous collision detection (CCD) to prevent dynamic bodies from tunneling through static bodies. Normally CCD is not used between dynamic bodies. This is done to keep performance reasonable. In some game scenarios you need dynamic bodies to use CCD. For example, you may want to shoot a high speed bullet at a stack of dynamic bricks. Without CCD, the bullet might tunnel through the bricks.

Fast moving objects in Box2D can be labeled as bullets. Bullets will perform CCD with both static and dynamic bodies. You should decide what bodies should be bullets based on your game design.

Parameters
valuewhether this body is a bullet

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setCentroid() [1/2]

virtual void cugl::physics2::Obstacle::setCentroid ( const Vec2  value)
inlinevirtual

Sets the center of mass for this physics body

This method converts from a CUGL vector type to a Box2D vector type. This cuts down on the confusion between vector types.

Parameters
valuethe center of mass for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setCentroid() [2/2]

virtual void cugl::physics2::Obstacle::setCentroid ( float  x,
float  y 
)
virtual

Sets the center of mass for this physics body

Parameters
xthe x-coordinate of the center of mass for this physics body
ythe y-coordinate of the center of mass for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setDebugColor()

virtual void cugl::physics2::Obstacle::setDebugColor ( Color4  color)
virtual

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.

Reimplemented in cugl::physics2::ComplexObstacle.

◆ setDebugScene()

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

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 update(float) for subclasses or setListener for decoupled classes.

Parameters
nodehe parent scene graph node for the debug wireframe

Reimplemented in cugl::physics2::ComplexObstacle.

◆ setDensity()

virtual void cugl::physics2::Obstacle::setDensity ( float  value)
inlinevirtual

Sets the density of this body

The density is typically measured in usually in kg/m^2. The density can be zero or positive. You should generally use similar densities for all your fixtures. This will improve stacking stability.

Parameters
valuethe density of this body

Reimplemented in cugl::physics2::ComplexObstacle, cugl::physics2::SimpleObstacle, and cugl::physics2::CapsuleObstacle.

◆ setEnabled()

virtual void cugl::physics2::Obstacle::setEnabled ( bool  value)
inlinevirtual

Sets whether the body is enabled

A disabled body not participate in collision or dynamics. This state is similar to sleeping except the body will not be woken by other bodies and the body's fixtures will not be placed in the broad-phase. This means the body will not participate in collisions, ray casts, etc.

Parameters
valuewhether the body is enabled

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setFilterData()

virtual void cugl::physics2::Obstacle::setFilterData ( b2Filter  value)
inlinevirtual

Sets the filter data for this object

Collision filtering allows you to prevent collision between fixtures. For example, say you make a character that rides a bicycle. You want the bicycle to collide with the terrain and the character to collide with the terrain, but you don't want the character to collide with the bicycle (because they must overlap). Box2D supports such collision filtering using categories and groups.

A value of null removes all collision filters.

Parameters
valuethe filter data for this object

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setFixedRotation()

virtual void cugl::physics2::Obstacle::setFixedRotation ( bool  value)
inlinevirtual

Sets whether this body be prevented from rotating

This is very useful for characters that should remain upright.

Parameters
valuewhether this body be prevented from rotating

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setFriction()

virtual void cugl::physics2::Obstacle::setFriction ( float  value)
inlinevirtual

Sets the friction coefficient of this body

The friction parameter is usually set between 0 and 1, but can be any non-negative value. A friction value of 0 turns off friction and a value of 1 makes the friction strong. When the friction force is computed between two shapes, Box2D must combine the friction parameters of the two parent fixtures. This is done with the geometric mean.

Parameters
valuethe friction coefficient of this body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setGravityScale()

virtual void cugl::physics2::Obstacle::setGravityScale ( float  value)
inlinevirtual

Sets the gravity scale to apply to this body

This allows isolated objects to float. Be careful with this, since increased gravity can decrease stability.

Parameters
valuethe gravity scale to apply to this body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setInertia()

virtual void cugl::physics2::Obstacle::setInertia ( float  value)
virtual

Sets the rotational inertia of this body

For static bodies, the mass and rotational inertia are set to zero. When a body has fixed rotation, its rotational inertia is zero.

Parameters
valuethe rotational inertia of this body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setLinearDamping()

virtual void cugl::physics2::Obstacle::setLinearDamping ( float  value)
inlinevirtual

Sets the linear damping for this body.

Linear damping is use to reduce the linear velocity. Damping is different than friction because friction only occurs with contact. Damping is not a replacement for friction and the two effects should be used together.

Damping parameters should be between 0 and infinity, with 0 meaning no damping, and infinity meaning full damping. Normally you will use a damping value between 0 and 0.1. Most people avoid linear damping because it makes bodies look floaty.

Parameters
valuethe linear damping for this body.

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setLinearVelocity() [1/2]

virtual void cugl::physics2::Obstacle::setLinearVelocity ( const Vec2  value)
inlinevirtual

Sets the linear velocity for this physics body

This method converts from a CUGL vector type to a Box2D vector type. This cuts down on the confusion between vector types.

Parameters
valuethe linear velocity for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setLinearVelocity() [2/2]

virtual void cugl::physics2::Obstacle::setLinearVelocity ( float  x,
float  y 
)
inlinevirtual

Sets the linear velocity for this physics body

Parameters
xthe x-coordinate of the linear velocity
ythe y-coordinate of the linear velocity

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setListener()

void cugl::physics2::Obstacle::setListener ( const std::function< void(Obstacle *obstacle)> &  listener)
inline

Sets the active listener to this object.

Listeners are called after every physics update, to notify them of any changes in this object state. For performance reasons, a physics obstacle can have only one listener. If you need multiple objects listening to a single physics obstacle, the listener should handle the dispatch to other objects.

Parameters
listenerthe active listener to this object.

◆ setMass()

virtual void cugl::physics2::Obstacle::setMass ( float  value)
virtual

Sets the mass of this body

The value is usually in kilograms.

Parameters
valuethe mass of this body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setName()

void cugl::physics2::Obstacle::setName ( std::string  value)
inline

Sets the physics object tag.

A tag is a string attached to an object, in order to identify it in debugging.

Parameters
valuethe physics object tag

◆ setPosition() [1/2]

virtual void cugl::physics2::Obstacle::setPosition ( const Vec2  value)
inlinevirtual

Sets the current position for this physics body

This method converts from a CUGL vector type to a Box2D vector type. This cuts down on the confusion between vector types.

Parameters
valuethe current position for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setPosition() [2/2]

virtual void cugl::physics2::Obstacle::setPosition ( float  x,
float  y 
)
inlinevirtual

Sets the current position for this physics body

Parameters
xthe current x-coordinate for this physics body
ythe current y-coordinate for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setRestitution()

virtual void cugl::physics2::Obstacle::setRestitution ( float  value)
inlinevirtual

Sets the restitution of this body

Restitution is used to make objects bounce. The restitution value is usually set to be between 0 and 1. Consider dropping a ball on a table. A value of zero means the ball won't bounce. This is called an inelastic collision. A value of one means the ball's velocity will be exactly reflected. This is called a perfectly elastic collision.

Parameters
valuethe restitution of this body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setSensor()

virtual void cugl::physics2::Obstacle::setSensor ( bool  value)
inlinevirtual

Sets whether this object is a sensor.

Sometimes game logic needs to know when two entities overlap yet there should be no collision response. This is done by using sensors. A sensor is an entity that detects collision but does not produce a response.

Parameters
valuewhether this object is a sensor.

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setSleepingAllowed()

virtual void cugl::physics2::Obstacle::setSleepingAllowed ( bool  value)
inlinevirtual

Sets whether the body should ever fall asleep

An sleeping body is one that has come to rest and the physics engine has decided to stop simulating it to save CPU cycles. If a body is awake and collides with a sleeping body, then the sleeping body wakes up. Bodies will also wake up if a joint or contact attached to them is destroyed. You can also wake a body manually.

Parameters
valuewhether the body should ever fall asleep

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setVX()

virtual void cugl::physics2::Obstacle::setVX ( float  value)
inlinevirtual

Sets the x-velocity for this physics body

Parameters
valuethe x-velocity for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setVY()

virtual void cugl::physics2::Obstacle::setVY ( float  value)
inlinevirtual

Sets the y-velocity for this physics body

Parameters
valuethe y-velocity for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setX()

virtual void cugl::physics2::Obstacle::setX ( float  value)
inlinevirtual

Sets the x-coordinate for this physics body

Parameters
valuethe x-coordinate for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ setY()

virtual void cugl::physics2::Obstacle::setY ( float  value)
inlinevirtual

Sets the y-coordinate for this physics body

Parameters
valuethe y-coordinate for this physics body

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ toString()

std::string cugl::physics2::Obstacle::toString ( ) const

Returns a string representation of this physics object.

This method converts the physics object into a string for debugging. By default it shows the tag and position. Other physics objects may want to override this method for more detailed information.

Returns
a string representation of this physics object

◆ update()

virtual void cugl::physics2::Obstacle::update ( float  delta)
inlinevirtual

Updates the object's physics state (NOT GAME LOGIC).

This method is called AFTER the collision resolution state. Therefore, it should not be used to process actions or any other gameplay information.
Its primary purpose is to adjust changes to the fixture, which have to take place after collision.

In other words, this is the method that updates the scene graph. If you forget to call it, it will not draw your changes.

Parameters
deltaTiming values from parent loop

Reimplemented in cugl::physics2::ComplexObstacle, and cugl::physics2::SimpleObstacle.

◆ updateDebug()

virtual void cugl::physics2::Obstacle::updateDebug ( )
protectedvirtual

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

The debug wireframe 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).

Reimplemented in cugl::physics2::SimpleObstacle, and cugl::physics2::ComplexObstacle.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Obstacle obj 
)
friend

Outputs this physics object to the given output stream.

This function uses the toString() method to convert the physics object into a string

Parameters
osthe output stream
objthe physics object to ouput
Returns
the output stream

Member Data Documentation

◆ _bodyinfo

b2BodyDef cugl::physics2::Obstacle::_bodyinfo
protected

Stores the body information for this shape

◆ _dcolor

Color4 cugl::physics2::Obstacle::_dcolor
protected

The wireframe color for debugging

◆ _debug

std::shared_ptr<scene2::WireNode> cugl::physics2::Obstacle::_debug
protected

The wireframe node for debugging.

◆ _fixture

b2FixtureDef cugl::physics2::Obstacle::_fixture
protected

Stores the fixture information for this shape

◆ _listener

std::function<void(Obstacle* obstacle)> cugl::physics2::Obstacle::_listener
protected

(Singular) callback function for state updates

◆ _massdata

b2MassData cugl::physics2::Obstacle::_massdata
protected

The mass data of this shape (which may override the fixture)

◆ _masseffect

bool cugl::physics2::Obstacle::_masseffect
protected

Whether or not to use the custom mass data

◆ _scene

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

The wireframe parent for debugging.

◆ _tag

std::string cugl::physics2::Obstacle::_tag
protected

A tag for debugging purposes


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