0 votes

For example, I have two NPC cars + player car , how can I best count player/car order?
1. throughout the race( on the track )....more complex
2. only at the finish line....easier
.....solution number two would be sufficient for me.
Thanks for help

in Engine by (450 points)

1 Answer

+1 vote
Best answer

If solution 2 is good for you, why don't you just do it? ^^

Solution 1 requires that you know where the car is in the road line between start and finish. A way to achieve it is to project the car's position on the curve describing the road, and calculate the distance of this point along the curve so that you can compare two car's advancements on the track. The rest is math ;)

If you don't need good precision you can cheat using checkpoints (see comments below)

by (27,240 points)
selected by

Thanks Zylann,
I go for solution 2....but how the finish line get to know order of a cars....groups or object ID or something else ?...it would be better to show me more on the example script...if that's possible?....I'm learning GDscript yet :-)....i don't want it only for myself but for many beginners too....this is very useful because it's the basis for a racing games.

...I have done laps counting on the finish line (area 2D trigger)
so....player car add to group 'player' and NPC cars add groups 'car1' and 'car2' and
triggering groups?

Knowing player order from only the finish line requires that you store two things when they cross the line:

1) The last time at which the player crossed the finish line
2) The number of laps the player achieved

Then you can tell if a player is beyond another by doing the following test:

if player_a.lap_count == player_b.lap_count:
    # Players are in the same lap but one was late
    if player_a.lap_time > player_b.lap_time:
        # Player A is first
    else:
        # Player B is first
        # Note: if a car takes the lead over another during the same lap,
        # this code won't detect it, which is why solution 1 would be better.
elif player_a.lap_count > player_b.lap_count:
    # Player A is first
else:
    # Player B is first

And then if you need the order as a list, then get all layers as an array, and use the sort_custom function using the code above.

It's not very accurate for a racing game though. You could even this out by adding checkpoints maybe (or many many invisible checkpoints), but the most accurate solution would be 1.

You can alternatively use the travelled distance since last lap/checkpoint instead of lap_time, which would be more accurate but still biased because not based on the road curve.

Thank you very much Zylann,
for example script and solution directions.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories