# Best way to count laps in racing game

Was looking to program a car racing 3d game and everything went well until it was time to determine laps. i tried ray casting at check pts to determine collisions with the car but it couldnt tell me how many laps the car has made. would appreciate any bright idea

I would suggest using int variable which you increment each lap.

Are you really saying that you have solved other problems and got stuck with this one?

One easy way would be to use polygon areas large enough for you to always render a frame when the car position is within it.
So depending on the speed of the car and the fps of the game, you can determine the length of the checkpoint required to ensure it will capture it.

But yea there are heaps of way to approach this problem.

[java]
int[] xPoints = new int[]{p1x, p2x, p3x, p4x};
int[] yPoints = new int[]{p1y, p2y, p3y, p4y};
Polygon checkPoint = Polygon(xPoints, yPoints, 4);
Polygon[] checkPoints = {array of all checkpoints};
Polygon currentCheckpoint = checkPoints[0];
int checkpointsCleared = 0;

// Then in the update loop of the control checking for it.

if(currentCheckpoint .contains(car.getLocalTranslation().getX(), car.getLocalTranslation().getZ()) {
currentCheckpoint = checkPoints[++checkPointsCleared%checkPoints.length];
}
[/java]

1 Like
@abies said: I would suggest using int variable which you increment each lap.

Are you really saying that you have solved other problems and got stuck with this one?

Lol, @Rhymez, this is why you need to give all information even if it seems obvious to you. People are not in your head

I agree to what @perfecticus said, most games do it in a way where you simply have to cross certain areas as â€ścheckpointsâ€ť on the way, a simple position check should do. Make sure your player crosses them in succession and youâ€™re good, that also avoids the player taking shortcuts.

@ abies ,i guess i didnt fully explain myself. @perfecticus ,you just made my day ,i had drawn a path along a curvy racing track using a motionpath Which clearly didnt have the functions that i need ,im still wondering though ,which xPoints and yPoints should i collect, along the edges(boundaries) of the track @norman ,itz always been my weakness ,il try to explain myself fully next time

Well you move something around that path i assume?
Get each frame the world position of the spatials being moved, and check if the distance between it and the goalline is less than x (for example width of track), remeber your result.
If the object was last frame in range and is now not anymore increment its counter

Okay i used a box instead of a polygon coz i was unsure how to draw a 2D object on such a diverse scene graph . I used
if(boxGeo.getWorldBound().contains()carControl.getPhysicsLocation)
method for each of the two boxes i created ,of cause ,i had seperate geometries for each box .For the i increanted a value int count and for the second box which was at the finish ,i had the that condition along with
â€¦&&count==1) locText.setText(â€śfinishedâ€ť)
but the problem was had this method executed in the update()

I meant to say the first box would increament count by ++count .So the prob was that since this whole process was in the update method,count was incremented for every second the car was within the box as i had a method for the first box
â€¦locText.setText(â€śreachedâ€ť+count)â€¦
and i could get values like 11 and 15 for count instead of 1.is there a better way of overcoming this coz if i use
â€¦&&count >0 â€¦
it would work but i still cannot determine the number of laps the player has made.

Okay so my phone cannot contain a lot of text so i have to keep commenting.
i wanted to add that i initialised count to zero and want it to be incremented once each lap so that i can determine the number of laps ,will appreciate more bright ideas

Add each checkpoint to a hashset.

Keep track of the box intersection from the previous frame. If there is no interesection, check to see if the previous frameâ€™s box is the finish line. If it is then check the size of your hashset to make sure all checkpoints were driven through and then increment the lap counterâ€¦ and clear the set. Clear the reference to the previous frames intersection, no matter what.

@pspeed Okay i get the idea .but since im still not farmiliar wit hashsets ,il try them tomorrow but i got to hear what they are all about just now so i can use
â€¦&&count>0 â€¦ Then add some data into a hashset ,thanks for the advice

Yesss! I finally got it to work using hashsets, im sure an array from perfecticus wouldv worked too. I defined two hashsets
HashSet <Geometry> lap1 =new HashSet <Geometry>();
the other one was lap2 . In the update method ,i had
Vector3f loc =carControl.getPhysicsLocation();BoundingVolume b1=box1Geo.getModelBound(); //then b2 for the finishline box