[help] i can't fix this so it also works for negative values
#1

I was making a function to set the player's facing angle to a point(x and y) on the map
it's like SetCameraLookAt but for the player and only x and y coord.

this is what i already had:
pawn Код:
stock SetPlayerLookAt(playerid, Float:x, Float:y)
{
    new Float:Px, Float:Py, Float: Pa;
    GetPlayerPos(playerid, Px, Py, Pa);
    Pa = atan(floatdiv(floatsub(y, Py), floatsub(x, Px))); //pythagoras -> this is the code that doesn't work when i use negative floats

    new Float:Par = floatadd((floatsub(Pa, (floatmul(Pa, 2)))), 90); //calculating the angle in gta from the angle i got with pythagoras
(see [url]http://forum.sa-mp.com/index.php?topic=89950.0[/url]) this code works good

    SetPlayerFacingAngle(playerid, Par);

    new text[64];
    format(text, sizeof text, "real angle = %f | gta angle = %f", Pa, Par);
    SendClientMessage(playerid, 0xFF3399AA, text); //just debugging
}
it only works if the X and Y are positive but when they're negative it sets the wrong facingangle
Reply
#2

please check this topic http://forum.sa-mp.com/index.php?topic=45996.0
Reply
#3

Quote:
Originally Posted by [LDT
LuxurY ]
please check this topic http://forum.sa-mp.com/index.php?topic=45996.0
Quote:

stock Float:AngleToPoint(Float,Float:y,Float1,Float: y1)
{
new Float:newang = atan(floatabs((x-x1)/(y-y1)));
if (x1 <= x && y1 >= y) newang += 90.0;
else if (x1 < x && y1 < y) newang += 180.0;
else if (x1 >= x && y1 <= y) newang += 270.0;
if (newang >= 360.0) newang = 0.0;
return newang;
}

so i was on my right way
does this code works?
Reply
#4

Quote:
Originally Posted by °ғαιιοцт°
Quote:
Originally Posted by [LDT
LuxurY ]
please check this topic http://forum.sa-mp.com/index.php?topic=45996.0
Quote:

stock Float:AngleToPoint(Float,Float:y,Float1,Float: y1)
{
new Float:newang = atan(floatabs((x-x1)/(y-y1)));
if (x1 <= x && y1 >= y) newang += 90.0;
else if (x1 < x && y1 < y) newang += 180.0;
else if (x1 >= x && y1 <= y) newang += 270.0;
if (newang >= 360.0) newang = 0.0;
return newang;
}

so i was on my right way
does this code works?
certainly
Reply
#5

i get this error when adding the code:
Код:
warning 208: function with tag result used before definition, forcing reparse
line = stock Float:AngleToPoint(Float,Float:y,Float1,Float: y1)
Reply
#6

Straight from pawn-lang.pdf:
Quote:
Originally Posted by pawn-lang.pdf
function with tag result used before definition, forcing
User-defined oper-
ators: 86
Forward declara-
tion: 82
reparse
When a function is “used” (invoked) before being declared, and
that function returns a value with a tag name, the parser must
make an extra pass over the source code, because the presence
of the tag name may change the interpretation of operators (in
the presence of user-defined operators). You can speed up the
parsing/compilation process by declaring the relevant functions
before using them.
Reply
#7

Quote:
Originally Posted by Marcel
Straight from pawn-lang.pdf:
Quote:
Originally Posted by pawn-lang.pdf
function with tag result used before definition, forcing
User-defined oper-
ators: 86
Forward declara-
tion: 82
reparse
When a function is “used” (invoked) before being declared, and
that function returns a value with a tag name, the parser must
make an extra pass over the source code, because the presence
of the tag name may change the interpretation of operators (in
the presence of user-defined operators). You can speed up the
parsing/compilation process by declaring the relevant functions
before using them.
thanks

@ [LDT]LuxurY:

The code is ready now
Код:
stock SetPlayerLookAt(playerid, Float:x, Float:y)
{
	new Float:Px, Float:Py, Float: Pa;
	GetPlayerPos(playerid, Px, Py, Pa);
	Pa = floatabs(atan((y-Py)/(x-Px)));
	if (x <= Px && y >= Py) Pa = floatsub(180, Pa);
	else if (x < Px && y < Py) Pa = floatadd(Pa, 180);
	else if (x >= Px && y <= Py) Pa = floatsub(360.0, Pa);
	Pa = floatsub(Pa, 90.0);
	if (Pa >= 360.0) Pa = floatsub(Pa, 360.0);
	SetPlayerFacingAngle(playerid, Pa);
}
ps i had to do
Pa = floatadd(Pa, 90.0);
you forgot that
it's because the angles in gta are 90° less (it's 90° rotated)
and why did you do
if (Pa >= 360.0) Pa = 0.0;
?
wtf if the value is higher than 360 it'll become 0°



another thing:
wiki is a little wrong here https://sampwiki.blast.hk/wiki/SetPlayerFacingAngle
it says:
Quote:

north (0)
|
(270)west- -east (90) (Good way to remember: Never Eat Shredded Wheat)
|
south (180)

but it should be:
Quote:

north (0)
|
(90)west- -east (270) (Good way to remember: Never Eat Shredded Wheat)
|
south (180)

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)