Distance
#1

Hey,

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

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.
Reply
#3

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
Reply
#4

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
Reply
#5

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!");
}
{
Reply
#6

@ricardo
20.0 lol thats too far
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)