The Pinball Game
Ian Parberry's "Introduction to Game Physics"
Public Member Functions | Private Attributes | List of all members
CGate Class Reference

A gate. More...

#include <Parts.h>

Inheritance diagram for CGate:
CCommon

Public Member Functions

 CGate (CLineSeg *p)
 Constructor. More...
 
 ~CGate ()
 Destructor.
 
void CloseGate ()
 Check latch to see if gate should be closed. More...
 
bool NarrowPhase (CDynamicCircle *)
 Narrow phase collision detection and response. More...
 

Private Attributes

CLineSeg * m_pLineSeg = nullptr
 Pointer to line segment representing gate.
 
bool m_bOpen = false
 true if gate is open.
 
bool m_bOccupied = false
 true if ball is holding gate open.
 

Additional Inherited Members

- Static Protected Attributes inherited from CCommon
static CRendererm_pRenderer = nullptr
 Pointer to the renderer.
 
static CObjectManagerm_pObjectManager = nullptr
 Pointer to the object manager.
 
static UINT m_nMIterations = 4
 Number of motion iterations.
 
static UINT m_nCIterations = 1
 Number of collision iterations.
 
static float m_fFrequency = 60.0f*m_nMIterations
 Frequency, number of physics iterations per second.
 
static eDrawMode m_eDrawMode = eDrawMode::Background
 Draw mode.
 
static bool m_bBallInPlay = false
 Is there a ball currently in play?
 
static UINT m_nScore = 0
 Current score.
 

Detailed Description

A gate allows balls to cross in one direction only. It's essentially a line segment with a couple of Boolean variables and some code that handles the crossing-in-one-direction requirement. Care must be taken to do the right thing when a ball gets nudged back while in the process of crossing.

A gate can be open or closed, latched or unlatched. When it is open, there can be no collisions. When it is closed, balls collide with it if they are travelling from one side but not the other. More specifically, if the vector from the ball's center to the POI is in the opposite direction to the line segment's normal vector CLineSeg::m_vNormal then they can cross. Recall that the direction of CLineSeg::m_vNormal depends on the order in which CLineSeg's end points were specified, so make sure you get it right.

Constructor & Destructor Documentation

◆ CGate()

CGate::CGate ( CLineSeg *  p)

Construct a closed and unlatched gate from a line segment.

Parameters
pPointer to a line segment.

Member Function Documentation

◆ CloseGate()

void CGate::CloseGate ( )

Close gate if open and there is no ball currently holding it open. Unset the occupied flag ready for use the the next frame. We assume that this is called at the end of the frame.

◆ NarrowPhase()

bool CGate::NarrowPhase ( CDynamicCircle *  p)

If a dynamic circle collides with a gate and it is moving in the correct direction, then the gate opens and the dynamic circle is allowed through. Otherwise the dynamic circle bounces off the gate as usual.

Parameters
pPointer to a dynamic circle.
Returns
true if the dynamic circle bounces off the gate.