![]() |
The 8-ball Pool End Game
Ian Parberry's "Introduction to Game Physics"
|
The object manager. More...
#include <ObjectManager.h>
Public Member Functions | |
CObjectManager () | |
Constructor. | |
~CObjectManager () | |
Destructor. | |
void | create (eSprite, const Vector2 &) |
Create new object. More... | |
void | clear () |
Reset to initial conditions. More... | |
void | move () |
Move all objects. More... | |
void | Draw () |
Draw all objects. More... | |
void | ResetImpulseVector () |
Reset the Impulse Vector. More... | |
void | AdjustImpulseVector (float) |
Adjust the Impulse Vector. More... | |
void | AdjustCueBall (float) |
Move cue-ball up or down. More... | |
void | Shoot () |
Shoot the cue ball. More... | |
bool | BallDown () |
Is a ball down in a pocket? More... | |
bool | CueBallDown () |
Is the cue ball down in a pocket? More... | |
bool | AllStopped () |
Have all balls stopped moving? More... | |
Private Member Functions | |
void | BroadPhase () |
Ball, rail, and pocket collision response for all balls. More... | |
void | NarrowPhase (CObject *, CObject *) |
Ball collision response for two balls. More... | |
bool | BallCollide (CObject *, CObject *, float &) |
Ball collision response for two balls. More... | |
void | RailCollide (CObject *) |
Collision response for ball with rail. More... | |
void | PocketCollide (CObject *) |
Collision response for ball with pocket. More... | |
Private Attributes | |
LParticleDesc2D | m_cPDesc0 |
Particle descriptor for balls in step mode. | |
LParticleDesc2D | m_cPDesc1 |
Particle descriptor for collisions in step mode. | |
LParticleDesc2D | m_cPDesc2 |
Particle descriptor for collisions in real-time mode. | |
CObject * | m_pCueBall = nullptr |
Cue ball object pointer. | |
CObject * | m_p8Ball = nullptr |
8 ball object pointer. | |
float | m_fCueAngle = 0 |
Cue ball impulse angle. | |
bool | m_bDrawImpulseVector = true |
Whether to draw the impulse vector. | |
Additional Inherited Members | |
![]() | |
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 bool | m_bShowCollisions = false |
Show ball positions at TOI. | |
static bool | m_bStepMode = false |
Is in step mode. | |
static bool | m_bStep = false |
Step flag. | |
static float | m_fXMargin = 78.0f |
Horizontal margin. | |
static float | m_fYMargin = 64.0f |
Vertical margin. | |
static Vector2 | m_vMargin = Vector2(m_fXMargin, m_fYMargin) |
Margins. | |
static Vector2 | m_vTopLPocket = Vector2(71, 478) |
Position of top left pocket. | |
static Vector2 | m_vTopRPocket = Vector2(955, 478) |
Position of top right pocket. | |
static Vector2 | m_vTopCPocket = Vector2(514, 478) |
Position of top center pocket. | |
static Vector2 | m_vBotLPocket = Vector2(71, 53) |
Position of bottom left pocket. | |
static Vector2 | m_vBotRPocket = Vector2(955, 53) |
Position of bottom right pocket. | |
static Vector2 | m_vBotCPocket = Vector2(514, 48) |
Position of bottom center pocket. | |
A collection of all of the game objects.
void CObjectManager::AdjustCueBall | ( | float | d | ) |
Adjust the cue ball up or down on the base line, being careful stop it at the horizontal rails. This should only be used in eGameState::Initial, but no check is made here.
d | Distance to move by. |
void CObjectManager::AdjustImpulseVector | ( | float | a | ) |
Adjust the angle of the impulse vector. This should only be used in eGameState::Initial or eGameState::SetupShot, but no check is made here.
a | Amount to add to the angle of the impulse vector. |
bool CObjectManager::AllStopped | ( | ) |
Check whether both the cue-ball and the 8-ball have stopped moving. Notice that we can set the velocity vector to the zero vector and expect it to succeed because CObject::move zeros out the velocity of slow-moving objects.
Begin by computing velocities relative to b1. Calculate the relative displacement c and the distance cdotvhat along the normal to common tangent vhat. Calculate d1 and d2, the distances moved back by b1 and b2 (respectively) to their positions at TOI. Calculate time elapsed since TOI, tdelta = d2/s2
. Move balls back to their positions at TOI and compute their new velocities. Move both balls after impact using their new velocities and tdelta.
p0 | Pointer to first object. |
p1 | Pointer to first object. |
s | [OUT] Collision speed. |
bool CObjectManager::BallDown | ( | ) |
Check whether the cue-ball or the 8-ball is in a pocket.
|
private |
Collision response for all balls against each other and the rails and the pockets. We do pocket collision for all balls first to remove them from the subsequent calculations.
void CObjectManager::clear | ( | ) |
Delete all of the objects in the game.
void CObjectManager::create | ( | eSprite | t, |
const Vector2 & | v | ||
) |
Create an object and put a pointer to it on the object list.
t | Sprite type. |
v | Initial position. |
bool CObjectManager::CueBallDown | ( | ) |
Check whether the cue-ball is down a pocket.
void CObjectManager::Draw | ( | ) |
Draw all of the objects in the game, two balls, the directional arrow, and the step mode indicator if necessary.
void CObjectManager::move | ( | ) |
Move all of the objects in the object list and perform broad phase collision detection and response. If in Step Mode, drop a particle.
Perform collision detection and response for a pair of objects. Play impact sound if they collide.
p0 | Pointer to the first object. |
p1 | Pointer to the second object. |
|
private |
Collision and response for ball-in-pocket. Check for a collision and does the necessary housework for disabling a ball that is in a pocket. If there is a collision, a sound is played at a volume proportional to the speed of collision and panned to the left or right according to where the pocket is on the table.
b | Pointer to a ball object to collide with pockets. |
|
private |
Collision detection and response for ball hitting any rail. Check for a collision and do the necessary housework for reflecting the ball if it hits a rail. If there is a collision, a sound is played at a volume proportional to the speed of collision and panned to the left or right according to where the collision occurred.
b | Pointer to a ball object to collide with rails. |
void CObjectManager::ResetImpulseVector | ( | ) |
Make the impulse vector point from the center of the cue-ball to the center of the 8-ball and set it as visible so it gets drawn (assuming that the only reason to reset the impulse vector is because it needs to be drawn).
void CObjectManager::Shoot | ( | ) |
Shoot the cue-ball by giving it a fixed impulse. Disable the drawing of the impulse vector and play a sound that is panned left or right depending on where the cue-ball is on the table.