-1

Summary:

So, as the question suggests I'm trying to develop a racing game. Although, I need to create a system to keep track of what positions the Player and AI are in. Positions as in first place, second place, third place, etc.

What I've tried:

At first I was going to use a system that was made by Jimmy Vegas in one of his tutorials for developing a racing game, but this system only works with two cars and an enclosed track/circuit. My game is an off road racing game, this means the track is open and the player will be able to make there own shortcuts and no walls will restrict the player's ability to roam the map. This renders Jimmy Vegas's system useless for my type of racing game. I tried to mess around with the code to find a way to still use his system, but I'm afraid it won't work. Any help would be greatly appreciated!

  • How would you sum up your question in **one** sentence? Hint: "How to write a working script?" is no valid question ... – derHugo Jan 09 '21 at 11:40
  • I modified the question hopefully this helps. – Johnny Shumway Jan 09 '21 at 16:09
  • I came here from Reopen Votes queue and while not being a matter expert BY FAR, I have to say I have trouble wrapping my head around your problem. If a race doesn't have a track it would mean it needs to have start and finish as separate points (otherwise player could just turn around to cross the line). That in turn would mean that player progress is measured simply as DISTANCE FROM FINISH. Does this make sense? So to compare two players you would take their current straight-line-distance divided by total straight line distance? – Mike Szyndel Jan 10 '21 at 10:30
  • @MikeSzyndel I eventually just abandoned the project, but what I meant was the track isn't closed-in, there was still checkpoints, but the off-road portions of the track would've been made of rough terrain which would slow the player down. So, at the expense of speed the player would be able to cut the distance between two checkpoints in-two by using a shortcut. Basically there wouldn't be a barrier that prevents the player from driving of the road/track. – Johnny Shumway May 13 '21 at 00:59
  • Ok so I think in this case my idea still stands. Player's (or AI's) distance from finish/end of lap is a sum of straight line distances between the checkpoints. This does however fall apart if the track can take the player away from the next checkpoint. I guess that's 2+ player racing games have tracks... – Mike Szyndel May 18 '21 at 14:03

1 Answers1

0

Possible Solution

I won't be writing the code for this as it's too implementation-specific, but here's some checklist pseudo-code.

Use the following to compute relative positions between two players:

  1. Compare first by laps.
  2. If same laps, compare by checkpoints.
  3. If same checkpoints, compare using a distance function to next checkpoint. This function may be as simple as Euclidean distance, but I'd recommend whatever you're using for your AI pathfinding.

If you want absolute positions of all players, make an array, and sort it using laps, then breaking ties with checkpoints, and finally breaking ties with distance.

Even a hundred-entity race running this every frame shouldn't have any problems.

Harshdeep Singh
  • 305
  • 1
  • 4
  • 1
    Thank you, this will help me a lot. I'll look into these. Thank you, idk why someone down voted your answer. – Johnny Shumway Jan 09 '21 at 14:31
  • Quick question though, so based on what you said I should Create a script that uses something like this: var heading = checkpoint.position - racer.position; And then it should take the magnitude of that heading to check which racer is closest. Whoever is closest will be considered as being in first place. Although, how exactly would I sort through the array to determine the other positions? – Johnny Shumway Jan 09 '21 at 19:05
  • "Although, how exactly would I sort through the array to determine the other positions?" is a bit too vague for me to understand what you mean. You sort them based on their distance from the checkpoint. This sort should be the last tie-breaker, i.e. comparison for racers that have both equal number of laps and equal number of checkpoints passed. – Harshdeep Singh Jan 09 '21 at 19:09
  • Oh I get it, thank you! – Johnny Shumway Jan 10 '21 at 09:01