how can i get the nearest player from point?
#5

pawn Код:
// Returns the nearest player to point x/y/z
// ignoreplayer will be ignored, if set
// (useful when you want to find the closest player to another player)
stock GetNearestPlayer(Float:x, Float:y, Float:z, ignoreplayer=-1) {
    // Store the currently nearest player and currently shortest distance in these:
    new Float:dist=9999999.0, player = -1;
    new Float:px, Float:py, Float:pz;    // Position of the currently looped player
    new Float:curdist;
    // Loop through all players
    for (new i = 0; i < MAX_PLAYERS; i++) {
        // Skip not connected players, and the player to ignore, if set
        if (!IsPlayerConnected(i) || i == ignoreplayer) continue;
        // Calculate quadratic distance from x/y/z to player
        GetPlayerPos(i, px, py, pz);
        curdist = (px - x) * (px - x) + (py - y) * (py - y) + (pz - z) * (pz - z);
        // if it is closer than the current minimum, update it
        if (curdist < dist) {
            dist = curdist;
            player = i;
        }
    }
    return player;
}
This will work, it uses the quadratic distance, and so is faster without any square roots, but this makes no difference for the closest player.

You could also have searched btw.
Reply


Messages In This Thread
how can i get the nearest player from point? - by Yoseflm - 26.10.2012, 11:47
Re: how can i get the nearest player from point? - by _Khaled_ - 26.10.2012, 12:05
Re: how can i get the nearest player from point? - by |zEd - 26.10.2012, 12:16
Re: how can i get the nearest player from point? - by Yoseflm - 26.10.2012, 12:17
Re: how can i get the nearest player from point? - by Mauzen - 26.10.2012, 12:27
Re: how can i get the nearest player from point? - by Yoseflm - 26.10.2012, 12:37
Re: how can i get the nearest player from point? - by Yoseflm - 26.10.2012, 12:54
Re: how can i get the nearest player from point? - by Mauzen - 26.10.2012, 13:48

Forum Jump:


Users browsing this thread: 4 Guest(s)