Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have an entity class which is updated every game tick. Let's just assume said entity moves forward constantly. What i want to know is, how can i make it so that i can give an angle to a function and it will make my entity move in that direction. let's say for example


Would make my character move to the right, or for example declaring my rotation as a global Integer:


Would make my entity move around in a small circle, I assume this includes some kind of vector math, which I haven't done any studying on so a brief explanation of that if it's necessary would be nice.

I would appreciate a small code snippet and an explanation I can analyze, thanks in advanced :)

share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

Well in the simplest sense you have something like this.

   y  |\
      | \
   m  |  \         s
   o  |   \        p
   v  |(a) \       e
(y)e  |angle\      e
   m  |      \     d
   e  |       \
   n  |        \
   t  |         \
      x movement

The speed is however fast the enemy is, and you can determine how much they should move in the x direction and how much they move in the y direction by taking the sin or cos of the angle and multiplying by speed. Because...

 sin(a) = x / speed


 x = speed * sin(a)


cos(a) = y / speed


y = speed * cos(a)

In your example moveForward(90) would yield speed * sin(90) or speed * 1 in the x direction and speed * cos(90) or 0 in the y direction (It should move to the right as you specified). That should get you started in the basic sense.

Making it general:

moveForward(float angle)
    x += speed * sin(angle);
    y += speed * cos(angle);
share|improve this answer
Ahh, thats a lot simpler than i imagined, thanks a lot :) –  Shaun Wild Sep 14 '12 at 16:11
I'm pretty sure you need to switch y's and x's. sin(alpha) = b/c. –  akled Sep 14 '12 at 17:52
@Bane it matches the drawing I made SOH-CAH-TOA, sin is opposite over hypotenuse which in the drawing is x / speed. If the angle were on the other side of the triangle it would be reversed (and if that is more practical for the purpose of game design I could change it, but I feel its all arbitrary). –  Kevin DiTraglia Sep 14 '12 at 17:57
I'm on my mobile phone so it was pretty hard to interpret that. I just skimmed over the formulas... –  akled Sep 14 '12 at 18:00
That's a really nice triangle you got there. +1 –  Bro Kevin D. Apr 18 '13 at 5:17
add comment

The other answer is wrong as of now, to correctly move along a plane based on a rotation you do the following:

posX += Math.cos(rotation) *  forwardSpeed + Math.sin(rotation) * strafeSpeed;
posY -= -Math.cos(rotation) * strafeSpeed + Math.sin(rotation) * forwardSpeed;

However I'd recommend making a variable for cos/sin that you update only when the rotation changes so you aren't calculating it 4 times a tick.

The strafeSpeed would be moving from side to side, the forwardSpeed for moving forward along your rotation.

edit: tesselode does the same thing except he doesnt have side to side movement.

share|improve this answer
add comment

You said update ticks, so I'm assuming you don't have a variable frame rate. If so:

x += speed * math.cos(angle)

y += speed * math.sin(angle)

If you're using variable frame rate, you need to multiply by delta time as well.

share|improve this answer
add comment

Your Answer


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.