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