Box2D Joint Toy
Ian Parberry's "Introduction to Game Physics"
Public Member Functions | Private Member Functions | Private Attributes | List of all members
CNautilusGear Class Reference

A system of 5 interlocking nautilus gears. More...

#include <NautilusGear.h>

Inheritance diagram for CNautilusGear:
CCommon

Public Member Functions

 CNautilusGear (float, float)
 Constructor. More...
 
void move ()
 Move gears in synchrony. More...
 
void reverse ()
 Reverse gears. More...
 

Private Member Functions

b2Body * CreateBase (float, float)
 Create invisible base in Physics World. More...
 
b2Body * CreateGear (float, float, eSprite, bool)
 Create gear wheel in Physics World. More...
 

Private Attributes

std::vector< b2Body * > m_stdGear
 Gear pointers.
 
b2RevoluteJoint * m_pJoint = nullptr
 Pointer to revolute joint for center gear.
 

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 the renderer.
 
static CObjectManagerm_pObjectManager = nullptr
 Pointer to the object manager.
 
static CWindmillm_pWindmill = nullptr
 Pointer to windmill.
 
static CGearm_pGear = nullptr
 Pointer to gear system.
 
static CNautilusGearm_pNautilusGear = nullptr
 Pointer to Nautilus gear system.
 
static CRackAndPinionm_pRackAndPinion = nullptr
 Pointer to rack-and-pinion.
 
static CCarm_pCar = nullptr
 Pointer to car.
 
static CRampm_pRamp = nullptr
 Pointer to ramp.
 
static CNewtonsCradlem_pNewtonsCradle = nullptr
 Pointer to Newton's Cradle.
 
static CPulleym_pPulley = nullptr
 Pointer to pulley system.
 
static eLevel m_eCurLevel = eLevel::Windmill
 Current level.
 

Detailed Description

A system of 5 interlocking nautilus gears.

A nautilus gear is a gear in the shape of a nautilus shell. The center gear has a revolute joint with a motor. The other gears have variable speed, so they cannot be animated by Box2D. We have to animate them ourselves which means doing a little math.
The number and positions of the gears are hard-coded but there is enough storage for any number of them.

Constructor & Destructor Documentation

◆ CNautilusGear()

CNautilusGear::CNautilusGear ( float  x,
float  y 
)

Constructor.

Parameters
xX coordinate in Physics World units.
yY coordinate in Physics World units.

Member Function Documentation

◆ CreateBase()

b2Body * CNautilusGear::CreateBase ( float  x,
float  y 
)
private

Create invisible base in Physics World.

Create an invisible base to attach the largest gear wheel to. The base has no sprite and no shape in Physics World. The latter means it doesn't collide with anything.

Parameters
xX coordinate in Physics World units.
yY coordinate in Physics World units.
Returns
Pointer to the cog wheel body in Physics World.

◆ CreateGear()

b2Body * CNautilusGear::CreateGear ( float  x,
float  y,
eSprite  t,
bool  dynamic 
)
private

Create gear wheel in Physics World.

Create a gear wheel at a given position in Physics World.

Parameters
xX coordinate in Physics World units.
yY coordinate in Physics World units.
tSprite type of the gear wheel.
dynamictrue if this is to be rotated by Box2D, false if we're going to take care of it ourselves.
Returns
Pointer to the cog wheel body in Physics World.

◆ move()

void CNautilusGear::move ( )

Move gears in synchrony.

Make the four gears surrounding the center gear rotate in synchrony with the center gear. We use the fact that the radius of a nautilus gear at angle \(\theta\) is \(ae^{b\theta}\), where \(a\) and \(b\) are the maximum and minimum radius of the gear, respectively, and \(d = \ln(b/a)/2\pi\).

◆ reverse()

void CNautilusGear::reverse ( )

Reverse gears.

Reverse the gears by setting the motor speed on the revolute joint for the first gear.