XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Racing game lap counter

Last post 05-06-2008 7:37 PM by jwatte. 4 replies.
  • 05-06-2008 8:23 AM

    Racing game lap counter

    I am doing a 3d racing game and cant figure out how to do the lap counter.

    Can anyone please help?
  • 05-06-2008 10:40 AM In reply to

    Re: Racing game lap counter

    Most racing games have a series of checkpoint zones. As a car passes through each one you use some sollision code to check them off. This ensures people go all the way round the track.

    Everytime all 4 (or however many you have) checkpoints get checked off in order then increment and display the lap counter.


    The ZBuffer - News and information for XNA and Managed DirectX
  • 05-06-2008 11:11 AM In reply to

    Re: Racing game lap counter

    To expand on ZMan's recommendation, you could do this with a BoundingBox object around the vehicle and finish line.  Here is a sample class.  I put the full sample at http://www.gamedeveloperonline.com/creators/lapCounter.zip 

    This actually just shows a triangle moving back and forth to keep it simple but the counter works and displays as you might need it.

    class LapCounter
    {
     private BoundingBox finishLine;
     private BoundingBox vehicleBox;
     private int         lapCount;
     private bool        checkPointA;
     private bool        checkPointB;

    public LapCounter()
    {

        // set bounding box around finish line
        finishLine.Max  = new Vector3(+1.0f,+0.5f,+0.5f);
        finishLine.Min  = new Vector3(-1.0f,-0.5f,-0.5f);

        // start at 0 laps
        lapCount        = 0;

        // clear check points
        checkPointA = checkPointB = false;                     
    }

    private void setVehicleBox(Vector3 vehiclePosition)
    {
        vehicleBox.Max = new Vector3(vehiclePosition.X + 0.3f,
         vehiclePosition.Y + 1.0f,
         vehiclePosition.Z + 0.1f);
        vehicleBox.Min = new Vector3(vehiclePosition.X - 0.3f,
         vehiclePosition.Y,
         vehiclePosition.Z - 0.1f);
    }

    public void CountLaps(Vector3 vehiclePosition)
    {
        if (checkPointA && checkPointB)
        {
     setVehicleBox(vehiclePosition);
     
     // check if vehicle intersects finish line
     if (vehicleBox.Intersects(finishLine))
     {
         lapCount += 1;

         // clear check points
         checkPointA = checkPointB = false;
     }
        }
    }

    public void setCheckPointA()
    {
        checkPointA = true;
    }

    public void setCheckPointB()
    {
        checkPointB = true;
    }

    public int LapCount()
    {
        return lapCount;
    }
    }

  • 05-06-2008 6:51 PM In reply to

    Re: Racing game lap counter

    You want to make sure you don't increment the lap counter each frame you're in contact with the box. Turning off all the checkpoints, and making sure that you check that those are up in order to increment the lap counter should work fine.
    XNA QuickStart Engine | My site
    "I'll be whatever I want to do!", Philip J. Fry
  • 05-06-2008 7:37 PM In reply to

    Re: Racing game lap counter

    Easiest is to keep a reference to each checkpoint trigger in some list/array. Then keep an index into the array for the "next" trigger to detect. When the player triggers a trigger, check whether it's the trigger you expect. If so, increment the counter. If the counter goes beyond the list of triggers, reset it to 0 and chalk up another lap.

      List<Trigger> triggers = ...;
    int index = 0;
    int laps = 0;

    void OnTrigger(Trigger t) {
    if (triggers[index] == t) {
    ++index;
    if (index == triggers.Count) {
    ++laps;
    index = 0;
    if (laps == NumLapsInARace) {
    ....
    }
    }
    }
    }


    Note that the "goal" will be the last trigger -- but the player will actually drive through it first. However, because it's not actually the first trigger in the list, it will be ignored when that's done.

    You can also use the list of triggers and the index to point the user at the next checkpoint, by drawing a compass/arrow that points towards the center of the next trigger element. I would recommend using at least 4 triggers on a round racetrack, and a larger number if you have lots of doubling back and forth, to avoid cheating by going off course. Also, if you use trigger volumes, then the leading edge should cross the road exactly where the checkpoint is supposed to be, so you don't get a "goal" registered before the car has actually hit the goal line. Also don't extend triggers outside the width of the road, because that can lead to cheats.


    Jon Watte, Direct3D MVP kW X-port 3ds Max .X exporter kW Animation source code
Page 1 of 1 (5 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG