In the game the light cycles have trails that follow them from point to point and are painted according to each light cycles color(blue or orange). In order to check for a collision we are using the Win32 API PtInRect() function that sees if a point (a light cycle) is inside the segment of rectangle. The code somewhat confuses me and I was wondering if someone could explain it to me.
In the first section that checks to see if the light cycles has hit its own trail we see a for loop that loops through each segment of the line as a rectangle. What does the "-2" means in the for loop "g_iTrailLen[i] - 2".
Code:
RECT rcTmpTrail;
if (g_iTrailLen[i] > 2) // Must have steered at least once
{
for (int j = 0; j < g_iTrailLen[i] - 2; j++)
{
rcTmpTrail.left = min(g_ptCycleTrail[i][j].x, g_ptCycleTrail[i][j + 1].x) - 1;
rcTmpTrail.right = max(g_ptCycleTrail[i][j].x, g_ptCycleTrail[i][j + 1].x) + 1;
rcTmpTrail.top = min(g_ptCycleTrail[i][j].y, g_ptCycleTrail[i][j + 1].y) - 1;
rcTmpTrail.bottom = max(g_ptCycleTrail[i][j].y, g_ptCycleTrail[i][j + 1].y) + 1;
if (PtInRect(&rcTmpTrail, g_ptCycleTrail[i][g_iTrailLen[i] - 1]) != 0)
{
// The game is over
EndGame(1 - i);
return;
}
}
}
// See if the light cycle collided with the other cycle's trail
for (int j = 0; j <= g_iTrailLen[1 - i] - 2; j++)
{
rcTmpTrail.left = min(g_ptCycleTrail[1 - i][j].x, g_ptCycleTrail[1 - i][j + 1].x) - 3;
rcTmpTrail.right = max(g_ptCycleTrail[1 - i][j].x, g_ptCycleTrail[1 - i][j + 1].x) + 3;
rcTmpTrail.top = min(g_ptCycleTrail[1 - i][j].y, g_ptCycleTrail[1 - i][j + 1].y) - 3;
rcTmpTrail.bottom = max(g_ptCycleTrail[1 - i][j].y, g_ptCycleTrail[1 - i][j + 1].y) + 3;
if (PtInRect(&rcTmpTrail, g_ptCycleTrail[i][g_iTrailLen[i] - 1]) != 0)
{
// The game is over
EndGame(1 - i);
return;
}