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

1. Senior Member
Join Date
Aug 2009
Posts
294
Rep Power
0

## 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:
PHP 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?

2. Senior Member
Join Date
Jul 2008
Posts
125
Rep Power
0

## Translating translations

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;

Java 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
Java 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
Java Code:
```NewPos[0] += PlayerX;
NewPos[1] += PlayerY;```

Java Code:
```NewPos[0] += PlayerX[COLOR="Red"]-x[/COLOR];
NewPos[1] += PlayerY[COLOR="#ff0000"]-y[/COLOR];```

4. Senior Member
Join Date
Aug 2009
Posts
294
Rep Power
0
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:
Java 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?

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:
Java 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,

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

Java Code:
`angle *= Math.PI / 180;`

6. Senior Member
Join Date
Aug 2009
Posts
294
Rep Power
0

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•