Box2D Cannon Game
Ian Parberry's "Introduction to Game Physics with Box2D"
Public Member Functions | Private Attributes | List of all members
CObject Class Reference

The game object. More...

#include <Object.h>

Inheritance diagram for CObject:
CCommon

Public Member Functions

 CObject (eSprite, b2Body *)
 Constructor. More...
 
 ~CObject ()
 Destructor. More...
 
void draw ()
 Draw object in Render World. More...
 
void MakeCollisionSound ()
 Make collision sound, if appropriate. More...
 
void Update ()
 Update from Physics Engine. More...
 
const bool CrateBelow (float) const
 Crate is below a given height. More...
 
const eSprite GetSpriteType () const
 Get sprite type. More...
 

Private Attributes

b2Body * m_pBody = nullptr
 Physics World body.
 
b2Vec2 m_b2vOldV
 Old velocity vector, needed to infer collisions.
 

Additional Inherited Members

- Static Protected Attributes inherited from CCommon
static b2World * m_pPhysicsWorld = nullptr
 Pointer to Box2D Physics World.
 
static CRendererm_pRenderer = nullptr
 Pointer to Render World.
 
static CObjectManagerm_pObjectManager = nullptr
 Pointer to object manager.

 
static LParticleEngine2D * m_pParticleEngine = nullptr
 Pointer to particle engine.
 
static bool m_bEasterEgg = false
 Easter egg!
 

Detailed Description

The game object.

Game objects are responsible for remembering information about themselves, in particular, their representations in Render World and Physics World.

Constructor & Destructor Documentation

◆ CObject()

CObject::CObject ( eSprite  t,
b2Body *  b 
)

Constructor.

This constructor assumes that a Physics World body has already been created for this object. It then has responsibility for deleting it in its destructor.

Parameters
tSprite type.
bPointer to Physics World body.

◆ ~CObject()

CObject::~CObject ( )

Destructor.

This destructor assumes that Box2D hasn't been shut down yet.

Member Function Documentation

◆ CrateBelow()

const bool CObject::CrateBelow ( float  h) const

Crate is below a given height.

If this is a crate, is it below a given height? We only care about crates, so we return true for everything else.

Parameters
hHeight in Render World units.
Returns
true if this is not a crate, or has no body, or is below height h.

◆ draw()

void CObject::draw ( )

Draw object in Render World.

Draw in Render World. Position and orientation must be gotten from Physics World.

◆ GetSpriteType()

const eSprite CObject::GetSpriteType ( ) const

Get sprite type.

Reader function for sprite type.

Returns
Sprite type.

◆ MakeCollisionSound()

void CObject::MakeCollisionSound ( )

Make collision sound, if appropriate.

Kluge collision sounds by querying Physics World to see whether the object's velocity has changed. If the length squared of the change in velocity vector is larger than some arbitrarily chosen value, then conclude that a collision must have occurred, so make a sound.

◆ Update()

void CObject::Update ( )

Update from Physics Engine.

Update roll and position member variables from Physics World.