Box2D Joint Toy
Game Physics with a 2D Physics Engine
Loading...
Searching...
No Matches
CNewtonsCradle Class Reference

Newton's cradle. More...

#include <NewtonsCradle.h>

Inheritance diagram for CNewtonsCradle:
Collaboration diagram for CNewtonsCradle:

Public Member Functions

 CNewtonsCradle ()
 Constructor.
 
 ~CNewtonsCradle ()
 Destructor.
 
void Move ()
 Swivel Isaac Newton's eyeballs.
 
void Draw ()
 Draw level.
 
void Action ()
 Reverse motor.
 
- Public Member Functions inherited from CLevel
 CLevel (eSprite)
 
virtual ~CLevel ()
 Default constructor.
 

Private Member Functions

void CreateBall (float, float)
 Create a ball.
 
void Create ()
 Create all balls.
 
void ResetBalls ()
 Reset all balls to initial conditions.
 
b2Body * CreateCradle (float, float)
 Create the cradle.
 

Private Attributes

CSirIsaacNewtonm_pIsaac = nullptr
 Isaac Newton caricature.
 
const UINT m_nNumBalls = 6LLU
 Number of balls.
 
UINT m_nBallsToMove = 1LLU
 Number of balls in to move at a time.
 
bool m_bBallsStillMoving = false
 Flag for moving balls.
 

Additional Inherited Members

- Protected Member Functions inherited from CLevel
CLineObjectCreateLine (b2Body *, const b2Vec2 &, bool, b2Body *, const b2Vec2 &, bool)
 Create new line object.
 
- Protected Attributes inherited from CLevel
std::vector< CObject * > m_stdObject
 Object pointers.
 
std::vector< CLineObject * > m_stdLine
 Line list.
 
eSprite m_eBgSprite
 Type of background sprite.
 
- Static Protected Attributes inherited from CCommon
static b2World * m_pPhysicsWorld = nullptr
 Pointer to Box2D Physics World.
 
static CBox2DRenderer * m_pRenderer = nullptr
 Pointer to the renderer.
 
static CLevelm_pLevel = nullptr
 Pointer to current level.
 
static eLevel m_eCurLevel = eLevel::Windmill
 Current level.
 
static eDrawMode m_eDrawMode = eDrawMode::Sprites
 Draw mode.
 

Detailed Description

Newton's Cradle has a bunch of balls on strings hanging from a cradle. It's a common executive toy. For those in the know, it demonstrates the conservation of momentum and energy in collisions.

Constructor & Destructor Documentation

◆ CNewtonsCradle()

CNewtonsCradle::CNewtonsCradle ( )

Create the cradle, the balls, the distance joints connecting the former to the latter, and the line objects that represent them. The balls are created in left-to-right order.

Here is the call graph for this function:

Member Function Documentation

◆ Action()

void CNewtonsCradle::Action ( )
virtual

If there are balls still moving, then reset them to the initial conditions. Otherwise, deliver an impulse to the leftmost balls, incrementing the number affected each time this function is called when the balls are not moving. The balls are considered moving if and impulse was applied the last time this function is called. This function assumes that the balls were created in left-to-right order, that is, they appear in the object list from left to right, with m_stdObject[0] being the leftmost ball.

Reimplemented from CLevel.

Here is the call graph for this function:

◆ Create()

void CNewtonsCradle::Create ( )
private

Create the cradle, the balls, their joints, and their lines.

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

◆ CreateBall()

void CNewtonsCradle::CreateBall ( float x,
float y )
private

Create a ball bearing object and Physics World body.

Parameters
xX coordinate in Physics World units.
yY coordinate in Physics World units.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreateCradle()

b2Body * CNewtonsCradle::CreateCradle ( float x,
float y )
private

The cradle except for the bar at the top of the cradle.

Parameters
xX coordinate in Physics World units.
yY coordinate in Physics World units.
Returns
Pointer to the Physics World body.
Here is the caller graph for this function:

◆ Draw()

void CNewtonsCradle::Draw ( )
virtual

Draw the background, the objects in this level, and the Isaac Newton head.

Reimplemented from CLevel.

Here is the call graph for this function:

◆ Move()

void CNewtonsCradle::Move ( )
virtual

Move Isaac Newton's eyeballs swivel in the direction of the last ball in the ball list, which is hopefully the rightmost one.

Reimplemented from CLevel.

Here is the call graph for this function:

◆ ResetBalls()

void CNewtonsCradle::ResetBalls ( )
private

Reset the balls, their lines, and their joints to the initial conditions.

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