SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Help Archive

 
 
Thread Tools Display Modes
Old 01/12/2010, 12:29 AM   #1
xxmitsu
Gangsta
 
Join Date: Feb 2009
Posts: 622
Reputation: 36
Question Diference between atan2 pawn vs C versions

Hello,

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

The pawn code:

pawn Code:
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 Code:
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:
Code:
[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.

Last edited by xxmitsu; 01/12/2010 at 12:40 AM.
xxmitsu is offline  
Old 01/12/2010, 10:46 AM   #2
Slice
High-roller
 
Join Date: Mar 2008
Posts: 1,831
Reputation: 1607
Default Re: Diference between atan2 pawn vs C versions

yo,
In C: atan2( y, x );
In PAWN: atan2( x, y );
Slice is offline  
Old 01/12/2010, 11:22 AM   #3
xxmitsu
Gangsta
 
Join Date: Feb 2009
Posts: 622
Reputation: 36
Default Re: Diference between atan2 pawn vs C versions

Thank you for your response, g_aSlice.

I've modified the parameter order of atan2 in the plugin but still results are different:
Code:
[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".
xxmitsu is offline  
Old 01/12/2010, 11:25 AM   #4
Slice
High-roller
 
Join Date: Mar 2008
Posts: 1,831
Reputation: 1607
Default Re: Diference between atan2 pawn vs C versions

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

Try:
Code:
angle = 180.0f - ( atan2( iy-y, ix-x ) * ( 3.14159265f / 180.0f ));
Slice is offline  
Old 01/12/2010, 11:31 AM   #5
xxmitsu
Gangsta
 
Join Date: Feb 2009
Posts: 622
Reputation: 36
Default Re: Diference between atan2 pawn vs C versions

Nop, still not ok.

So, this is what I got now:
pawn Code:
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:
Code:
[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".
xxmitsu is offline  
Old 01/12/2010, 11:35 AM   #6
Slice
High-roller
 
Join Date: Mar 2008
Posts: 1,831
Reputation: 1607
Default Re: Diference between atan2 pawn vs C versions

Sorry,
pawn Code:
angle = 180.0f - ( atan2( iy-y, ix-x ) * ( 180.0f / 3.14159265f ) );
Slice is offline  
Old 01/12/2010, 11:39 AM   #7
xxmitsu
Gangsta
 
Join Date: Feb 2009
Posts: 622
Reputation: 36
Default Re: Diference between atan2 pawn vs C versions

Still different results:

Code:
[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".
xxmitsu is offline  
Old 01/12/2010, 11:50 AM   #8
Slice
High-roller
 
Join Date: Mar 2008
Posts: 1,831
Reputation: 1607
Default Re: Diference between atan2 pawn vs C versions

I'm not sure what it is, really. Hopefully ****** will spot this topic and save the day.
Slice is offline  
Old 01/12/2010, 11:53 AM   #9
xxmitsu
Gangsta
 
Join Date: Feb 2009
Posts: 622
Reputation: 36
Default Re: Diference between atan2 pawn vs C versions

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.
xxmitsu is offline  
Old 01/12/2010, 12:43 PM   #10
xxmitsu
Gangsta
 
Join Date: Feb 2009
Posts: 622
Reputation: 36
Default Re: Diference between atan2 pawn vs C versions

Nop, still different results using
pawn Code:
angle = 90.0f - ( atan2( y-iy, x-ix ) * ( 180.0f / 3.1415f ) );
Code:
[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 ****** View Post
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.
xxmitsu is offline  
 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
whats the diference dark_clown Help Archive 2 27/10/2010 02:01 AM
More versions????? Steven82 Help Archive 8 10/04/2010 04:30 PM
asin. acos, atan, atan2 Kovacs_Richard Help Archive 1 17/11/2009 07:55 AM
asin, acos, atan, atan2 funcs? xomka Help Archive 1 28/10/2009 08:35 AM
about sa-mp versions Sfinx_17 General 1 17/10/2009 07:28 PM


All times are GMT. The time now is 02:16 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.