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

The cannon. More...

#include <Cannon.h>

Inheritance diagram for CCannon:
CCommon

Public Member Functions

 CCannon ()
 Constructor. More...
 
void Initialize ()
 Set up a cannon. More...
 
void Explode ()
 Make cannon explode. More...
 
bool Fire ()
 Fire the cannon. More...
 
void BarrelUp (float a)
 Rotate the cannon barrel. More...
 
void SetSpeed (float s)
 Set the motor speed in the wheel joints. More...
 
void CoolDown ()
 Cannon temperature drops over time. More...
 
void Reset ()
 Reset cannon to initial conditions. More...
 
const UINT GetNumFired () const
 Get number of cannonballs fired in this level. More...
 
const bool IsDead () const
 Whether cannon has exploded. More...
 
const Vector2 GetPos () const
 Get cannon position. More...
 
void GetTemps (float &, float &) const
 Get current and max temperatures. More...
 

Private Member Functions

b2Body * CreateBase (float, float, int)
 Create a cannon base in Physics World. More...
 
b2Body * CreateBarrel (float, float, int)
 Create a cannon barrel in Physics World. More...
 
b2Body * CreateWheel (float, float, int)
 Create a cannon wheel in Physics World. More...
 
void DeliverImpulse (b2Body *, const b2Vec2 &, const b2Vec2 &=b2Vec2(0, 0))
 Apply an impulse in Physics World. More...
 
void MakeCollide (b2Body *b)
 Make cannon parts collide-able in Physics World. More...
 

Private Attributes

b2Body * m_pBarrel = nullptr
 Pointer to cannon barrel in Physics World.
 
b2Body * m_pBase = nullptr
 Pointer to cannon base in Physics World.
 
b2Body * m_pWheel1 = nullptr
 Pointer to cannon wheel in Physics World.
 
b2Body * m_pWheel2 = nullptr
 Pointer to cannon wheel in Physics World.
 
b2WheelJoint * m_pWheelJoint1 = nullptr
 Pointer to cannon wheel joint in Physics World.
 
b2WheelJoint * m_pWheelJoint2 = nullptr
 Pointer to cannon wheel joint in Physics World.
 
b2RevoluteJoint * m_pBarrelJoint = nullptr
 Pointer to cannon barrel body in Physics World.
 
float m_fTemp = 0.0f
 Cannon temperature.
 
float m_fMaxTemp = 0.0f
 Cannon maximum allowable temperature.
 
UINT m_nBallsFired = 0
 Number of cannonballs fired in current level.
 
bool m_bExploded = false
 Whether cannon has exploded.
 

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

The cannon is a composite made up of several sprites in Render World and several bodies in Physics World. It represents the player.

Constructor & Destructor Documentation

◆ CCannon()

CCannon::CCannon ( )

Constructor.

The constructor uses Reset() to construct the cannon.

Member Function Documentation

◆ BarrelUp()

void CCannon::BarrelUp ( float  a)

Rotate the cannon barrel.

Rotate the cannon barrel up or down by a small increment.

Parameters
aAngle increment in radians. Positive means up, negative means down.

◆ CoolDown()

void CCannon::CoolDown ( )

Cannon temperature drops over time.

Cool the cannon's temperature, unless it is too high already, in which case mark it for exploding on the next fire.

◆ CreateBarrel()

b2Body * CCannon::CreateBarrel ( float  x,
float  y,
int  nIndex 
)
private

Create a cannon barrel in Physics World.

Parameters
xCannon barrel's x coordinate in Render World.
yCannon barrel's y coordinate in Render World.
nIndexCannon barrel's collision group index.
Returns
Pointer to cannon barrel body in Physics World.

◆ CreateBase()

b2Body * CCannon::CreateBase ( float  x,
float  y,
int  nIndex 
)
private

Create a cannon base in Physics World.

Parameters
xCannon base's x coordinate in Render World.
yCannon base's y coordinate in Render World.
nIndexCannon base's collision group index.
Returns
Pointer to cannon base body in Physics World.

◆ CreateWheel()

b2Body * CCannon::CreateWheel ( float  x,
float  y,
int  nIndex 
)
private

Create a cannon wheel in Physics World.

Parameters
xCannon wheel's x coordinate in Render World.
yCannon wheel's y coordinate in Render World.
nIndexCannon wheel's collision group index.
Returns
Pointer to cannon wheel body in Physics World.

◆ DeliverImpulse()

void CCannon::DeliverImpulse ( b2Body *  b,
const b2Vec2 &  v,
const b2Vec2 &  ds = b2Vec2(0, 0) 
)
private

Apply an impulse in Physics World.

Deliver an impulse to part of the cannon, which is made up of many bodies in Physics World.

Parameters
bBody to apply impulse to.
vVector direction and magnitude of impulse.
dsVector dispolacement to point at which impulse is applied.

◆ Explode()

void CCannon::Explode ( )

Make cannon explode.

Make the cannon explode by destroying the joints, applying impulses to cannon parts so that they fly apart, and resetting their collision group indices so that they can collide.

◆ Fire()

bool CCannon::Fire ( )

Fire the cannon.

Create a cannonball in both Render World and Physics World and send it on its way. Apply a recoil impulse to the cannon in Physics World. Increment the cannon's temperature. All this is assuming that the cannon isn't nose down and hasn't already exploded.

◆ GetNumFired()

const UINT CCannon::GetNumFired ( ) const

Get number of cannonballs fired in this level.

Return the number of cannonballs fired in this level of the game.

Returns
Number of cannonballs fired.

◆ GetPos()

const Vector2 CCannon::GetPos ( ) const

Get cannon position.

Reader function for the cannon's position.

Returns
Cannon position in Render World.

◆ GetTemps()

void CCannon::GetTemps ( float &  tcur,
float &  tmax 
) const

Get current and max temperatures.

Reader function for the current and max temperatures.

Parameters
tcur[OUT] Current temperature.
tmax[OUT] Maximum temperature experienced.

◆ Initialize()

void CCannon::Initialize ( )

Set up a cannon.

Create a cannon in Physics World and Object World, linking the two representations together. All positions are hard-coded.

◆ IsDead()

const bool CCannon::IsDead ( ) const

Whether cannon has exploded.

Determine whether the cannon has exploded already.

Returns
true If it's dead, Dave.

◆ MakeCollide()

void CCannon::MakeCollide ( b2Body *  b)
private

Make cannon parts collide-able in Physics World.

Reset the collision group index for part of cannon, which is made up of many bodies in Physics World.

Parameters
bBody to reset collision group index of.

◆ Reset()

void CCannon::Reset ( )

Reset cannon to initial conditions.

Reset the cannon to its initial conditions ready for a new game to begin.

◆ SetSpeed()

void CCannon::SetSpeed ( float  s)

Set the motor speed in the wheel joints.

Start the cannon moving using motors in the wheels.

Parameters
sSpeed of cannon. Negative means left, positive means right.