The Stroop Test Game
Game Physics with a 2D Physics Engine
Loading...
Searching...
No Matches
CVectorField Class Reference

A vector field. More...

#include <VectorField.h>

Inheritance diagram for CVectorField:
Collaboration diagram for CVectorField:

Public Member Functions

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

Private Member Functions

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

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 CBox2DRenderer * m_pRenderer = nullptr
 Pointer to renderer.
 
static CObjectManagerm_pObjectManager = nullptr
 Pointer to the object manager.
 
static Sage::CParticleEngine2D * 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 int m_nScore = 0
 Current score, can be negative.
 
static int m_nDelivered = 0
 Number of words delivered.
 
static int m_nExcellent = 0
 Number of words scored as excellent.
 
static int m_nGood = 0
 Number of words scored as excellent.
 
static int m_nFair = 0
 Number of words scored as excellent.
 
static int m_nFailed = 0
 Number of words the player got wrong.
 
static int m_nMissed = 0
 Number of words the player did not attempt.
 
static eGameState m_eGameState = eGameState::Loading
 Current game state.
 
static UINT m_nCntdownNum = 0
 The number we are displaying in the countdown.
 
static CStroopm_pStroop = nullptr
 Pointer to Stroop caricature.
 
static eDrawMode m_eDrawMode = eDrawMode::Sprites
 Draw mode.
 

Detailed Description

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)

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.
Here is the call graph for this function:

Member Function Documentation

◆ Clear()

void CVectorField::Clear ( )

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

Here is the caller graph for this function:

◆ Detonate()

void CVectorField::Detonate ( const Vector2 & p)

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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Draw()

void CVectorField::Draw ( )

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

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

◆ DrawPoints()

void CVectorField::DrawPoints ( )
private

Draw the vector field points.

Here is the caller graph for this function:

◆ DrawPressures()

void CVectorField::DrawPressures ( )
private

Draw pressures as light and dark squares.

Here is the caller graph for this function:

◆ DrawVectors()

void CVectorField::DrawVectors ( )
private

Draw the vectors as scaled arrows.

Here is the caller graph for this function:

◆ GetAveragePressure()

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

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.
Here is the caller graph for this function:

◆ GetClosestVector()

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

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

Parameters
pA point.
Returns
The vector acting on p.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetClosestVertex()

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

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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetVectorFromPressure()

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

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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isInternalVertex()

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

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.
Here is the caller graph for this function:

◆ RelaxPressure()

void CVectorField::RelaxPressure ( const UINT n)
private

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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Step()

void CVectorField::Step ( const UINT n)

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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Update() [1/2]

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

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.
Here is the call graph for this function:

◆ Update() [2/2]

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

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.
Here is the call graph for this function:
Here is the caller graph for this function: