Hi guys,
I’m making a car game but I haven’t any ideas about how can I determinate the position of the cars.
Who is the first, who’s the second? …
Is there any one can help me please?
thanks, Nik
Hi guys,
I’m making a car game but I haven’t any ideas about how can I determinate the position of the cars.
Who is the first, who’s the second? …
Is there any one can help me please?
thanks, Nik
On turns, this is harder. On a straight piece of track, you only have to measure from a checkpoint at the end to the front of the cars (or even centers by transform if they are close enough in size). The only time it really matters is at the finish line in which case you record the time of each collider hitting the finish line.
Have I to compare the time of each car ?
To compare the finish time of each car, you need a listing of the cars or a member variable for each car. When it crosses a checkpoint, capture that time in either your collection or on the car. Use the built-in Time class, but make sure you start the timer when the race starts and not when the level itself loads.
Ok:), but in the checkpoints: I’ll use OnTriggerEnter obviously, but I can’t understand how can I check the position of my car. It’s such as:
if(mytimer>AI1timer)
{ i’m first }
else{
if(mytimer>AI2timer)
{ I’m second }
} bla bla bla
…
???
The position has nothing to do with time, unless you use a ton of checkpoints, which is impractical. You’d have to periodically, not necessarily every frame, measure the distance from your vehicle to the checkpoint with the Vector3.distance. Remember that other than the checkpoints it is not a huge deal if there is some discrepancy as people won’t even notice it if they are focused on their own car.
Let’s say every second you have a scoreboard option update positions, its goal would be to check each segment of the track and find the car furthest along. If your track had 7 segments, you could start at the end point of 7 and then check it by finding the distance for the each car, then go to segment 6, then 5. That would populate the position rankings quite handily, but you would still need to check the lap number. If the car is on segment 1, but lap 3, it should be in front of a car in segment 7 on lap 2.
There are likely to be other ways to check these positions for the leaderboard, but I think my suggestion would work. I doubt it is the best.
What I’d try first for something like this would be to project the position of each car onto the medial spline of the track, and then compare the positions of the cars in parametric space. It wouldn’t necessarily be trivial to implement, but it would give you exact placing, rather than having to rely on checkpoints or other approximations.
But, something simple like checkpoints might work as well.
How do you can compare the positions of the cars in parametric space??
It’s not entirely trivial, but the basic idea is this:
One way to think of a curve is in terms of a single value (the parameter) that tells you how far along that curve you are. Mathematically, we’re talking about a function, e.g. P(t), that given a scalar value (the parameter t in this case) returns a point in space.
There are various types of curves that can be used (Bezier, etc.), although working with them in parametric space can get a bit involved.
An alternative would be simply to represent the path of the track (basically a curving line running along its center) as a series of line segments. You can use long segments for the straight portions, and then shorter segments to approximate the curves.
Finding the closest point on a line segment to a point is straightforward. Finding the closest point on a series of line segments to a point can be implemented simply as a series of closest-point-on-segment tests (that is, test every segment and take the closest point found), or it can be accelerated using some sort of broad-phase system if performance is a concern.
Once you have this closest point, you’ll then want to determine how far along the entire curve the point is, e.g. as a percentage. This is the number that will tell you where you are with respect to the track.
Once you know which segment the point falls on and where the point is, computing this value is fairly straightforward. The track has a total length that is the sum of the lengths of all the line segments. The distance from the start of the track to the point in question is the sum of the lengths of all segments prior to the segment on which the point falls, plus the distance from the starting point of that segment to the closest point. This number then basically tells you how far along the track you are (if you want a percentage, you can divide the value by the total length of the track).
All in all, it’s not that involved; to get up and running, all you really need is a series of line segments approximating your track, and a little math.