Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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?

share|improve this question
    
I think you should clarify more. What does the direction mean? By that I mean what degree and what is the point of reference. –  picakhu Nov 17 '11 at 15:30
    
@picakhu: With the conventions being used here the direction is the "compass angle" of the movement, with 0 being straight up, 90 straight right, etc. –  Henning Makholm Nov 17 '11 at 15:33
    
@HenningMakholm, Thanks! –  picakhu Nov 17 '11 at 15:36
    
@John: What do you already know about how the sine and cosine works? That is, do you have any preferred definition of them? One possible definition of sine and cosine is that they are the unique functions that make your formula work when the distance is $1$. When you know how to move one unit step in your chosen direction, multiplying both coordinate offsets by the distance just adjust the length of the step to be the given distance without changing the direction. –  Henning Makholm Nov 17 '11 at 15:37
    
You're not increasing the sine of the object's $x$ position multiplied by the distance you want to travel; you're increasing the object's $x$ position by the sine of the angle multiplied by the distance you want to travel. Does that clear things up? –  joriki Nov 17 '11 at 15:41

1 Answer 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.