Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Looking for a method,

  1. #1
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Looking for a method,

    I need a method that returns the degree's needed for a rotation for one image to basically turn to the other image,

    example:




    The degrees the the the rectangle, will need to turn to the dot on the circle,

    I hope you get what I mean,


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,260
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: Looking for a method,

    If I do understand what you mean, you need to use some simple trig to retrieve the angle...
    sin(angle) = opposite/hypotenuse
    or
    angle = arcsin(opposite/hypotenuse)
    Here, angle would be between the vertical arrow and the line between circle and rect, opposite would be the x distance between circle and rect, and hypotenuse would be distance bewtween circle and rect.

  3. The Following User Says Thank You to copeg For This Useful Post:

    Time (May 13th, 2010)

  4. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Looking for a method,

    Have a look at Trigonometry Calculator - Visual Triangle Math Calculator

    That might give you a visual clue as well as the formulas.

    // Json

  5. #4
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Looking for a method,

    Quote Originally Posted by copeg View Post
    If I do understand what you mean, you need to use some simple trig to retrieve the angle...
    sin(angle) = opposite/hypotenuse
    or
    angle = arcsin(opposite/hypotenuse)
    Here, angle would be between the vertical arrow and the line between circle and rect, opposite would be the x distance between circle and rect, and hypotenuse would be distance bewtween circle and rect.
    Thanks, I derived this formula and it works fine.

    	public static double getDegrees(Entity toFace, Entity faced) {
    		double A = toFace.getAbsX() - faced.getAbsX();
    		double B = toFace.getAbsY() - faced.getAbsY();
    		return Math.atan(B/A);
    	}

    Quote Originally Posted by Json View Post
    Have a look at Trigonometry Calculator - Visual Triangle Math Calculator

    That might give you a visual clue as well as the formulas.

    // Json

    No,

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Looking for a method,

    You're formula is slightly flawed. Arctangent is picky and doesn't take into account the signs of the two values, or when the x-direction is very close to zero (you'll essentially be trying to divide by 0, a very bad prospect).

    You can cheat a little bit by using the atan2 method to get the angle back from an x,y distances between the two points (in vector notation, going from the rectangle point directed towards the circle's point this is just x=circle.x - rectangle.x, y= circle.y - rectangle.y).

    System.out.println("The angle from the positive x-axis is: " + Math.atan2(yDistance, xDistance));

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    Time (May 13th, 2010)

  8. #6
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Looking for a method,

    Quote Originally Posted by helloworld922 View Post
    You're formula is slightly flawed. Arctangent is picky and doesn't take into account the signs of the two values, or when the x-direction is very close to zero (you'll essentially be trying to divide by 0, a very bad prospect).

    You can cheat a little bit by using the atan2 method to get the angle back from an x,y distances between the two points (in vector notation, going from the rectangle point directed towards the circle's point this is just x=circle.x - rectangle.x, y= circle.y - rectangle.y).

    System.out.println("The angle from the positive x-axis is: " + Math.atan2(yDistance, xDistance));
    I was wondering why the occurrence of that was happening,

    Well thanks that fixed one issue,

    Another issue I'm having is the instance of 2 or more,

    Once I use make one object(1) face another object(2), If i send another object(3) face object(2) he ends up facing object(1)

  9. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Looking for a method,

    without looking at the code there's no way we can tell what's going wrong. I'm guessing that you're either passing the wrong object references, though.

  10. #8
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Looking for a method,

    Quote Originally Posted by helloworld922 View Post
    without looking at the code there's no way we can tell what's going wrong. I'm guessing that you're either passing the wrong object references, though.
    Yea, well i fixed it awhile back, I frogot to reset the angle when I passed out another object(sprite),