# My rotate 2d pos method isnt working correctly..

• 11-30-2009, 08:34 PM
My rotate 2d pos method isnt working correctly..
Hai!
Im trying to make a method where I give them a point, and a point of the position it should rotate around + the amount of degrees to rotate.
Then it should return the new position.
But right now, it gives such random positions that i have no clue what to do
Heres my mehtod:
Code:

```public static int[] rotatePos(double angle,int x,int y,int PlayerX, int PlayerY){         double [] NewPos = {0,0};         int nx = x-PlayerX;         int ny = y-PlayerY;         NewPos[0] = nx*Math.cos(angle)-ny*Math.sin(angle);         NewPos[1] = nx*Math.sin(angle)+ny*Math.cos(angle);         NewPos[0] += PlayerX-x;         NewPos[0] += PlayerY-y;         int[] NewPos2 = {(int)NewPos[0],(int)NewPos[1]};         System.out.println(NewPos2[0]+ ":"+NewPos2[1]);         return NewPos2;     }```
Now first I make so that it got correct distance from the orgin by setting nx and ny (new x and new y)
then I use a common formula for rotating a point around orgin.
Then I add back the distance to the axis so that the point moves back so it seems like its not rotation around orgin but around the point.

But it gives alot of random number, Could anyone help me fix this, or show me a better method for doing this?

Thanks for reading :D
• 11-30-2009, 10:15 PM
paul pasciak
Translating translations
Your formula seems correct.
I work with offset rotations all the time.
You translated to the origin correctly.
You did not translate back using the proper values.
(Although it should work with x and y = 0)

lines 7 and 8 should be:

NewPos[0] += PlayerX;
NewPos[0] += PlayerY;
• 11-30-2009, 10:39 PM
CodesAway
Quote:

Originally Posted by Addez
Code:

```public static int[] rotatePos(double angle,int x,int y,int PlayerX, int PlayerY){         double [] NewPos = {0,0};         int nx = x-PlayerX;         int ny = y-PlayerY;         NewPos[0] = nx*Math.cos(angle)-ny*Math.sin(angle);         NewPos[1] = nx*Math.sin(angle)+ny*Math.cos(angle);         NewPos[0] += PlayerX-x;         [COLOR="Red"]NewPos[0][/COLOR] += PlayerY-y;         int[] NewPos2 = {(int)NewPos[0],(int)NewPos[1]};         System.out.println(NewPos2[0]+ ":"+NewPos2[1]);         return NewPos2;     }```

First off, there's a typo (highlighted in red), the 0 should be a 1. Also, for clarification, you are rotating around (PlayerX, PlayerY), correct? BTW, the variable names (e.g. PlayerX, PlayerY, NewPos, NewPos2) should start with a lower-case letter (as per Java specs). Upper case letters are for classes.

The second concern is after you rotate the point relative to the origin, you don't restore the original point correctly.

For example, after fixing the typo try
Code:

`rotatePos(0, 20, 10, 10, 10);`
and you will get (0, 0).

This is because after the rotation, you restore the position incorrectly, it should be
Code:

```NewPos[0] += PlayerX; NewPos[1] += PlayerY;```

Code:

```NewPos[0] += PlayerX[COLOR="Red"]-x[/COLOR]; NewPos[1] += PlayerY[COLOR="#ff0000"]-y[/COLOR];```
• 12-01-2009, 09:22 AM
Ahh you guys are real cleverheads XD
Hehe, Thanks alot, it finaly works now!
Almoste anyways..
the "angle" is given in 1-360 degrees.
I would need to change this to radians i assume since now its not rotating 1 degrees, it rotates randomly.

So I tried to do this:
Code:

`angle = angle/Math.PI;`
But that doesnt do it..
It makes it spin slower, but still it jumps way to much..
Any idea why?
• 12-01-2009, 09:32 AM
CodesAway
Quote:

Originally Posted by Addez
Ahh you guys are real cleverheads XD
Hehe, Thanks alot, it finaly works now!
Almoste anyways..
the "angle" is given in 1-360 degrees.
I would need to change this to radians i assume since now its not rotating 1 degrees, it rotates randomly.

So I tried to do this:
Code:

`angle = angle/Math.PI;`
But that doesnt do it..
It makes it spin slower, but still it jumps way to much..
Any idea why?

You're using the wrong conversion.

360 degrees = 2 * Math.PI radians
1 degree = Math.PI / 180 radians
x degrees = x * Math.PI / 180 radians

For example,

Code:

`90 degrees = 90 * Math.PI / 180 = Math.PI / 2 radians`
So, to convert from degrees to radians, the conversion should be

Code:

`angle *= Math.PI / 180;`
• 12-01-2009, 10:04 AM