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
    I think you should clarify more. What does the direction mean? By that I mean what degree and what is the point of reference.2011-11-17
  • 0
    @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.2011-11-17
  • 0
    @HenningMakholm, Thanks!2011-11-17
  • 0
    @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.2011-11-17
  • 0
    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?2011-11-17
  • 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.