![]() |
The Stroop Test Game
Game Physics with a 2D Physics Engine
|
A vector field. More...
#include <VectorField.h>


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 CObjectManager * | m_pObjectManager = nullptr |
| Pointer to the object manager. | |
| static Sage::CParticleEngine2D * | 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 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 CStroop * | m_pStroop = nullptr |
| Pointer to Stroop caricature. | |
| static eDrawMode | m_eDrawMode = eDrawMode::Sprites |
| Draw mode. | |
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 | ) |
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 | ( | ) |
Set all of the vectors and pressures to zero, and mark all vertices as unoccupied.

| 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).
| p | A point. |


| void CVectorField::Draw | ( | ) |
Draw the vector field points, vectors and pressures as directed by the corresponding Boolean member variables.


|
private |
Draw the vector field points.

|
private |
Draw pressures as light and dark squares.

|
private |
Draw the vectors as scaled arrows.

|
private |
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 the vector acting on a point by the vector field.
| p | A point. |


|
private |
Find the horizontal and vertical indices of the closest vertex.
| p | A point. |
| i | [OUT] Vertical index. |
| j | [OUT] Horizontal index. |


|
private |
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 |
A vertex is isInternalVertex if it is in range and not an edge vertex.
| i | Row index. |
| j | Column index. |

|
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\).
| n | Number of iterations of Gauss-Seidel relaxation. |


| 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.
| n | Number of iterations of relaxation. |


| 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.
| p | Position of moving point. |
| v | Velocity of moving point. |

|
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.
| i | Row index. |
| j | Column index. |
| r | Effect radius. |
| p | New pressure. |

