SA-MP Forums Archive
Distance - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Distance (/showthread.php?tid=241174)



Distance - GiS - 17.03.2011

Hey,

how can I check the distance of a person to another person? Is there something like GetPlayerToPlayerDistance??


Re: Distance - Mike Garber - 17.03.2011

pawn Код:
forward GetPlayerDistanceToPoint(playerid,Float:x,Float:y);
public GetPlayerDistanceToPoint(playerid,Float:x,Float:y)
{
    new Float:x1,Float:y1,Float:z1;
    new Float:tmpdis;
    GetPlayerPos(playerid,x1,y1,z1);
    tmpdis = floatsqroot(floatpower(floatabs(floatsub(x,x1)),2)+floatpower(floatabs(floatsub(y,y1)),2));
    return floatround(tmpdis);
}
pawn Код:
new Float:X,Float:Y,Float:Z;
GetPlayerPos(playerid,X,Y,Z);
GetPlayerDistanceToPoint(OTHERID,X,Y);
If there's a faster/better method tell me, I'd like to use that to have my script as efficient as possible.


Re: Distance - Mauzen - 17.03.2011

Well, that function indeed can be optimized a bit

pawn Код:
forward Float:GetPlayerDistanceToPoint(playerid,Float:x,Float:y);
public Float:GetPlayerDistanceToPoint(playerid,Float:x,Float:y)     // Make it return a Float safes a flaotround and a variable
// Well, depends on if you can use it like that or if you need it as int
{
    new Float:x1,Float:y1,Float:z1;
    GetPlayerPos(playerid,x1,y1,z1);
    return floatsqroot(floatsub(x,x1) * floatsub(x,x1)) + floatsub(y,y1) * floatsub(y,y1)); // You dont need floatabs, as squared numbers are always positive
    // Also do not use floatpower, but just *, because floatpower is extremely slow
}
Should be at least 100% faster now


Re: Distance - AH.1990 - 17.03.2011

you want any player near certain player?? or a certain player near a certain player?? i will give you both
certain player near certain player
pawn Код:
stock IsPlayerNearPlayer(playerid, otherplayerid)//This Will check if "playerid" is near "otherplayerid"
{
    new Float:X, Float:Y, Float:Z;
    GetPlayerPos(otherplayerid, X, Y, Z);
    if(IsPlayerInRangeOfPoint(playerid, 3.0, X, Y, Z)) // 3.0 is the distance from otherplayerid 3.0 is like 2-3 steps away from otherplayerid you can change 3.0 to any thing you want
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
you can now easy use it here is an example(cmd using ZCMD and SSCANF):
pawn Код:
CMD:checkdistance(playerid, params[])
{
       new checkingplayerid
       if(sscanf(params, "u", checkingplayerid))
       {
              SendClientMessage(playerid, 0xC0C0C0AA, "Usage:/checkdistance <playerid>");
       }
       else
       {
               if(IsPlayerNearPlayer(playerid, checkingplayerid))
               {
                       SendClientMessage(playerid, 0xFFFFFFAA, "You Are Near That Player");
               }
        }
        return 1;
}
easy huh??

now checking if is any player near certain player
pawn Код:
stock IsAnyPlayerNearPlayer(playerid)
{
        new Float:X, Float:Y, Float:Z;
        GetPlayerPos(playerid, X, Y, Z);
        for(new i = 0; i<MAX_PLAYERS; i++)
        {
               if(IsPlayerConnected(i))
               {
                     if(IsPlayerInRangePoint(i, 3.0, X, Y, Z)) // again you can increase 3.0
                     {
                             return 1;
                     }
                     else
                     {
                             return 0;
                     }
                }
          }
}
you can use it same way as IsPlayerNearPlayer if you need more help post and i will help you


Re: Distance - ricardo178 - 17.03.2011

pawn Код:
{                        
    new Float:X, Float:Y, Float:Z;
    GetPlayerPos(playerid, X,Y,Z);
    if(IsPlayerInRangeOfPoint(toplayer, 20.0, X,Y,Z))
    {

    }
    else return SendClientMessage(playerid 0x0259EAAA, "The player isn't close you!");
}
{


Re: Distance - AH.1990 - 17.03.2011

@ricardo
20.0 lol thats too far