Relationship Between Unit Circle and SA-MP World
#1

If you're an avid sa-mp scripter, you may have seen this piece of code before
pawn Code:
x += (Distance * floatsin(-r, degrees));
y += (Distance * floatcos(-r, degrees));
Up until now, I really had no idea what this code even did, but seeing it applied in mathematics and then seeing applied in sa-mp gives rise to one question.

Look at the diagram below.

(Source: http://en.wikipedia.org/wiki/File:Unit_circle.svg)

t would represent a player's facing angle relative to the x-axis, and the point on the circle would be the point we are trying to solve for.

I know for a fact that the x coordinate is equal to the cos t, and the y coordinate is equal to sin t.

The player's position will not always be at 0, 0 so it's evident that we need to append these values to the player's current position, giving us this

x + cos t
y + sin t


but that produces an error in game and spawns the object to the right of the player (excuse the image sizes)


if we switch the code to be

x + sin t
y + cos t


we get the correct output


Why is that? By the definition of the unit circle, the first example should be correct.

I haven't found any reliable information as to why the initial code (at the top of the post) works, so if you have any knowledge as to why it does, please contribute.
Reply
#2

the GTA degrees go clockwise from -180 to 180, the maths unit circle goes with the clock from 0 to 360 (or reverse, dunno if I said it good in English :P)

At least I know it has to do with the directions (clockwise/counter-clockwise) and the domain ([-180,180] / [0, 360]) of the degrees.
Reply
#3

I have no idea what you mean, lol.

However, I think I've just figured out why.

A player's angle is relative to the y axis, not the x. For example, look at the diagram, t is the value returned from GetPlayerFacingAngle

Now here is the same diagram just flipped 90 degrees clockwise.


In case it was hard to read, I just constructed a congruent triangle in red.

Just looking at it, makes sense to me now.

x + sin t
y + cos t


I guess I just answered my own question
Reply
#4

I would love to know what this is. But as even the images with sin t cos t etc confuses me I doubt I will know any time soon. Until now i never heard of cos t and sin t.
Reply
#5

Quote:
Originally Posted by thefatshizms
View Post
I would love to know what this is. But as even the images with sin t cos t etc confuses me I doubt I will know any time soon. Until now i never heard of cos t and sin t.
don't worry, you will get it soon, dunno how old you 're.. but it isn't that hard.
Reply
#6

I'm 15 years old. The most with have done with circles in maths is circumference of a circle and the other basic things.
Reply
#7

Quote:
Originally Posted by Gamer_Z
View Post
the GTA degrees go clockwise from -180 to 180, the maths unit circle goes with the clock from 0 to 360 (or reverse, dunno if I said it good in English :P)

At least I know it has to do with the directions (clockwise/counter-clockwise) and the domain ([-180,180] / [0, 360]) of the degrees.
Are you 100% sure? I'm fairly confident that they go from 0-360, just anti-clockwise (east is 270, but is 90 in real life). I've never seen negative angles in SA:MP..
Reply
#8

Quote:
Originally Posted by MP2
View Post
Are you 100% sure? I'm fairly confident that they go from 0-360, just anti-clockwise (east is 270, but is 90 in real life). I've never seen negative angles in SA:MP..
try GetVehicleZAngle and you will see :P
Reply
#9

Quote:
Originally Posted by Gamer_Z
View Post
Quote:
Originally Posted by MP2
View Post
Quote:
Originally Posted by Gamer_Z
View Post
the GTA degrees go clockwise from -180 to 180, the maths unit circle goes with the clock from 0 to 360 (or reverse, dunno if I said it good in English :P)

At least I know it has to do with the directions (clockwise/counter-clockwise) and the domain ([-180,180] / [0, 360]) of the degrees.
Are you 100% sure? I'm fairly confident that they go from 0-360, just anti-clockwise (east is 270, but is 90 in real life). I've never seen negative angles in SA:MP..
try GetVehicleZAngle and you will see :P
I had to test this just to be sure, as I was very confident I was right, and I was.. (sorry to correct you :P)

[ame]http://www.youtube.com/watch?v=f9vm8VWRdMs[/ame]

pawn Code:
public OnPlayerUpdate(playerid)
{
    new Float:a, szString[32];
    if(!IsPlayerInAnyVehicle(playerid))
    {
        GetPlayerFacingAngle(playerid, a);
        format(szString, sizeof(szString), "Player Angle: %.0f", a);
    }
    else
    {
        GetVehicleZAngle(GetPlayerVehicleID(playerid), a);
        format(szString, sizeof(szString), "Veh Angle: %.0f", a);
    }
    GameTextForPlayer(playerid, szString, 3000, 3);
    return 1;
}
Are you referring to something else?
Reply
#10

Quote:
Originally Posted by MP2
View Post
I had to test this just to be sure, as I was very confident I was right, and I was.. (sorry to correct you :P)

http://www.youtube.com/watch?v=f9vm8VWRdMs

pawn Code:
public OnPlayerUpdate(playerid)
{
    new Float:a, szString[32];
    if(!IsPlayerInAnyVehicle(playerid))
    {
        GetPlayerFacingAngle(playerid, a);
        format(szString, sizeof(szString), "Player Angle: %.0f", a);
    }
    else
    {
        GetVehicleZAngle(GetPlayerVehicleID(playerid), a);
        format(szString, sizeof(szString), "Veh Angle: %.0f", a);
    }
    GameTextForPlayer(playerid, szString, 3000, 3);
    return 1;
}
Are you referring to something else?
Whathe.. ;o When I was creating the AutoPilot i had difficulty with it just because the car angles returned between -180 and 180, and I doubt sa-mp changed anything.. probbly some code I didn't notice was changing my angle >_>

at least, where it happend to me was north-east in LV, but I highly doubt location change will matter.

So well I was wrong ;p
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)