Results 1 to 6 of 6
 11302009, 08:34 PM #1Senior 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 = xPlayerX; int ny = yPlayerY; NewPos[0] = nx*Math.cos(angle)ny*Math.sin(angle); NewPos[1] = nx*Math.sin(angle)+ny*Math.cos(angle); NewPos[0] += PlayerXx; NewPos[0] += PlayerYy; int[] NewPos2 = {(int)NewPos[0],(int)NewPos[1]}; System.out.println(NewPos2[0]+ ":"+NewPos2[1]); return NewPos2; }
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
 11302009, 10:15 PM #2Senior Member
 Join Date
 Jul 2008
 Posts
 125
 Rep Power
 0
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;
 11302009, 10:39 PM #3
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 lowercase 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);
This is because after the rotation, you restore the position incorrectly, it should be
Java Code:NewPos[0] += PlayerX; NewPos[1] += PlayerY;
instead of
Java Code:NewPos[0] += PlayerX[COLOR="Red"]x[/COLOR]; NewPos[1] += PlayerY[COLOR="#ff0000"]y[/COLOR];
CodesAway  codesaway.info
writing tools that make writing code a little easier
 12012009, 09:22 AM #4Senior 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 1360 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;
It makes it spin slower, but still it jumps way to much..
Any idea why?
 12012009, 09:32 AM #5
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
Java Code:angle *= Math.PI / 180;
CodesAway  codesaway.info
writing tools that make writing code a little easier
 12012009, 10:04 AM #6Senior Member
 Join Date
 Aug 2009
 Posts
 294
 Rep Power
 0
Similar Threads

[SOLVED] \t not working correctly?
By Gakusei in forum New To JavaReplies: 8Last Post: 10182017, 10:29 AM 
dispose() method not working
By R&R in forum New To JavaReplies: 19Last Post: 11152010, 02:46 AM 
The code isnt working unless I add print statements at diffrent points!:confused:
By Addez in forum New To JavaReplies: 6Last Post: 11122009, 11:50 AM 
why isnt my background color displaying correctly ?
By nicnicnic in forum Java 2DReplies: 8Last Post: 10292009, 11:54 AM 
Gueesing Game Almost done, but not working correctly
By mbnumba6 in forum New To JavaReplies: 5Last Post: 03182009, 04:01 AM
Bookmarks