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

A vector field. More...

#include <VectorField.h>

Inheritance diagram for CVectorField:
CCommon

Public Member Functions

 CVectorField (UINT)
 Constructor. More...
 
 ~CVectorField ()
 Destructor.
 
void Clear ()
 Clear. More...
 
void Draw ()
 Draw. More...
 
void Update (const Vector2 &, const Vector2 &)
 Update pressures around moving object. More...
 
void Detonate (const Vector2 &)
 Detonation. More...
 
void Step (const UINT n)
 Perform an animation step. More...
 
const Vector2 GetClosestVector (const Vector2 &) const
 Get vector closest to a point. More...
 

Private Member Functions

const bool GetClosestVertex (const Vector2 &, UINT &, UINT &) const
 Closest vertex. More...
 
const bool isInternalVertex (UINT, UINT) const
 Check that indices are for a non-edge vertex. More...
 
const float GetAveragePressure (UINT, UINT) const
 Average pressure around a vertex. More...
 
const Vector2 GetVectorFromPressure (UINT, UINT) const
 Average vectors around a vertex. More...
 
void Update (UINT, UINT, UINT, float)
 Update pressures around moving object. More...
 
void RelaxPressure (const UINT)
 Relax pressure at the vertices. More...
 
void DrawVectors ()
 Draw vectors. More...
 
void DrawPoints ()
 Draw points. More...
 
void DrawPressures ()
 Draw pressures. More...
 

Private Attributes

CVertex ** m_pVertex = nullptr
 2D array of vertices.
 
UINT m_nWidth = 0
 Number of vertices wide.
 
UINT m_nHeight = 0
 Number of vertices high.
 
float m_fDelta = 0.0f
 Spacing between vertices in Render World units.
 

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 LParticleEngine2D * m_pParticleEngine = nullptr
 Pointer to particle engine.

 
static CVectorFieldm_pVectorField = nullptr
 Pointer to a vector field.
 
static CBubbleEnginem_pBubbleEngine = nullptr
 Pointer to a bubble engine.
 
static bool m_bDrawVectors = false
 Do we draw the vector field?
 
static bool m_bDrawPressure = false
 Do we draw the pressures from the vector field?
 
static bool m_bDrawPoints = false
 Do we draw the points from the vector field?
 
static bool m_bDrawParticles = true
 Do we draw particles?
 
static eWordColor m_eCurColor = eWordColor::Black
 Color of the current word.
 
static bool m_bTried = false
 Whether the player has tried on the current word.
 
static UINT m_nWordCnt = INITIAL_WORDCOUNT
 Number of words.
 
static UINT m_nScore = 0
 Current score, can be negatice.
 
static UINT m_nDelivered = 0
 Number of words delivered.
 
static UINT m_nExcellent = 0
 Number of words scored as excellent.
 
static UINT m_nGood = 0
 Number of words scored as excellent.
 
static UINT m_nFair = 0
 Number of words scored as excellent.
 
static UINT m_nFailed = 0
 Number of words the player got wrong.
 
static UINT m_nMissed = 0
 Number of words the player did not attempt.
 
static eGameState m_eGameState = eGameState::Instructions
 Current game state.
 
static UINT m_nCntdownNum = 0
 The number we are displaying in the countdown.
 

Detailed Description

A vector field.

Our vector field consists of an evenly-spaced grid of points. At each point there is a vector and a pressure. The pressure is the magnitude of the vertex at that point.

Constructor & Destructor Documentation

◆ CVectorField()

CVectorField::CVectorField ( UINT  n)

Constructor.

Create a vector field with a given number of vertices horizontally. The vertices will be spaced out evenly horizontally. The vertical spacing will be the same as the horizontal spacing, and as many rows of vertices as fits into the window will be used.

Parameters
nNumber of vertices across the window.

Member Function Documentation

◆ Clear()

void CVectorField::Clear ( )

Clear.

Set all of the vectors and pressures to zero, and mark all vertices as unoccupied.

◆ Detonate()

void CVectorField::Detonate ( const Vector2 &  p)

Detonation.

Add vectors acting outwards from a point. Eight vectors are added, horizontal, vertical,diagonal, and back-diagonal. The magnitude of the diagonal and back-diagonal vectors are divided by sqrt(2).

Parameters
pA point.

◆ Draw()

void CVectorField::Draw ( )

Draw.

Draw the vector field points, vectors and pressures as directed by the corresponding Boolean member variables.

◆ DrawPoints()

void CVectorField::DrawPoints ( )
private

Draw points.

Draw the vector field points.

◆ DrawPressures()

void CVectorField::DrawPressures ( )
private

Draw pressures.

Draw pressures as light and dark squares.

◆ DrawVectors()

void CVectorField::DrawVectors ( )
private

Draw vectors.

Draw the vectors as scaled arrows.

◆ GetAveragePressure()

const float CVectorField::GetAveragePressure ( UINT  i,
UINT  j 
) const
private

Average pressure around a vertex.

Compute the average pressure of 8 neighboring vertices weighted by distance. This does not include the center vertex.

Parameters
iRow number of vertex.
jColumn number of vertex.
Returns
Average pressure around vertex.

◆ GetClosestVector()

const Vector2 CVectorField::GetClosestVector ( const Vector2 &  p) const

Get vector closest to a point.

Get the vector acting on a point by the vector field.

Parameters
pA point.
Returns
The vector acting on p.

◆ GetClosestVertex()

const bool CVectorField::GetClosestVertex ( const Vector2 &  p,
UINT &  i,
UINT &  j 
) const
private

Closest vertex.

Find the horizontal and vertical indices of the closest vertex.

Parameters
pA point.
i[OUT] Vertical index.
j[OUT] Horizontal index.
Returns
true if the indices are without range, false for failure.

◆ GetVectorFromPressure()

const Vector2 CVectorField::GetVectorFromPressure ( UINT  i,
UINT  j 
) const
private

Average vectors around a vertex.

Compute the average vector of 8 neighboring vertices based on pressure, weighted by distance. This does not include the center vertex.

Parameters
iRow index of vertex.
jColumn index of vertex.
Returns
Average of the eight neighbors of vertex.

◆ isInternalVertex()

const bool CVectorField::isInternalVertex ( UINT  i,
UINT  j 
) const
private

Check that indices are for a non-edge vertex.

A vertex is isInternalVertex if it is in range and not an edge vertex.

Parameters
iRow index.
jColumn index.
Returns
true if vertex [i][j] is in range and not an edge vertex.

◆ RelaxPressure()

void CVectorField::RelaxPressure ( const UINT  n)
private

Relax pressure at the vertices.

Relax the pressure at each vertex by lerping it towards the weighted average of the pressures of its neighbors. The weights are Euclidean distances, that is, diagonal neighbors are weighted by \(\sqrt{2}\) while the others are weighted \(1\).

Parameters
nNumber of iterations of Gauss-Seidel relaxation.

◆ Step()

void CVectorField::Step ( const UINT  n)

Perform an animation step.

Relax the pressures, update the vectors, and mark all vertices unoccupied. Vectors at each vertex are lerped with the average of the vectors from its neighbors.

Parameters
nNumber of iterations of relaxation.

◆ Update() [1/2]

void CVectorField::Update ( const Vector2 &  p,
const Vector2 &  v 
)

Update pressures around moving object.

Update the pressures due to a moving point. Pressure increases ahead of the point and decrases behind it. Since each vertex can be updated only once per step, increases are done before decreases.

Parameters
pPosition of moving point.
vVelocity of moving point.

◆ Update() [2/2]

void CVectorField::Update ( UINT  i,
UINT  j,
UINT  r,
float  p 
)
private

Update pressures around moving object.

The pressures around a vertex are lerped towards new values. The lerp fraction is hard-coded. Each vertex can be updated only once per step, so make sure that the important ones get done first.

Parameters
iRow index.
jColumn index.
rEffect radius.
pNew pressure.