25

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?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Rob
  • 643
  • 3
  • 8
  • 13

2 Answers2

37

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

Makonede
  • 442
  • 1
  • 6
  • 13
gjvdkamp
  • 9,929
  • 3
  • 38
  • 46
  • 10
    Thanks, got it working! :) double radians = (Math.PI / 180) * ( this.orient - 90 ) ; this.CorX = Convert.ToInt16(this.CorX + 15 * Math.Cos(radians)); this.CorY = Convert.ToInt16(this.CorY + 15 * Math.Sin(radians)); – Rob Mar 04 '11 at 11:45
1

Check this out: http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.matrix.aspx (examples at the bottom of the page)

Cosmin
  • 2,365
  • 2
  • 23
  • 29