Race Position HELP!!

we created a racing game,, but it seems there a problem in positioning.. like (1st, 2nd, 3rd.. etc.) Can you help up to find some way to finish our project?? Please ?? :( thanks! :)

I actually learned how to do this from a video on YouTube (on GyroVorbis's Channel but it was Dual Threat who said it), the way that team did it was to set up 'checkpoints' along the track. Then to determine the position of the racers is in three steps: What lap you on. What checkpoint you on. Whats your distance to the next checkpoint. This meaning if you are on your final lap, and everyone else is on the 2nd lap you are in the lead. However, if you and someone else are on the same lap, but you are half the a track ahead of them, then you can tell by what checkpoint you are on. Now if you are both neck and neck with each other, then who ever was just SLIGHTYLY ahead of the other will be in the lead. Here is the link to the video explaining it.

YouTube Video: Go to 12:55 for the info you are looking for, but also check out the other stuff they got, it is really good.

Lastly, all credit for this idea goes to Dual Threat Studios.

Like xToxicInferno said, you use the current lap and last checkpoint visited for broad comparisons. When two vehicles are between the same node-points, you can calculate the following for each vehicle---it gives the fraction of that piece of road covered (0 means you are at the first checkpoint, 1 means you are at the second checkpoint).

public float GetFractionOfPathCovered(Vector3 position, Vector3 lastNodeReached, Vector3 nextNode)
{
    Vector3 displacementFromCurrentNode = position - lastNodeReached;
    Vector3 currentSegmentVector = nextNode - lastNodeReached; 
    float fraction = Vector3.Dot(displacementFromCurrentNode, currentSegmentVector) / 
        currentSegmentVector.sqrMagnitude;

        return fraction;
}

This essentially calculates the length of the projection of the vehicle's position on the line segment between the checkpoints before and after the vehicle. (You can see this article that will explain more on the maths of vectors and projections).

You can compare these values - the vehicle with the larger one is in front.

You can also use this to go from one checkpoint to the next - as soon this value is larger than 1, you know that the vehicle reached the next checkpoint.

A few things to watch out for when you use checkpoints:

  • The method does not work well when the angle between consecutive segments is 90 degrees or smaller. For sharp bends, you need to add a lot of checkpoints to prevent this.
  • For the last reason, you will need a good tool / system to place check-points and set up links between them, especially if you intend to support shortcuts / alternate routes.
  • You need to carefully consider how you will handle players driving backwards (of course, everyone knows that is not part of a racing game, however, it's a very obvious bug when not addressed). Ideally, you will need to to decrease checkpoints and lap counts for vehicles going in reverse. (You can use the above test as well - when the value goes below 0, you know the vehicle is past the last checkpoint in the reverse direction). This significantly complicates the logic and data you need to keep track of for each vehicle.
  • Vehicles driving head to head might sometimes cause the race positions to flicker between two positions. Although this is "correct" in the sense that this value is calculated, it looks like a bug. You might want to use a buffering scheme that only changes the displayed racing position after it has remained consistent for some number of frames or some time interval (but not too long!)

The way we did it for our project is have LOADS of empty game objects as waypoints (that cover the entire width of the track, from boundary to boundary, so cars cannot miss them)… it’s brute-forcing it, but it works a charm. Basically, each car has a counter, as it passes a waypoint, we add one to the counter… so the player with the highest counter at any one time is in the lead and so on. Simple but very effective :wink:

Obviously you’ll need to take into account driving the wrong way down the track etc, but this was the most simple and effective way we could come up with. We did it with a whole bunch of different mathy things too but still found issues when it comes to going around corners etc.

I don't know if this is of any use to you, but a waypoint is, at least on my project, nothing more than another game object dropped into the scene. You just use it's transform.position property as a reference point.

vector3 waypointPosition = GameObject.Find("nameOfYourObjectWorkingAsWaypoint").transform.position;

should give you a vector3 to work with. Or you can just skip the waypointPosition variable and use the whole function, but I'd advice against that, considering it's a paint to read it.

Hi I am thinking a slightly different way to calculate position

  1. .first we will calculate a float
    value say FACTOR

2.Sort them in descending order So Car with heighest FACTOR will be RANK 1,

Calculate FACTOR by following procedure

  1. we will have a LAP POINT with heigh value such that it will be greater than total no of way points in Race. Say LAP POINT is 1000

  2. we will have a DISTANCE to measure total distance from Cars last Crossed way point to next way point.Such that total distace b/w two consecutive way point is always 1

DISTANCE = (distance b/w current WayPoint’s position to car’sPosition )/(distance b/w current WayPoint to next way point)

Now FACTOR = LAP_POINT * no of laps + current way point index + DISTANCE

Please tell what you think about this way

yes, to determine who finishes 1st, 2nd, 3rd.. i mean when in an actual race track, when an AI comes behind you, typically we say that your in first position, when AI runs out in your front, you'll become in 2nd position.. what we will do?

Easiest way would be creating a collider at the finish line and calculate the no. of hits depending on the tag name.

If you have same track and 4 laps in that then the car which touches the collider firstly for the fourth time wins and in the same way you can get all the positions…

thanks mr.xtoxicInferno and sir Herman Tulleken.. But, may i ask how to create a checkpoint? Can you share to me the codes for it?? thanks God BLess! :)