SA-MP Forums Archive
Calculating 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)
+--- Thread: Calculating distance (/showthread.php?tid=383118)



Calculating distance - printer - 06.10.2012

Hello, how can I calculate distance between two players? (in meters)

I'd appreciate if someone can make a function for me


Re: Calculating distance - Dolby - 06.10.2012

pawn Код:
stock GetDistanceBetweenPlayers(playerid,playerid2)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    new Float:tmpdis;
    GetPlayerPos(playerid,x1,y1,z1);
    GetPlayerPos(playerid2,x2,y2,z2);
    tmpdis = floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
    return floatround(tmpdis);
}
Credits: Unknow


Re: Calculating distance - Vince - 06.10.2012

Better version:
pawn Код:
stock Float:GetDistanceBetweenPoints(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2)
{
        x1 -= x2;
    y1 -= y2;
    z1 -= z2;
    return floatsqroot((x1 * x1) + (y1 * y1) + (z1 * z1));
}



Re: Calculating distance - RedJohn - 06.10.2012

Offtopic: Vince, LOL at your post in my sig.


AW: Calculating distance - Nero_3D - 06.10.2012

In most cases you only need (they are both native since 0.3a)

GetPlayerDistanceFromPoint or
GetVehicleDistanceFromPoint

in all other cases use Vince code