person who's closest to checkpoint
#1

I need a function that calculates who's closest to the checkpoint.

So when id 12 is closest to the checkpoint it will say to him, you arre currently on the lead!
otherwise it will say your are not on the lead. I hope somebody can help me!!
Reply
#2

You should use a function to get the distance between two coords.
Here's a simple one using the Pythagorean theorem:
pawn Код:
stock GetDistanceBetweenCoords(Float:x1, Float:y1, Float:x2, Float:y2)
{
    return floatsqroot(floatpower(floatabs(floatsub(x2, x1)), 2) + floatpower(floatabs(floatsub(y2, y1)), 2));
}
Reply
#3

I don't understand this code really i hope somebody can give me a example or full code so i can be learning from it i not really good in math in real life so i hope anybody can help me
Reply
#4

You don't really need to understand the maths in the GetDistanceBetweenCoords method itself, you just need
to know how to use the function. It does as it says - gets the distance between two points.

It has four parameters:
x1 and y1 are the X and Y coordinates of one point (in your case the player)
x2 and y2 are the X and Y coordinates of another point (in your case the checkpoint)

Here's an example of how you might use the function:
pawn Код:
new leaderid, Float:shortestdistance=999999, Float:x, Float:y, Float:z;
for(new i=0; i<MAX_PLAYERS; i++) // loops through all players
{
    if(IsPlayerConnected(i)) // checks the current player being checked is connected
    {
        GetPlayerPos(i, x, y, z); // gets the current players x, y and z coords and stores them in the variables x, y and z
        if(GetDistanceBetweenCoords(x, y, CHECKPOINT_X, CHECKPOINT_Y) < shortestdistance) // if the player is closer to the checkpoint than the current shortest distance of another player, continue
        {
            shortestdistance = GetDistanceBetweenCoords(x, y, CHECKPOINT_X, CHECKPOINT_Y); // set the new shortest distance
            leaderid= i; // set the playerid of the new closest player
        }
    }
}
leaderid would be the playerid of the player closest to the checkpoint.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)