It is currently Sat May 25, 2013 3:00 pm

 All times are UTC

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Light Cycles collision detectionPosted: Wed Apr 25, 2012 11:06 pm
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;
}

Top

 Post subject: Re: Light Cycles collision detectionPosted: Sat Apr 28, 2012 7:07 am
 Bibliotherapist

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 6719
Location: Oxford, Englandshire
At a guess I would say that the player is also represented by last rectangle in the array of RECT objects. I think the -2 value prevents the RECT that represents the player and the trail immediately behind the player from being included in the collision checks.

This does mean that if the player won't be killed by the trail that is touching him as he leaves it, but doesn't turn for a while, the trail left immediately behind will not 'kill' the opponent.

_________________
20 GOTO 10

Top

 Post subject: Re: Light Cycles collision detectionPosted: Sat Apr 28, 2012 8:57 am
 Bytewise

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 277
Location: Here (where else?)
A useful tactic is to try and code the routine yourself. That usually gives insights in the problems that need to be solved there.

Also, you can compute the result with your own routine and with the existing one, and compare the outcomes. When they are different, you have a precise example which you can analyze (possibly updating your version, and playing the compare game again with the improved solution).

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ Forums    Forum Rules and Posting Guidelines Wiki Discussion    Help    Content Issues Game Programming Discussion    C and C++ Game Programming    Java Game Programming    Language Agnostic Programming    .NET Game Programming    VB Game Programming    Mobile Game Programming    Web-Based Game Programming    Other Languages    OpenGL Development    Direct X Development Game Development Discussion    Game Design    Game Media Off-Topic Discussion    Announcements    Off-Topic    Community Projects    News