Function such as "IsPlayerCloseToPlayer" or something
#1

Hey

Is there a function that can detect if a player is within a certain distance to another player? I looked all over the SA-MP Wiki but couldn't find one?

Thanks!
Reply
#2

It's impossible you've looked all over the SAMP wiki because there's something which can help you : IsPlayerInRangeOfPoint.

Just get the player2 pos and check if it's close enough through the "radius" parameter to the playerid's position (gotten automatically by SAMP)
Reply
#3

pawn Код:
stock Float:GetDistanceBetweenPlayers(playerid,targetplayerid)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    if(!IsPlayerConnected(playerid) || !IsPlayerConnected(targetplayerid)) {
        return -1.00;
    }
    GetPlayerPos(playerid,x1,y1,z1);
    GetPlayerPos(targetplayerid,x2,y2,z2);
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}
P.S : by Kostantinos
Reply
#4

Quote:
Originally Posted by S4t3K
Посмотреть сообщение
It's impossible you've looked all over the SAMP wiki because there's something which can help you : IsPlayerInRangeOfPoint.

Just get the player2 pos and check if it's close enough through the "radius" parameter to the playerid's position (gotten automatically by SAMP)
Quote:
Originally Posted by Clad
Посмотреть сообщение
pawn Код:
stock Float:GetDistanceBetweenPlayers(playerid,targetplayerid)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    if(!IsPlayerConnected(playerid) || !IsPlayerConnected(targetplayerid)) {
        return -1.00;
    }
    GetPlayerPos(playerid,x1,y1,z1);
    GetPlayerPos(targetplayerid,x2,y2,z2);
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}
P.S : by Kostantinos
This is exactly what I wanted! Thank you. And S4t3K, I saw that but I didn't think that would be useful. Thanks anyways!

EDIT: Now for the GetDistanceBetweenPlayers function, would I do:
Код:
if(GetDistanceBetweenPlayers(playerid, targetid)) = x (x would be a number or a float)
Reply
#5

Konstantinos' function can be optimized using VectorSize :

PHP код:

stock Float
:GetDistanceBetweenPlayers(pl1pl2)
{
      new 
Float:pos[6];
      if(!
IsPlayerConnected(p1) || !IsPlayerConnected(p2)) return -1.0;
      
GetPlayerPos(pl1pos[0], pos[1], pos[2]);
      
GetPlayerPos(pl2pos[3], pos[4], pos[5]);
      return 
VectorSize(pos[0]-pos[3],pos[1] - pos[4], pos[2]-pos[5]);

This "VectorSize" usage is from the wiki page VectorSize, and a similar function called "GetDistanceBetweenPoints" uses this kind of implementation, credits go to Vince.
Reply
#6

pawn Код:
forward Float:GetDistanceBetweenPlayers(playerid,targetplayerid);

public Float:GetDistanceBetweenPlayers(playerid,targetplayerid)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    if(!IsPlayerConnected(playerid) || !IsPlayerConnected(targetplayerid)) {
        return -1.00;
    }
    GetPlayerPos(playerid,x1,y1,z1);
    GetPlayerPos(targetplayerid,x2,y2,z2);
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}
Reply
#7

Why do you want to use a public function here ? Except if you plan to call the function through a timer or through CallLocal/RemoteFunction, it's pointless.

And why do you repost the exact function as above ?
Reply
#8

Late.
Reply
#9

?
Quote:

EDIT: Now for the GetDistanceBetweenPlayers function, would I do:
Code:

if(GetDistanceBetweenPlayers(playerid, targetid)) == x (x would be a number or a float)
I don't understand what you are trying to say by just giving me the stock?
Reply
#10

Quote:
Originally Posted by S4t3K
Посмотреть сообщение
Why do you want to use a public function here ? Except if you plan to call the function through a timer or through CallLocal/RemoteFunction, it's pointless.

And why do you repost the exact function as above ?
Don't get mad homie, When I sent it I didn't know you will send it too, Relax pls.
Reply


Forum Jump:


Users browsing this thread: