![]() |
The Stroop Test Game
Ian Parberry's "Introduction to Game Physics"
|
A vector field. More...
#include <VectorField.h>
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 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 LParticleEngine2D * | m_pParticleEngine = nullptr |
Pointer to particle engine. | |
static CVectorField * | m_pVectorField = nullptr |
Pointer to a vector field. | |
static CBubbleEngine * | m_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. | |
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.
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.
n | Number of vertices across the window. |
void CVectorField::Clear | ( | ) |
Clear.
Set all of the vectors and pressures to zero, and mark all vertices as unoccupied.
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).
p | A point. |
void CVectorField::Draw | ( | ) |
Draw.
Draw the vector field points, vectors and pressures as directed by the corresponding Boolean member variables.
|
private |
Draw points.
Draw the vector field points.
|
private |
Draw pressures.
Draw pressures as light and dark squares.
|
private |
Draw vectors.
Draw the vectors as scaled arrows.
|
private |
Average pressure around a vertex.
Compute the average pressure of 8 neighboring vertices weighted by distance. This does not include the center vertex.
i | Row number of vertex. |
j | Column number of vertex. |
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.
p | A point. |
|
private |
Closest vertex.
Find the horizontal and vertical indices of the closest vertex.
p | A point. |
i | [OUT] Vertical index. |
j | [OUT] Horizontal index. |
|
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.
i | Row index of vertex. |
j | Column index of vertex. |
|
private |
Check that indices are for a non-edge vertex.
A vertex is isInternalVertex if it is in range and not an edge vertex.
i | Row index. |
j | Column index. |
|
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\).
n | Number of iterations of Gauss-Seidel relaxation. |
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.
n | Number of iterations of relaxation. |
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.
p | Position of moving point. |
v | Velocity of moving point. |
|
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.
i | Row index. |
j | Column index. |
r | Effect radius. |
p | New pressure. |