# Rank of cars in racing game

Hi guys, I’m making another template for a racing minigame and the issue of car positions is really giving me headaches. I’ve seen some Unity and Construct tutorials on how to do it but I can’t do it.

I’ve tried sections a each section of the track but I have not succeeded, I have also tried calculating the distance between the cars but nothing.

Let’s see if anyone can give me any advice this requires a better mind than mine. maybe @bouh, @silver-streak or even @4ian can help me with this.

Right now I only have a silly variable to display the number based on the repeat but the intention is to use that variable to display the car rank in race.

Here a preview of the game so far.

From what I know about racing the inside track is really important as it’s the shortest path. So if you break your track into segments it should be like this:

For sections I through IV you measure which car has the closest distance to the purple point on the corner of that segment. For section V you measure shortest distance to the finish line.

So total ordered sorting algorithm:
Which car has the most laps?
If tied: Which car has the highest section (I-V)
If tied: Which car in the section is closest to the inner track corner (purple dot on that section or flat distance to finish line for section V)

Hi @krunkster your logic is imbatible but how in the heck I translate that logic to GDevelop I couldn’t even set an Array to order the 4 cars if just calculate the the section_number of the car increment the variable PositionGroup.Variable(section_number) when the car collides with position_check then position_check has a variable named position from 1 to 4 so if is 4 then reset to 0

Look what I’m doing now is almos there but fails if a car do not collides with position_check

The 4 cars are inside a group PositionGroup so I can manage the collision with the group.

I don’t know I think I’m going to release this template without the rank system so maybe other developer can figure out how to.

It’s a sorting problem, and Gdevelop doesn’t really offer sorting tools…so If it were me I’d use JavaScript because it has lots of sorting capabilities. However, as you are already attempting to do, you can also just create the sorting algorithm using multiple sets of imbedded loops. It will be tedious.
Even just trying to sort 4 cars based on the number of laps without JavaScript is painful to think about for me because you have to swap around the contents of the array manually.

If you are looking for a way to do this without sorting, I have a hacky idea:
Create a hidden sprite called “PlaceLine” that is the width of the track
Create a path for that “PlaceLine” sprite to travel backwards along the track in a non-stop loop, really fast, but not so fast that it doesn’t overlap itself between frames.
Create a counter associated with that sprite, maybe an instance variable named “CurrentPlace”
CurrentPlace should start at 1.
During game play whenever PlaceLine Collides with a car assign that car the CurrentPlace value and increment by one. If CurrentPlace is greater than 4, then reset it to 1.

This will only work for cars on the same number of laps, so you could also keep track of the current highest lap (lets call that X), and have PlaceLine runs the course backwards X times looking only for collisions with cars at X laps (decrementing each loop).

That’s probably how I’d do it in GDevelop without JavaScipt.

1 Like

Couple of after thoughts on the “scanner” PlaceLine.
It would need to be square probably, otherwise you’d have to build rotation logic in as it travels along the track.
It would probably need to have a counter on it for each “scanning” lap that it’s made, and when it adjusts the place of a car it updates a variable on the car called “LastScan” with that counter so that the collision logic can ignore any cars it’s already marked with the current “LastScan”.

1 Like

I finally released here

without the rank system in case you want to check the source is in my itch.io