Diference between atan2 pawn vs C versions
#1

Hello,

I was doing some tests when I discovered that atan2 function returns different values

The pawn code:

pawn Код:
stock SetPlayerToFacePlayera(playerid, facingtoid)
{
    new Float:x, Float:y, Float:z, Float:ix, Float:iy, Float:iz, Float:angle;
    GetPlayerPos(facingtoid, x, y, z);
    GetPlayerPos(playerid, ix, iy, iz);
    angle = 180.0-atan2(ix-x,iy-y);
    new string[196];
    format(string,sizeof(string),"!PAWN- Playerid: X: %f, Y: %f, Z: %f | facingID: X: %f, Y: %f, Z: %f Unghiul %f",ix, iy, iz,x, y, z,angle);
    WriteLog(string);
}
And this is the C code:

pawn Код:
PLUGIN_FUNCTION n_SetPlayerFaceToPlayer( AMX* amx, cell* params )
{
  float x, y, z, ix, iy, iz, angle;
  callFunction(&PAWN::GetPlayerPos, params[2], &x, &y, &z);
  callFunction(&PAWN::GetPlayerPos, params[1], &ix, &iy, &iz);
  angle = 180.0-atan2(ix-x,iy-y);
  Log("!PLUGIN- Playerid: X: %f, Y: %f, Z: %f | facingID: X: %f, Y: %f, Z: %f Unghiul %f",ix, iy, iz,x, y, z,angle);
}
This is what I got in debug file:
Код:
[Wed Dec  1 03:33:44 2010] DEBUG: "!PAWN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1539.359008, Y: -1627.401000, Z: 13.103496 Unghiul 47.077163".
[Wed Dec  1 03:33:44 2010] DEBUG: "!PLUGIN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1539.359009, Y: -1627.401001, Z: 13.103497 Unghiul 177.680054".
The coords are correct. They are the same as the ones collected with client's /save command.

Why is angle different ? The value witch is calculated trough pawn, is the value that I would like to be in both cases.

Seems like atan2 doesn't return the same result. It's just a name coincidence ? Or I'm doing smth wrong..

Does anyone have a wiki refference for atan2 - pawn version ?

How to rewrite the C variant of function to gain the same result as pawn does?

Thank you in advance,
Mike.
Reply
#2

yo,
In C: atan2( y, x );
In PAWN: atan2( x, y );
Reply
#3

Thank you for your response, g_aSlice.

I've modified the parameter order of atan2 in the plugin but still results are different:
Код:
[Wed Dec  1 14:29:24 2010] DEBUG: "!PAWN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1539.011840, Y: -1628.687988, Z: 13.102458 Unghiul 58.217597".
[Wed Dec  1 14:29:24 2010] DEBUG: "!PLUGIN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1539.011841, Y: -1628.687988, Z: 13.102459 Unghiul 180.554703".
[Wed Dec  1 14:31:44 2010] DEBUG: "!PAWN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1538.126464, Y: -1630.241821, Z: 13.104832 Unghiul 74.712142".
[Wed Dec  1 14:31:44 2010] DEBUG: "!PLUGIN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1538.126465, Y: -1630.241821, Z: 13.104833 Unghiul 180.266830".
[Wed Dec  1 14:32:00 2010] DEBUG: "!PAWN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1548.454101, Y: -1627.056396, Z: 13.105273 Unghiul 318.704406".
[Wed Dec  1 14:32:00 2010] DEBUG: "!PLUGIN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1548.454102, Y: -1627.056396, Z: 13.105273 Unghiul 182.291534".
Reply
#4

Oh, the C function is outputting radians I'm guessing.

Try:
Код:
angle = 180.0f - ( atan2( iy-y, ix-x ) * ( 3.14159265f / 180.0f ));
Reply
#5

Nop, still not ok.

So, this is what I got now:
pawn Код:
PLUGIN_FUNCTION n_SetPlayerFaceToPlayer( AMX* amx, cell* params )
{
  float x, y, z, ix, iy, iz, angle;
  callFunction(&PAWN::GetPlayerPos, params[2], &x, &y, &z);
  callFunction(&PAWN::GetPlayerPos, params[1], &ix, &iy, &iz);
  //angle = 180.0-atan2(iy-y,ix-x);
  angle = 180.0f - ( atan2( iy-y, ix-x ) * ( 3.14159265f / 180.0f ));
  Log("DEBUG: \"!PLUGIN- Playerid: X: %f, Y: %f, Z: %f | facingID: X: %f, Y: %f, Z: %f Unghiul %f\".",ix, iy, iz,x, y, z,angle);
}
Output:
Код:
[Wed Dec  1 14:39:53 2010] DEBUG: "!PAWN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1540.246215, Y: -1627.771362, Z: 13.098144 Unghiul 43.737625".
[Wed Dec  1 14:39:53 2010] DEBUG: "!PLUGIN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1540.246216, Y: -1627.771362, Z: 13.098145 Unghiul 180.014099".
[Wed Dec  1 14:40:12 2010] DEBUG: "!PAWN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1548.842773, Y: -1628.033325, Z: 13.102981 Unghiul 309.788970".
[Wed Dec  1 14:40:12 2010] DEBUG: "!PLUGIN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1548.842773, Y: -1628.033325, Z: 13.102982 Unghiul 180.042709".
Reply
#6

Sorry,
pawn Код:
angle = 180.0f - ( atan2( iy-y, ix-x ) * ( 180.0f / 3.14159265f ) );
Reply
#7

Still different results:

Код:
[Wed Dec  1 14:49:05 2010] DEBUG: "!PAWN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1548.151367, Y: -1627.234130, Z: 13.104700 Unghiul 319.736328".
[Wed Dec  1 14:49:05 2010] DEBUG: "!PLUGIN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1548.151367, Y: -1627.234131, Z: 13.104700 Unghiul 310.263672".
[Wed Dec  1 14:49:34 2010] DEBUG: "!PAWN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1548.151367, Y: -1627.234130, Z: 13.104700 Unghiul 319.736328".
[Wed Dec  1 14:49:34 2010] DEBUG: "!PLUGIN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1548.151367, Y: -1627.234131, Z: 13.104700 Unghiul 310.263672".
[Wed Dec  1 14:49:49 2010] DEBUG: "!PAWN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1540.595581, Y: -1628.579833, Z: 13.103104 Unghiul 47.323928".
[Wed Dec  1 14:49:49 2010] DEBUG: "!PLUGIN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1540.595581, Y: -1628.579834, Z: 13.103105 Unghiul 222.676071".
[Wed Dec  1 14:51:04 2010] DEBUG: "!PAWN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1541.014892, Y: -1634.031372, Z: 13.327032 Unghiul 123.785278".
[Wed Dec  1 14:51:04 2010] DEBUG: "!PLUGIN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1541.014893, Y: -1634.031372, Z: 13.327032 Unghiul 146.214722".
Reply
#8

I'm not sure what it is, really. Hopefully ****** will spot this topic and save the day.
Reply
#9

That's ok. I really apreciate your support.

I just made some tests in trying to rewrite some of my gamemode using C. If I won't find a sollution I'll leave it in pawn, if there's no other choice.

Once again g_aSlice, Thank you.
Reply
#10

Nop, still different results using
pawn Код:
angle = 90.0f - ( atan2( y-iy, x-ix ) * ( 180.0f / 3.1415f ) );
Код:
[Wed Dec  1 15:49:58 2010] DEBUG: "!PAWN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1540.967651, Y: -1628.268066, Z: 13.103264 Unghiul 41.641311".
[Wed Dec  1 15:49:58 2010] DEBUG: "!PLUGIN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1540.967651, Y: -1628.268066, Z: 13.103265 Unghiul -41.645187".
[Wed Dec  1 15:50:12 2010] DEBUG: "!PAWN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1545.893432, Y: -1627.286132, Z: 13.092100 Unghiul 339.832031".
[Wed Dec  1 15:50:12 2010] DEBUG: "!PLUGIN- Playerid: X: 1544.198242, Y: -1631.901489, Z: 13.382812 | facingID: X: 1545.893433, Y: -1627.286133, Z: 13.092100 Unghiul 20.165895".
Quote:
Originally Posted by ******
Посмотреть сообщение
However I would be VERY careful with rewriting your mode in C, especially if you have a lot of calls to native functions, because there is no direct access so there are very large overheads involved.
Thank you both for your advices. I'll make some speedtests and see if it worth converting. Probably I'll just rewrite custom functions without samp native functions calls.

Have a nice day,
Mike.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)