1
$\begingroup$

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?

  • 0
    @John, make sure you draw a picture of the situation, clearly showing the coordinates of the new and old points. That should help a lot.2012-01-16

1 Answers 1

1

Let us say that you start at a point $(0,0)$ and want to go to a point $(x,y)$. For the purposes, assume that $x$ and $y$ can take both positive and negative values which generalizes my answer to all starting points.

However all you are given is the direction $\theta$ and the distance $d$ which leads you to $(x,y)$. So, you have that $d=\sqrt{x^2+y^2}$ and that $\tan\theta=-y/x$(why?).

Now, it is a simple problem of using $d$ and $\theta$ to figure out $x$ and $y$ and your equations pop right out of that.