0

I'm using the following to get the x and y position of an entity as it travels along an elliptical path over time:

x = Math.cos(time)*width/2
y = Math.sin(time)*height/2

Is there a simple way to rotate the entire thing by a certain amount of degrees, say 45, or 132 for example?

Joncom
  • 1,985
  • 1
  • 18
  • 29

3 Answers3

3

You may use a simple rotation transformation:

x1 = x*cos(a) - y*sin(a)
y1 = x*sin(a) + y*cos(a)

Where a - is the angle to rotate.

This Wikipedia article explains that in detail

Serge
  • 6,088
  • 17
  • 27
1

For each point(x, y) you calculated with above equation, you can rotate it theta degree(counter-clockwise) by the following equation

  • x' = x * cos(theta) - y * sin(theta);
  • y' = x * sin(theta) + y * cos(theta);

where x and y are the original coordinates before rotation, x' and y' are the coordinates after rotation, theta is the angle to rotate.

coordinate rotation

zdd
  • 8,258
  • 8
  • 46
  • 75
  • This seems like a good idea, but does not seem to work for me. I've updated my original post to include a code example. – Joncom Oct 15 '12 at 05:36
  • var newX = x * Math.cos( this.angle ) - x * Math.sin( this.angle ); var newY = x * Math.sin( this.angle ) - y * Math.cos( this.angle ); This is not the equation what I mentioned above. is should be var newX = x * Math.cos( this.angle ) - y * Math.sin( this.angle ); var newY = x * Math.sin( this.angle ) - y * Math.cos( this.angle ); – zdd Oct 15 '12 at 05:44
0

Yes, just do a 2D rotation on the resulting x and y to rotate your ellipse:

xrot = x * cos(A) - y * sin(A)
yrot = x * sin(A) + y * cos(A)

And remember that Radians = Degrees * PI / 180.

paddy
  • 60,864
  • 6
  • 61
  • 103