I have had this math expression memorized for about 10 years. I know what it does but I do not understand the math behind it, and I would like to.
In a computer program where the top left corner of the screen is position 0,0 and moving downwards is increasing Y and moving right is increasing X.
You can move an object any distance in any direction with the following expression:
set the object's y position to its current position minus COSINE(direction * PI / 180) * distance
set the object's x position to its current position plus SINE(direction * PI / 180) * distance
or in code:
object.y -= Math.cos(direction * Math.PI / 180) * distance; object.x += Math.sin(direction * Math.PI / 180) * distance;
In the above expressions, direction is the angle you want to move the object in degrees and distance is the distance you want to move the object.
I understand that (degree * PI / 180) is converting the degree to a radian.
I also understand that I am subtracting from y instead of adding to it because on the graph (screen) I am using, y starts at the top and goes down as it increases.
What I do not understand is why increasing the sine(radian) of an objects x position multiplied by the distance you want to travel as well as increasing the cosine(radian) of the same objects y position and multiplying by the same distance will move the object in that direction to that distance.
Can someone explain how this works in a way I can wrap my head around?