CUGL 2.5
Cornell University Game Library
Loading...
Searching...
No Matches
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::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)
 
void setShared (bool shared)
 
bool isShared () const
 
void clearSharingDirtyBits ()
 
bool hasDirtyType () const
 
bool hasDirtyPosition () const
 
bool hasDirtyVelocity () const
 
bool hasDirtyAngle () const
 
bool hasDirtyAngularVelocity () const
 
bool hasDirtyBool () const
 
bool hasDirtyFloat () const
 
virtual b2Body * getBody ()
 
virtual bool activatePhysics (b2World &world)
 
virtual void deactivatePhysics (b2World &world)
 
virtual void createFixtures ()
 
virtual void releaseFixtures ()
 
virtual void update (float delta)
 
const std::function< void(Obstacle *obstacle)> & getListener () const
 
void setListener (const std::function< void(Obstacle *obstacle)> &listener)
 
int getPositionSnap ()
 
void setPositionSnap (unsigned int snap)
 
int getAngleSnap ()
 
void setAngleSnap (unsigned int snap)
 
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
 
b2Body * _body
 
int _posSnap
 
unsigned long _posFact
 
int _angSnap
 
unsigned long _angFact
 
bool _shared
 

Friends

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

Detailed Description

Base model class to support collisions.

Instances of this class represent a body. There should be NO game controlling logic code in a physics objects. That should reside in the Controllers.

This is an instance of a Physics object with just one body. It does not have any joints. It is the primary type of physics object. This class does not provide Shape information, and should not be instantiated directly.

In addition to combining body and fixture information, this class provides some basic primitives for synchronizing bodies between multiple physics worlds. See setShared.

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. Note that we do not allow an obstacle to be deleted while physics is still active. Doing so will result in an error.

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)
virtual

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

◆ clearSharingDirtyBits()

void cugl::physics2::Obstacle::clearSharingDirtyBits ( )
inline

Clears all of the bits tracking the shared state.

This is done after a physics object has been synchronized across the appropriate worlds.

◆ createFixtures()

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

Create new fixtures for this body, defining the shape

This is the primary method to override for custom physics objects.

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

◆ deactivatePhysics()

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

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

This removes the body from the Box2D world.

Parameters
worldBox2D world that stores body

◆ 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

◆ getAngleSnap()

int cugl::physics2::Obstacle::getAngleSnap ( )
inline

Returns the number of decimal places to snap rotation to the physics body

Physics bodies will have very precise angles, but these fractional angles may not be ideal for drawing, and may produce artifacts. When the value of snap is nonnegative, CUGL will round the rotation (measured in degrees, as that is the value used by images) of the image to snap decimal places.

For example, if the snap is 0, it will always round the angle to the nearest degree. If it is 1, it will round it to the nearest 10th of a degree. If it is -1 (or any negative value) it will not snap at all.

Returns
the number of decimal places to snap rotation to the physics body

◆ 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.

◆ 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

◆ 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.

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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.

◆ 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

◆ 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

◆ 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

◆ getPositionSnap()

int cugl::physics2::Obstacle::getPositionSnap ( )
inline

Returns the number of decimal places to snap the node to the physics body

Physics bodies will have very precise positions, but these fractional positions may not be ideal for drawing, and may produce artifacts. When the value of snap is nonnegative, CUGL will round the position of the node to snap decimal places.

For example, if the snap is 0, it will always round position to the nearest integer. If it is 1, it will round it to the nearest 10th of a point. If it is -1 (or any negative value) it will not snap at all.

Returns
the number of decimal places to snap the node to the physics body

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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.

◆ hasDirtyAngle()

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

Returns true if the angle of this obstacle is dirty

This happens if the angle is modified outside of the physics simulation.

Returns
true if the angle of this obstacle is dirty

◆ hasDirtyAngularVelocity()

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

Returns true if the angular velocity of this obstacle is dirty

This happens if the angular velocity is modified outside of the physics simulation.

Returns
true if the angular velocity of this obstacle is dirty

◆ hasDirtyBool()

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

Returns true if any boolean attribute of this obstacle is dirty

This happens if the attribute is modified outside of the physics simulation.

Returns
true if the boolean attribute of this obstacle is dirty

◆ hasDirtyFloat()

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

Returns true if any float attribute of this obstacle is dirty

This happens if the attribute is modified outside of the physics simulation.

Returns
true if the float attribute of this obstacle is dirty

◆ hasDirtyPosition()

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

Returns true if the positional data of this obstacle is dirty

This happens if the position is modified outside of the physics simulation.

Returns
true if the positional data of this obstacle is dirty

◆ hasDirtyType()

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

Returns true if the body type of this obstacle is dirty

This happens if the body type is modified outside of the physics simulation.

Returns
true if the body type of this obstacle is dirty

◆ hasDirtyVelocity()

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

Returns true if the velocity data of this obstacle is dirty

This happens if the velocity is modified outside of the physics simulation.

Returns
true if the velocity data of this obstacle is dirty

◆ 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::PolygonObstacle, cugl::physics2::BoxObstacle, cugl::physics2::CapsuleObstacle, 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::BoxObstacle, cugl::physics2::CapsuleObstacle, cugl::physics2::WheelObstacle, and cugl::physics2::PolygonObstacle.

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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.

◆ isShared()

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

Returns whether this object is shared across multiple physics worlds.

If true, this object will keep additional information about it's state changes. In particular, we track any change to a physics object that is not a result of the physics simulation itself.

Returns
whether this object is shared across multiple physics worlds.

◆ 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

◆ 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

◆ releaseFixtures()

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

Release the fixtures for this body, reseting the shape

This is the primary method to override for custom physics objects.

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

◆ 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::BoxObstacle, cugl::physics2::CapsuleObstacle, cugl::physics2::PolygonObstacle, 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

◆ 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)

◆ setAngleSnap()

void cugl::physics2::Obstacle::setAngleSnap ( unsigned int  snap)
inline

Sets the number of decimal places to snap rotation to the physics body

Physics bodies will have very precise angles, but these fractional angles may not be ideal for drawing, and may produce artifacts. When the value of snap is nonnegative, CUGL will round the rotation (measured in degrees, as that is the value used by images) of the image to snap decimal places.

For example, if the snap is 0, it will always round the angle to the nearest degree. If it is 1, it will round it to the nearest 10th of a degree. If it is -1 (or any negative value) it will not snap at all.

Parameters
snapthe number of decimal places to snap rotation to the physics body

◆ 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.

◆ 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)

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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.

◆ 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

◆ setDensity()

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

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::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

◆ setFilterData()

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

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

◆ 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

◆ setFriction()

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

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

◆ 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

◆ 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

◆ 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.

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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

◆ setPositionSnap()

void cugl::physics2::Obstacle::setPositionSnap ( unsigned int  snap)
inline

Sets the number of decimal places to snap the node to the physics body

Physics bodies will have very precise positions, but these fractional positions may not be ideal for drawing, and may produce artifacts. When the value of snap is nonnegative, CUGL will round the position of the node to snap decimal places.

For example, if the snap is 0, it will always round position to the nearest integer. If it is 1, it will round it to the nearest 10th of a point. If it is -1 (or any negative value) it will not snap at all.

Parameters
snapthe number of decimal places to snap the node to the physics body

◆ setRestitution()

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

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

◆ setSensor()

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

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.

◆ setShared()

void cugl::physics2::Obstacle::setShared ( bool  shared)
inline

Sets whether this object is shared across multiple physics worlds.

If true, this object will keep additional information about it's state changes. In particular, we track any change to a physics object that is not a result of the physics simulation itself.

Parameters
sharedWhether this object is shared across multiple worlds.

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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

◆ 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).

Friends And Related Symbol 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

◆ _angFact

unsigned long cugl::physics2::Obstacle::_angFact
protected

Cache of factor to snap rotation of image to physics body

◆ _angSnap

int cugl::physics2::Obstacle::_angSnap
protected

Number of decimal places to snap rotation of image to physics body

◆ _body

b2Body* cugl::physics2::Obstacle::_body
protected

The physics body for Box2D.

◆ _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

◆ _posFact

unsigned long cugl::physics2::Obstacle::_posFact
protected

Cache of factor to snap position of image to physics body

◆ _posSnap

int cugl::physics2::Obstacle::_posSnap
protected

Number of decimal places to snap position of image to physics body

◆ _scene

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

The wireframe parent for debugging.

◆ _shared

bool cugl::physics2::Obstacle::_shared
protected

Whether or not this obstacle is shared across multiple obstacle worlds

◆ _tag

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

A tag for debugging purposes


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