Needing help - Co-Ords/Facing angle
#1

Hello, I need some help on something.....

Is there a way to find if a player is facing along the X or Y co-ord?

I am totally dumb-founded on this and need help.


Thanks in advance
_________________________________________________

If you think your so cool and 'good' at scripting, only truly good scripters would help out others.
Reply
#2

https://sampwiki.blast.hk/wiki/GetPlayerFacingAngle
Reply
#3

No you totally didnt understand - like i need to get something like a position infront of a player (the way they are facing).
Reply
#4

Search for "GetXYInFrontOfPlayer" on this forum.
Reply
#5

it uses trigonometry, i would right a function to explain it, but here is that function with comments by me! (comments are so you understand what you are doing, i know you might be thinking "it works, i don't give a care how," but you will, trust me)
pawn Код:
stock GetXYInFrontOfPlayer(playerid, &Float:x, &Float:y, Float:distance) //playerid, players x and y pos, distance you want in front of player. Float:x and Float:y, will be updated to the xy in front of player
{  

    new Float:a; //creates a float to hold playerangle
    GetPlayerFacingAngle(playerid, a); //gets the angle
    a -= 90; //subtracts 90, (sa-mp and normal angles are 90 degrees off) so 0 would mean you were facing east, we need north (i think, i learned this a long time ago, and i am not sure anymore)

    y += (distance * floatsin(a, degrees)); //gets the sin of the angle (in degrees), multiplies it by the distance, and adds that to x
    x += (distance * floatcos(a, degrees)); //gets the cosin (cos) of the angle, multiplies it by the distance, and adds that to y
}
floatsin will get the sinus (opposite over the hypotenuse) of an angle.
so if i entered 10.0 as the angle it would give me 0.173648
I multiply that by the distance (lets say 5) and i get 0.868240
then i add that to the existing x (1523.25) and get 1524.11828
with an angle more than 10.0 it would have added more, but the way the angle is facing makes it now work.

and now, how to get the angle based off of 2 XY's
first the function
pawn Код:
stock Float:AngleToPoint(Float:x,Float:y,Float:x1,Float:y1) //xy is your pos, x1y1 is the place you are trying to get angle between
{
    new Float:newang = atan(floatabs((x-x1)/(y-y1))); //gets the arctan, this is always a number between 0.0 and 90.0
    if (x1 <= x && y1 >= y) newang += 90.0; //quadrant 2 makes the number a full 360 for quad 2, stuff below does the same thing
    else if (x1 < x && y1 < y) newang = -newang + 270.0; //quadrant 3
    else if (x1 >= x && y1 <= y) newang += 270.0; //quadrant 4
    else if (x1 > x && y1 > y) newang = 90.0 - newang; //quadrant 1
    return newang; //returns the angle
}
you now have the angle between point a and point b.
what do you do with this you might ask, well, using the floatsin/cos functions, i can do this (0.3 needed)
pawn Код:
SetVelocityToward(playerid, Float:multi, Float:x2, Float:y2) //player velocities are wierd and usually end up with someone dead.
{
    if (IsPlayerInAnyVehicle(playerid))
    {
        new Float:x, Float:y, Float:z, Float:angle, Float:velox, Float:veloy;
        GetPlayerPos(playerid, x, y, z);
        angle = AngleToPoint(x, y, x2, y2);
        velox = multi * floatsin(angle, degrees);
        veloy = multi * floatcos(angle, degrees);
        SetVehicleVelocity(GetPlayerVehicleID(playerid), velox, veloy, 0.01);
    }
    return 1;
}
also i can do (doesn't need most recent function)
pawn Код:
SetVelocityForward(playerid, Float:multi)
{
    if (IsPlayerInAnyVehicle(playerid))
    {
        new Float:angle, Float:velox, Float:veloy;
        GetPlayerFacingAngle(playerid, angle);
        velox = multi * floatsin(angle, degrees);
        veloy = multi * floatcos(angle, degrees);
        SetVehicleVelocity(GetPlayerVehicleID(playerid), velox, veloy, 0.01);
    }
    return 1;
}
have fun!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)