Racing game position sorting logic

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By luiscesjr

Hi everyone. Right now I’m not with my pc around, so as soon as I can I’ll update here with some code if needed.
Let me explain what is my game about and why what’s my logic problem:

The race in my game takes place with 10 car entities, 9 are purely AI and 1 is an AI that occasionally the player may interfere with it’s behavior. So basically all of them follow a path2d with a pathfollow2d in it, while avoiding each other and other obstacles.

How I have set up my initial race position, I retrieve the following variables:

Path2d size, each car position in the path, and lap count. The lap should be incremented by 1 once the first car pass through its checkpoint (The code for this is pretty easy, I just don’t have a car for that lol)

So my main problem is, how should I approach this? I mean, what’s the program logic I should use here? I’m thinking it like this:

Have a variable for each of their position minus the path size, so the greater the value, the further behind you are. Sort this out, but I should also check for lap count, if a car is on lap 2 it should not Sort the same as a car on lap 1 for example.
Even if I Sort their values in an array for instance, how am I supposed to know which is which after the array is sorted?

BTW I’m adding a label above each car so they can show their position real time.

The path2d size and position approach is perfect for me, and I think it’s a much better solution than checkpoints, I really just need to sort this sorting problem lol.

Thanks in advance! If more info or code are needed please don’t hesitate to ask me!

:bust_in_silhouette: Reply From: Inces

Do You mean sorting array of cars in order of winning position ? (Like 1st,2nd,3rd) ?
If so, I believe sort_custom method is ideal for that
Your idea is good. However You will surely reach a problem how should a car blindly follow a path while straying from it to avoid collisions. You will come to conclusion, that You have to design some kind of simple pathfinding. So checking path size for winning position will not be applicable anymore. But You will propably end up with some kind of points to indicate advancement over the path. Lets call them points for now.

Lets make an integer variable in each car to sign its current advancement. Whenever a car reaches a point, let it add +1 to this variable. It will win the race when this variable reaches amount of all points in a lap * number of laps. Lets call this variable _advancement

Lets create a dictionary variable in your game managing node. Let it be var podium = {}
Every time car reaches a point it will update this variable :

podium[self] = _advancement

And lets finally create an array to display cars in correct order. For example var carorder =
Evey time podium is updated ( best on setget ), gamemanager will call carorder.sort_custom(self, "sortwinner")
Method should be in game manager script :

func sortwinner(a,b):
       if podium[a] > podium[b] :
             return true
       return false

You don’t have to use dictionary, You can design your sorting to get advancement straight from the car nodes. But I believe my way might be optimal

Thanks a lot for your suggestion, actually on reddit someone also told me about sort_custom, and I ended up using that. I didn’t have time yesterday to update the post here, but I’ll do it now. Again thank you, you gave me some good ideas for other things not only for the race ending aswell! The reason why I am using the path size and the node position on the path is to update the car’s position in real time, not only at the race’s ending. To counter the issues that following that path can bring, I’m also using state machines to define some other behaviours other than blindly going foward.

I’ll leave the reddit links here, until I update my main post:
Reddit - Dive into anything

Reddit - Dive into anything

I hope all the discussion will also help more people in the future!

luiscesjr | 2022-04-01 11:57