Results 1 to 6 of 6
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default 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?

    Thanks for reading :D

  2. #2
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default 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;

  3. #3
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by Addez View Post
    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;

    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

  4. #4
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    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?

  5. #5
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by Addez View Post
    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;
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  6. #6
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

Similar Threads

  1. dispose() method not working
    By R&R in forum New To Java
    Replies: 19
    Last Post: 11-15-2010, 02:46 AM
  2. Replies: 6
    Last Post: 11-12-2009, 11:50 AM
  3. Replies: 8
    Last Post: 10-29-2009, 11:54 AM
  4. Gueesing Game Almost done, but not working correctly
    By mbnumba6 in forum New To Java
    Replies: 5
    Last Post: 03-18-2009, 04:01 AM
  5. [SOLVED] \t not working correctly?
    By Gakusei in forum New To Java
    Replies: 5
    Last Post: 05-06-2008, 05:45 PM

Posting Permissions

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