views:

501

answers:

4

my onscreen object has a var rotation (in degrees).

how do i fix it so when i push up arrow it move forwards and not just x++ and y++?

+1  A: 

Use trigonometry.

You have the angle and the length of movement (which is your hypotenuse) so you should be able to use sin or cos to calculate the amount of x and y movement required.

Pyrolistical
+3  A: 
x += sin(rotation) * speed;
y += cos(rotation) * speed;

But it depends on the orientation of your rotation. This code will work for rotation orientated up (north) at 0 degrees, moving clock-wise.

Pindatjuh
0 degrees for north is not standard. In mathematics, 0 degrees is always to the right, increasing counter-clockwise.
Peter Alexander
It's a matter of taste.
Pindatjuh
+6  A: 

Not 100% sure that this is what you want, but I think you want to do:

x += speed * cos(angle);
y += speed * sin(angle);

Where angle is the rotation of your object, and x and y are its coordinates. speed is the speed of your object.

Peter Alexander
This is where 0 degrees starts right (east) at 0 degrees, moving counter clock-wise.
Pindatjuh
lol, our posts/comments are practically mirroring each other :P
Peter Alexander
Yea, quite funny! :D
Pindatjuh
You gave him the code without explaining why it works. JasonX is obviously learning here and you are not helping by short cutting for him.
Pyrolistical
A: 

Just to clarify, since this is C#, you'd want to do this

double radians = (Math.PI/180)*angleInDegrees;
x += speed * Math.Cos(radians);
y += speed * Math.Sin(radians);

As other posters have mentioned, this is based on using trigonometry to apply a rotation to a speed vector (in this case, the 0 degrees vector is pointing to the right).

Dan Bryant