Recommendations on how to find players closest to finish line?

Hi, so this is for a obby type of game where players race on foot through obstacles and such and get to the finish line.

For one map I’m coding, it’s a huge mountain. Players start at the bottom and have to work their way up.

Now, I thought originally about using magnitude to get the players that are in 1st, 2nd, 3rd, and so on. Although, I feel this could yield inaccurate results depending where players are in the map, even if they are closer to finishing than the other players.

Just was wondering about some other strategies or ideas that I could use to tackle this. Thanks!

you could make invisible checkpoint triggers. at the end of the round calculate positions based on the last trigger a player touched. and if multiple players last touched the same trigger, you can use magnitude to determine the players distance from that trigger, and sort placement accordingly.

2 Likes

If they go up - then rank them on their height

1 Like

There was a module that lets you do that by @Zomebody. Take a look into the module, you’ll most probably find some useful/new information there. Or if you don’t want to implement someone else’s module you could divide your path into several points, then calculate the distance between the points and the player, and mark it accordingly.

3 Likes

First, you are going to want to come up with some list of points that represent your path. You can manually generate these points along the path or automate this. The points should be spaced close enough to each other it approximates the path relatively well. Now, we generate a set of splines which represent our entire path. You can iterate through each point (n) and select point n-1,n+1 to create a cubic spline from n-1,n,n+1. If your game isn’t too hilly, just treat the problem in 2D. Lots of algorithms exist for cubic-spline interpolation.

For 3 points, you can literally just solve for k0,k1,k2 via

With k solved for

You then can obtain

These spline segments will represent your path.

Next, we need to find the a point on the spline that is closest to a player. To do this, we can use a orthogonal projection onto the cubic spline. Academic research has been done on this topic. I will be using the paper Robust and Efficient Computation of the Closest Point on a Spline Curve (Wang, Kearney, Atikson 2002). Coincidentally, this paper is focused on roads modeled by splines as well!

First, we define L as the spline curve arc length and x(s),y(s),z(s) as the cubic spline functions.

We define D(s) as the square of the distance between p0 (our player point) and the spline curve point.

Our problem now is to calculate s* which minimizes D(s). The point on the spline curve can be referred to as p1. The vector formed by p0 and p1 must be normal to the cubic spline.

In the paper, a Newton’s method and Quadratic Minimization are used jointly to calculate a approximate solution. You can probably just use Newton’s method alone in order to achieve necessary game-performance. The formula obtained is just:

After you obtain the spline point, you can then compare value s to evaluate which player is further along the spline. This should give you a very, very accurate measure of track progression.

7 Likes

Use the X,Y, and Z positions of the players to make pre-set checkpoint.

For example, if a player’s x y and z values are in between one number and another (the numbers being chosen by you while scripting) then the game will know they are farther then one player.

You could also make a giant 3-D grid with a bunch of little ordered boxes. Higher the box number, higher the score.

1 Like