Calculating X Y movement based on rotation angle?

Rob picture Rob · Mar 4, 2011 · Viewed 25.6k times · Source

Say I have an object in 2D space that can rotate and then should move according to its rotation angle.

For example:

  • If angle is 0(pointing upwards), then on_timer it should move 1 by Y and 0 by X.

  • If angle is 45, then it should move 1 by Y and 1 by X.

  • If pointing 90 degrees, then 0 by Y and 1 by X.

  • If pointing 135 degrees, then -1 by Y and +1 by X, etc.

Do you know any functions for calculating this?

Answer

gjvdkamp picture gjvdkamp · Mar 4, 2011

well, it seems to move at different speeds for different angles.

For 0 degress (straight up) it moved by 1, but 45 begrees it moved sqrt(1^2 + 1^2) = 1.41.

I think you want to look at Sin and Cos.

X += Speed * Math.Cos(angle);
Y += speed * Math.Sin(angle);

Regards Gert-Jan