Angel 3.2
A 2D Game Prototyping Engine
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
PhysicsActor Class Reference

An Actor that interacts with other Actors using our built-in physics system. More...

#include <PhysicsActor.h>

+ Inheritance diagram for PhysicsActor:

Public Types

enum  eShapeType { SHAPETYPE_BOX, SHAPETYPE_CIRCLE }
 

Public Member Functions

 PhysicsActor ()
 
virtual ~PhysicsActor ()
 
void SetDensity (float density)
 
void SetFriction (float friction)
 
void SetRestitution (float restitution)
 
void SetShapeType (eShapeType shapeType)
 
void SetIsSensor (bool isSensor)
 
void SetGroupIndex (int groupIndex)
 
void SetFixedRotation (bool fixedRotation)
 
virtual void InitPhysics ()
 
virtual void CustomInitPhysics ()
 
void ApplyForce (const Vector2 &force, const Vector2 &point)
 
void ApplyLocalForce (const Vector2 &force, const Vector2 &point)
 
void ApplyTorque (float torque)
 
void ApplyLinearImpulse (const Vector2 &impulse, const Vector2 &point)
 
void ApplyAngularImpulse (float impulse)
 
b2Body * GetBody ()
 
void ResetBody ()
 
void SetSize (float x, float y=-1.f)
 
void SetDrawSize (float x, float y=-1.f)
 
void SetPosition (float x, float y)
 
void SetPosition (const Vector2 &pos)
 
void SetRotation (float rotation)
 
void MoveTo (const Vector2 &newPosition, float duration, String onCompletionMessage="")
 
void RotateTo (float newRotation, float duration, String onCompletionMessage="")
 
void ChangeSizeTo (const Vector2 &newSize, float duration, String onCompletionMessage="")
 
void ChangeSizeTo (float newSize, float duration, String onCompletionMessage="")
 
virtual const String GetClassName () const
 
- Public Member Functions inherited from Actor
 Actor ()
 
virtual ~Actor ()
 
void SetSize (float x, float y=-1.f)
 
void SetSize (const Vector2 &newSize)
 
const Vector2GetSize () const
 
const BoundingBox GetBoundingBox () const
 
const Vector2GetPosition () const
 
const float GetRotation () const
 
void SetColor (float r, float g, float b, float a=1.0f)
 
void SetColor (const Color &color)
 
const ColorGetColor () const
 
void SetAlpha (float newAlpha)
 
const float GetAlpha () const
 
virtual void SetDrawShape (actorDrawShape drawShape)
 
const actorDrawShapeGetDrawShape () const
 
void UseDisplayList (int listIndex)
 
void MoveTo (const Vector2 &newPosition, float duration, bool smooth=false, String onCompletionMessage="")
 
void RotateTo (float newRotation, float duration, bool smooth=false, String onCompletionMessage="")
 
void ChangeColorTo (const Color &newColor, float duration, bool smooth=false, String onCompletionMessage="")
 
void ChangeSizeTo (const Vector2 &newSize, float duration, bool smooth=false, String onCompletionMessage="")
 
void ChangeSizeTo (float newSize, float duration, bool smooth=false, String onCompletionMessage="")
 
int GetSpriteTexture (int frame=0) const
 
bool SetSprite (const String &filename, int frame=0, GLint clampmode=GL_CLAMP, GLint filtermode=GL_LINEAR, bool optional=false)
 
void ClearSpriteInfo ()
 
void LoadSpriteFrames (const String &firstFilename, GLint clampmode=GL_CLAMP, GLint filtermode=GL_LINEAR)
 
void PlaySpriteAnimation (float delay, spriteAnimationType animType=SAT_Loop, int startFrame=-1, int endFrame=-1, const char *animName=NULL)
 
void SetSpriteFrame (int frame)
 
int GetSpriteFrame () const
 
const bool IsSpriteAnimPlaying () const
 
virtual void AnimCallback (String animName)
 
void SetUVs (const Vector2 &lowleft, const Vector2 &upright)
 
void GetUVs (Vector2 &lowleft, Vector2 &upright) const
 
const bool IsTagged (const String &tag)
 
void Tag (const String &newTag)
 
void Untag (const String &oldTag)
 
const StringSet & GetTags () const
 
const String & SetName (String newName)
 
const String & GetName () const
 
virtual void ReceiveMessage (Message *message)
 
void SetLayer (int layerIndex)
 
void SetLayer (const String &layerName)
 
virtual void Update (float dt)
 
virtual void Render ()
 
virtual void LevelUnloaded ()
 
ActorGetSelf ()
 
- Public Member Functions inherited from Renderable
 Renderable ()
 
virtual ~Renderable ()
 
void Destroy ()
 
bool IsDestroyed ()
 
int GetLayer ()
 

Protected Member Functions

virtual void InitShape (b2Shape *)
 

Protected Attributes

b2Body * _physBody
 
float _density
 
float _friction
 
float _restitution
 
eShapeType _shapeType
 
bool _isSensor
 
int _groupIndex
 
bool _fixedRotation
 
- Protected Attributes inherited from Actor
Vector2 _size
 
Vector2 _position
 
Color _color
 
float _rotation
 
float _UV [8]
 
actorDrawShape _drawShape
 
int _spriteCurrentFrame
 
int _spriteNumFrames
 
float _spriteFrameDelay
 
float _spriteCurrentFrameDelay
 
int _spriteTextureReferences [MAX_SPRITE_FRAMES]
 
spriteAnimationType _spriteAnimType
 
int _spriteAnimStartFrame
 
int _spriteAnimEndFrame
 
int _spriteAnimDirection
 
int _displayListIndex
 
StringSet _tags
 
String _name
 
String _currentAnimName
 
- Protected Attributes inherited from Renderable
bool _deleteMe
 
int _layer
 

Friends

class World
 

Additional Inherited Members

- Static Public Member Functions inherited from Actor
static Actor *const GetNamed (const String &nameLookup)
 
static ActorCreate (const String &archetype)
 
static void SetScriptCreatedActor (Actor *a)
 
- Static Protected Attributes inherited from Actor
static const float _squareVertices []
 
static float _circleVertices []
 
static float _circleTextureCoords []
 
static std::map< String, Actor * > _nameList
 
static Actor_scriptCreatedActor = NULL
 

Detailed Description

Angel incorporates Box2D (http://www.box2d.org) to handle our physics simulation. This Actor is a loose wrapping around Box2D so that you don't need to worry about the underlying library if you're just doing simple physics.

Definition at line 44 of file PhysicsActor.h.

Member Enumeration Documentation

The two physics shapes we currently support. If you want anything else, you're going to be delving into the Box2D library itself.

Definition at line 67 of file PhysicsActor.h.

Constructor & Destructor Documentation

PhysicsActor::PhysicsActor ( void  )

Sets up a new PhysicsActor with some reasonable default physical properties. Box-shaped; Density = 1.0; Friction = 0.3; Restitution = 0.0.

Note that the PhysicsActor won't actually start doing anything until you call PhysicsActor::InitPhysics().

Definition at line 43 of file PhysicsActor.cpp.

PhysicsActor::~PhysicsActor ( )
virtual

The destructor removes the PhysicsActor from the internal simulation. It's very important to call the destructor when your remove a PhysicsActor from your World, because otherwise the physics engine will think it's still around and you'll get some strange results.

Definition at line 55 of file PhysicsActor.cpp.

Member Function Documentation

void PhysicsActor::SetDensity ( float  density)

Set the density to be used by the physics simulation. Box2D assumes that the units are kg/m^2. If you set density to 0.0, the PhysicsActor is static and will never move in the world. (Other objects will still collide with it.)

Note that after you call PhysicsActor::InitPhysics, the density is locked and this function will do nothing but spew a warning.

Parameters
densityDesired density

Definition at line 64 of file PhysicsActor.cpp.

void PhysicsActor::SetFriction ( float  friction)

Set the friction to be used by the physics simulation. 0.0 indicates no friction, while 1.0 indicates very high friction.

Note that after you call PhysicsActor::InitPhysics, the friction is locked and this function will do nothing but spew a warning.

Parameters
densityDesired friction

Definition at line 72 of file PhysicsActor.cpp.

void PhysicsActor::SetRestitution ( float  restitution)

Set the restitution (bounciness) to be used by the physics simulation. 0.0 indicates no bounce at all, while 1.0 is a superball.

Note that after you call PhysicsActor::InitPhysics, the restitution is locked and this function will do nothing but spew a warning.

Parameters
densityDesired restitution

Definition at line 80 of file PhysicsActor.cpp.

void PhysicsActor::SetShapeType ( eShapeType  shapeType)

Set the shape you want to use for this object.

Note that after you call PhysicsActor::InitPhysics, the restitution is locked and this function will do nothing but spew a warning.

Parameters
shapeTypeEither SHAPETYPE_BOX or SHAPETYPE_CIRCLE.

Definition at line 88 of file PhysicsActor.cpp.

void PhysicsActor::SetIsSensor ( bool  isSensor)

If you set an object up as a sensor, it will cause collision messages to be sent, but not actually physically react to the collision. Useful for detecting when objects have entered certain regions.

Note that after you call PhysicsActor::InitPhysics, the restitution is locked and this function will do nothing but spew a warning.

Parameters
isSensorTrue if the object should be a sensor, false if not. PhysicsActors are assumed to not be sensors unless otherwise specified.

Definition at line 96 of file PhysicsActor.cpp.

void PhysicsActor::SetGroupIndex ( int  groupIndex)

Set a group index if you want to have objects only collide with certain other groups of objects. Positive numbers mean that objects always collide, negative numbers mean they never will.

See the Box2D documentation for more information on collision groups

Note that after you call PhysicsActor::InitPhysics, the group index is locked and this function will do nothing but spew a warning.

Parameters
groupIndexThe new group index for this PhysicsActor

Definition at line 104 of file PhysicsActor.cpp.

void PhysicsActor::SetFixedRotation ( bool  fixedRotation)

If true, this PhysicsActor will not rotate (useful for characters).

Note that after you call PhysicsActor::InitPhysics, the this value is locked and the function will do nothing but spew a warning.

Parameters
fixedRotation

Definition at line 112 of file PhysicsActor.cpp.

void PhysicsActor::InitPhysics ( )
virtual

Start simulating this PhysicsActor in the world.

Definition at line 121 of file PhysicsActor.cpp.

virtual void PhysicsActor::CustomInitPhysics ( )
inlinevirtual

If you want to have your own setup in a derived class, you can implement this function there. It's called at the end of the base class's PhysicsActor::InitPhysics.

Definition at line 166 of file PhysicsActor.h.

void PhysicsActor::ApplyForce ( const Vector2 force,
const Vector2 point 
)

Apply a force to an object. The standard units are Newtons (kg * m/s^2). The point parameter is a location in actor space; if you want world space, you can either convert it yourself or use GetBody() to manipulate the underlying Box2D API.

ApplyForce should be called every timestep if you want the force to be continuously applied.

Parameters
forceThe force to apply (direction and magnitude are significant) in Newtons
pointThe point in actor space to which the force should be applied

Definition at line 181 of file PhysicsActor.cpp.

void PhysicsActor::ApplyLocalForce ( const Vector2 force,
const Vector2 point 
)

Applies a force relative to the Actor. The standard units are Newtons (kg * m/s^2). The point parameter is a location in actor space; if you want world space, you can either convert it yourself or use GetBody() to manipulate the underlying Box2D API.

ApplyLocalForce should be called every timestep if you want the force to be continuously applied.

Parameters
forceThe force to apply (direction and magnitude are significant) in Newtons
pointThe point in actor space to which the force should be applied

Definition at line 189 of file PhysicsActor.cpp.

void PhysicsActor::ApplyTorque ( float  torque)

Apply torque to affect angular velocity without affecting linear velocity. The standard units are Newton-meters.

Parameters
torqueAmount of torque to apply around the Z-axis.

Definition at line 197 of file PhysicsActor.cpp.

void PhysicsActor::ApplyLinearImpulse ( const Vector2 impulse,
const Vector2 point 
)

Similar to ApplyForce, but will immediately affect a PhysicsActor's velocity, as opposed to forces which need to be applied every frame. The result is that ApplyImpulse will appear to have roughly 30x the affect of ApplyForce.

For more information: http://www.box2d.org/forum/viewtopic.php?f=3&t=260

The point parameter is a location in actor space; if you want world space, you can either convert it yourself or use GetBody() to manipulate the underlying Box2D API.

Parameters
impulseThe strength of the impulse to apply.
point

Definition at line 205 of file PhysicsActor.cpp.

void PhysicsActor::ApplyAngularImpulse ( float  impulse)

Similar to ApplyLinearImpulse, but affects the angular rather than linear velocity.

Parameters
impulseThe strength of the impulse to apply.

Definition at line 213 of file PhysicsActor.cpp.

b2Body* PhysicsActor::GetBody ( )
inline

Get the Box2D representation of this PhysicsActor. If you're going to be directly manipulating the Box2D API, this will be useful to you.

Returns
The Box2D data about the actor

Definition at line 240 of file PhysicsActor.h.

void PhysicsActor::ResetBody ( )
inline

Resets the internal pointer to the Box2D physics body to NULL. Call this if you're manually destroying the body for some reason and need to make sure the PhysicsActor doesn't keep trying to track it.

Definition at line 248 of file PhysicsActor.h.

void PhysicsActor::SetSize ( float  x,
float  y = -1.f 
)

An override of the Actor::SetSize function that disables itself after InitPhysics has been called.

Definition at line 221 of file PhysicsActor.cpp.

void PhysicsActor::SetDrawSize ( float  x,
float  y = -1.f 
)

Sometimes you want the visible size of an Actor to be larger or smaller than its collision geometry in the world. This function lets you alter the former without affecting the latter.

Parameters
xHorizontal draw size in OpenGL units – negative numbers treated as zero
yVertical draw size in OpenGL units – if less than or equal to zero, assumed to be equal to x

Definition at line 229 of file PhysicsActor.cpp.

void PhysicsActor::SetPosition ( float  x,
float  y 
)
virtual

An override of the Actor::SetPosition function that disables itself after InitPhysics has been called.

Reimplemented from Actor.

Definition at line 234 of file PhysicsActor.cpp.

void PhysicsActor::SetPosition ( const Vector2 pos)
virtual

An override of the Actor::SetPosition function that disables itself after InitPhysics has been called.

Reimplemented from Actor.

Definition at line 242 of file PhysicsActor.cpp.

void PhysicsActor::SetRotation ( float  rotation)
virtual

An override of the Actor::SetRotation function that disables itself after InitPhysics has been called.

Reimplemented from Actor.

Definition at line 250 of file PhysicsActor.cpp.

void PhysicsActor::MoveTo ( const Vector2 newPosition,
float  duration,
String  onCompletionMessage = "" 
)
inline

An override of the Actor::MoveTo function that doesn't allow the interval to be applied to PhysicsActors.

Definition at line 290 of file PhysicsActor.h.

void PhysicsActor::RotateTo ( float  newRotation,
float  duration,
String  onCompletionMessage = "" 
)
inline

An override of the Actor::RotateTo function that doesn't allow the interval to be applied to PhysicsActors.

Definition at line 296 of file PhysicsActor.h.

void PhysicsActor::ChangeSizeTo ( const Vector2 newSize,
float  duration,
String  onCompletionMessage = "" 
)
inline

An override of the Actor::ChangeSizeTo function that doesn't allow the interval to be applied to PhysicsActors.

Definition at line 302 of file PhysicsActor.h.

void PhysicsActor::ChangeSizeTo ( float  newSize,
float  duration,
String  onCompletionMessage = "" 
)
inline

An override of the Actor::ChangeSizeTo function that doesn't allow the interval to be applied to PhysicsActors.

Definition at line 308 of file PhysicsActor.h.

virtual const String PhysicsActor::GetClassName ( ) const
inlinevirtual

Used by the SetName function to create a basename for this class. Overridden from Actor::GetClassName.

Returns
The string "PhysicsActor"

Reimplemented from Actor.

Definition at line 316 of file PhysicsActor.h.


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