![]() |
Box2D Joint Toy
Ian Parberry's "Introduction to Game Physics"
|
A system of 5 interlocking nautilus gears. More...
#include <NautilusGear.h>
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 b2World * | m_pPhysicsWorld = nullptr |
Pointer to Box2D Physics World. | |
static CRenderer * | m_pRenderer = nullptr |
Pointer to the renderer. | |
static CObjectManager * | m_pObjectManager = nullptr |
Pointer to the object manager. | |
static CWindmill * | m_pWindmill = nullptr |
Pointer to windmill. | |
static CGear * | m_pGear = nullptr |
Pointer to gear system. | |
static CNautilusGear * | m_pNautilusGear = nullptr |
Pointer to Nautilus gear system. | |
static CRackAndPinion * | m_pRackAndPinion = nullptr |
Pointer to rack-and-pinion. | |
static CCar * | m_pCar = nullptr |
Pointer to car. | |
static CRamp * | m_pRamp = nullptr |
Pointer to ramp. | |
static CNewtonsCradle * | m_pNewtonsCradle = nullptr |
Pointer to Newton's Cradle. | |
static CPulley * | m_pPulley = nullptr |
Pointer to pulley system. | |
static eLevel | m_eCurLevel = eLevel::Windmill |
Current level. | |
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.
CNautilusGear::CNautilusGear | ( | float | x, |
float | y | ||
) |
Constructor.
x | X coordinate in Physics World units. |
y | Y coordinate in Physics World units. |
|
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.
x | X coordinate in Physics World units. |
y | Y coordinate in Physics World units. |
|
private |
Create gear wheel in Physics World.
Create a gear wheel at a given position in Physics World.
x | X coordinate in Physics World units. |
y | Y coordinate in Physics World units. |
t | Sprite type of the gear wheel. |
dynamic | true if this is to be rotated by Box2D, false if we're going to take care of it ourselves. |
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\).
void CNautilusGear::reverse | ( | ) |
Reverse gears.
Reverse the gears by setting the motor speed on the revolute joint for the first gear.