GPWiki.org
GPWiki.org
It is currently Thu May 23, 2013 7:46 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: 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
  
 
PostPosted: Sat Apr 28, 2012 7:07 am 
Bibliotherapist
User avatar

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 6716
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.

_________________
10 PRINT "Bad Monkey ";
20 GOTO 10


Top
 Profile  
 
PostPosted: 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
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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 forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group