Angles according to player positions
#1

The title is not self explanatory, but I didn't know what to say.

Well basically I'm trying to do a system that will show on the screen a red ball the direction of the guy that hit you. Though since I've had the idea I've been having problems with it, first at understanding how to do it and then in making it work, but let's cut the bullshit and go straight to the point.

I've made in the script to show the player info's about the shot and the coordinates on the screen to understand better what was going on and I got into some weird conclusions, the py and px does not follow some "rules" I've added:



pawn Код:
new Float:x, Float:y, Float:z, Float:x2, Float:y2, Float:z2, Float:AD, Float:angle, Float:px, Float:py, mess[280];
    GetPlayerPos(playerid, x, y, z);
    GetPlayerPos(damagedid, x2, y2, z2);
    angle = atan2(x2-x,y2-y);
    GetPlayerFacingAngle(damagedid, AD);
    angle += AD;
    if(angle >= 360)
        angle -= 360.0;
    if(angle >= 90.0 && angle < 180.0)
    {
        angle -= 90.0;
        py = -(floatcos(angle)*200);
        px = -(floatsin(angle)*200);
        angle += 90.0;
    }
    else if(angle >= 180.0 && angle < 270.0)
    {
        angle -= 180.0;
        py = -(floatcos(angle)*200);
        px = (floatsin(angle)*200);
        angle += 180.0;
    }
    else if(angle >= 270.0 && angle < 360.0)
    {
        angle -= 270.0;
        py = (floatcos(angle)*200);
        px = (floatsin(angle)*200);
        angle += 270.0;
    }
    else
    {
        py = (floatcos(angle)*200);
        px = -(floatsin(angle)*200);
    }

    format(mess, sizeof(mess), "Original Angle: %f AD: %f Angle:%f py:%f px:%f",atan2(x2-x,y2-y),AD,angle,py,px);
    SendClientMessage(damagedid,White,mess);
As you can see in picture the angles in that box are always >180 && <=270 so supposedly the py should be negative and the px positive (because I set the angle from 0 to 90 so the floatcos and floatsin would return positive values). But as you can see only 1 follow that rule...

How come that these values seem random? They look random when moving (increasing/decreasing distance) and shooting but when standing and shooting you notice that all the variables stay the same, so this proves they're not random, but simply they're not right... I would love if someone could explain me what I've done wrong... I've tried so many different ways. If anyone knows a easier way please tell me.
Reply
#2

Because you haven't specified what anglemode that Floatcos, Floatsin etc. will be using. So they are in 'radians' by default.

Код:
(Float:value, anglemode:mode=radian)
Float:value	The angle from which to get the cosine.
anglemode	The angle mode to use, depending on the value entered.
Quote:
Originally Posted by SA-MP Wiki
GTA/SA-MP use degrees for angles in most circumstances, for example GetPlayerFacingAngle. Therefore, it is most likely you'll want to use the 'degrees' angle mode, not radians.
Also note that angles in GTA are counterclockwise; 270° is East and 90° is West. South is still 180° and North still 0°/360°.
Example:
pawn Код:
new Float:x, Float:y, Float:x2, Float:y2, Float:AD, Float:angle, mess[128];
    GetPlayerPos(playerid, x, y, AD);
    GetPlayerPos(damagedid, x2, y2, AD);
    new Float:var = atan2((x2 - x), (y2 - y));
    GetPlayerFacingAngle(damagedid, AD);
    angle = (var + AD);
    if(angle >= 360) angle -= 360.0;
    if(90.0 <= angle < 180.0)
    {
        y = -(floatcos(angle - 90.0, degrees) * 200);
        x = -(floatsin(angle - 90.0, degrees) * 200);
    }
    else if(180.0 <= angle < 270.0)
    {
        y = -(floatcos(angle - 180.0, degrees) * 200);
        x = (floatsin(angle - 180.0, degrees) * 200);
    }
    else if(270.0 <= angle < 360.0)
    {
        y = (floatcos(angle - 270.0, degrees) * 200);
        x = (floatsin(angle - 270.0, degrees) * 200);
    }
    else
    {
        y = (floatcos(angle, degrees) * 200);
        x = -(floatsin(angle, degrees) * 200);
    }
    format(mess, sizeof(mess), "Original Angle: %f AD: %f Angle:%f py:%f px:%f", var, AD, angle, y, x);
    SendClientMessage(damagedid, White, mess);
Sources:
https://sampwiki.blast.hk/wiki/Floatcos
https://sampwiki.blast.hk/wiki/Floatsin
https://sampwiki.blast.hk/wiki/Floattan
Reply
#3

Holy cow... I could swear that the default was degrees, that's why I didn't care about the degrees... I just misread that... damn so much time wasted just because of the radians...

Thank you, it's working just fine now.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)