The Cannon Lullaby Toy
Game Physics with a 2D Physics Engine
Loading...
Searching...
No Matches
CCannon Class Reference

The cannon. More...

#include <Cannon.h>

Inheritance diagram for CCannon:
Collaboration diagram for CCannon:

Public Member Functions

void Initialize ()
 Set up a cannon.
 
void Explode ()
 Make cannon explode.
 
void Fire ()
 Fire the cannon.
 
void SetSpeed (float)
 Set the motor speed in the wheel joints.
 
const bool IsDead () const
 Whether cannon has exploded.
 
const Vector2 GetPos () const
 Get cannon position.
 

Private Member Functions

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

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.
 
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 CBox2DRenderer * m_pRenderer = nullptr
 Pointer to Render World.
 
static CObjectManagerm_pObjectManager = nullptr
 Pointer to the Object Manager.
 
static Sage::CParticleEngine2D * m_pFrontParticleEngine = nullptr
 Pointer to front particle engine.

 
static Sage::CParticleEngine2D * m_pBackParticleEngine = nullptr
 Pointer to front particle engine.
 
static CCannonm_pCannon = nullptr
 Pointer to a cannon.
 
static Vector2 m_vBellPos [NUMBELLS] = {Vector2(0.0f)}
 Positions of bells.
 
static float m_fGravity = 20.0f
 Magnitude of gravity in Render World units.
 
static eDrawMode m_eDrawMode = eDrawMode::Sprites
 Draw mode.
 

Detailed Description

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

Member Function Documentation

◆ CreateBarrel()

b2Body * CCannon::CreateBarrel ( float x,
float y,
int nIndex )
private
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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreateBase()

b2Body * CCannon::CreateBase ( float x,
float y,
int nIndex )
private
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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreateWheel()

b2Body * CCannon::CreateWheel ( float x,
float y,
int nIndex )
private
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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DeliverImpulse()

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

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.
Here is the caller graph for this function:

◆ Explode()

void CCannon::Explode ( )

Make the cannon explode by destroying the joints, resetting their collision group indices so that they can collide.

smoke

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Fire()

void CCannon::Fire ( )

Create cannonballs in both Render World and Physics World and send them on their way. The tune, the delay to the first note, and the time between notes are hard-coded. Apply a recoil impulse to the cannon in Physics World, stop the wheel motors, and emit a puff of smoke..

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetPos()

const Vector2 CCannon::GetPos ( ) const

Reader function for the cannon's position.

Returns
Cannon position in Render World.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Initialize()

void CCannon::Initialize ( )

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsDead()

const bool CCannon::IsDead ( ) const

Determine whether the cannon has exploded already.

Returns
true If it's dead, Dave.
Here is the caller graph for this function:

◆ MakeCollide()

void CCannon::MakeCollide ( b2Body * b)
private

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.
Here is the caller graph for this function:

◆ SetSpeed()

void CCannon::SetSpeed ( float s)

Start the cannon moving using motors in the wheels.

Parameters
sSpeed of cannon. Negative means left, positive means right.
Here is the caller graph for this function: