distance
#1

can anyone give me a function to get the distance from a player to another player?
when a player killed another player, it should display the distance between the killer and the victim.
Reply
#2

pawn Code:
stock GetDistanceBetweenPlayers(playerid, giveplayerid)
{
    new Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2;
    GetPlayerPos(playerid, x1, y1, z1);
    GetPlayerPos(giveplayerid, x2, y2, z2);
    return floatround(floatsqroot(floatpower(floatabs(floatsub(x2,x1)), 2) + floatpower(floatabs(floatsub(y2,y1)),2) + floatpower(floatabs(floatsub(z2,z1)) ,2)));
}
pawn Code:
public OnPlayerDeath(playerid, killerid, reason[])
{
    new distance = GetDistanceBetweenPlayers(playerid, killerid);
    new string[128];
    format(string, sizeof(string), "* Distance between you and a killer is: %dm.", distance);
    SendClientMessage(playerid, -1, string);
    // Set here timer and change it to GameTextForPlayer or what ever you wan't.
    return true;
}
Reply
#3

thanks it worked =)
Reply
#4

i tired it to add %.2f insted of %d and didn't worked.
so it showed 0.00m.
Reply
#5

What should %.2f represent?
Reply
#6

the exact number of metres, like 30.32 meters
Reply
#7

Nver heard of something like that.

PHP Code:
Format Strings
Placeholder     Meaning
%b     Inserts a number at this position in binary radix
%c     Inserts a single character.
%
d     Inserts an integer (wholenumber
%f     Inserts a floating point number.
%
i     Inserts an integer.
%
s     Inserts a string.
%
x     Inserts a number in hexadecimal notation.
%%     
Inserts the literal '%' 
You probably need %f not %.2f
Reply
#8

ah okay,the %.2f means that there are 2 numbers after the comma.
and also tried %f,result was 0.00000000...
Reply
#9

Quote:
Originally Posted by [MG]Dimi
View Post
Nver heard of something like that.
Inform yourself better.

As to the problem: That function GetDistanceBetweenPlayers (which is really awfully coded) returns an integer variable, not a float. In order to return a float from that function, you need to remove the floatround() in it. This function will return a float value and is coded a lot cleaner:

pawn Code:
stock Float:GetDistanceBetweenPlayers(p1, p2)
{
    if(!IsPlayerConnected(p1) || !IsPlayerConnected(p2))
        return FLOAT_NAN;

    new
        Float:x1,
        Float:y1,
        Float:z1,
        Float:x2,
        Float:y2,
        Float:z2;

    GetPlayerPos(p1,x1,y1,z1);
    GetPlayerPos(p2,x2,y2,z2);
   
    return GetDistanceBetweenPoints(x1, y1, z1, x2, y2, z2);
}

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));
}
Reply
#10

how the code could be done better?
Reply
#11

Are you sure someone is actually killing you? coz
PHP код:
SetPlayerHealth(playerid,0); 
won't work
Reply
#12

thanks for the code, but i got these errors:

warning 208: function with tag result used before definition, forcing reparse (line: 20456)
warning 208: function with tag result used before definition, forcing reparse (line: 20475)
error 017: undefined symbol "FLOAT_NAN" (line: 20459)
warning 213: tag mismatch (line: 20459)

my code:

pawn Код:
new Float:Distance;
        Distance = GetDistanceBetweenPlayers(killerid,playerid);
        format(string,sizeof(string),"Distance: %.2f",Distance);

pawn Код:
stock Float:GetDistanceBetweenPlayers(p1, p2) // line 20456
{
    if(!IsPlayerConnected(p1) || !IsPlayerConnected(p2))
        return FLOAT_NAN; // line 20459

    new
        Float:x1,
        Float:y1,
        Float:z1,
        Float:x2,
        Float:y2,
        Float:z2;

    GetPlayerPos(p1,x1,y1,z1);
    GetPlayerPos(p2,x2,y2,z2);

    return GetDistanceBetweenPoints(x1, y1, z1, x2, y2, z2);
}

stock Float:GetDistanceBetweenPoints(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2) //line 20475
{
    x1 -= x2;
    y1 -= y2;
    z1 -= z2;
    return floatsqroot((x1 * x1) + (y1 * y1) + (z1 * z1));
}
Reply
#13

Put these things on top:

pawn Код:
#define FLOAT_NAN           (Float:0xFFFFFFFF)

forward Float:GetDistanceBetweenPlayers(p1, p2);
forward Float:GetDistanceBetweenPoints(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2);
Reply
#14

ah thanks man works now! =)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)