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

# Thread: Looking for a method,

1. ## 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,   Reply With Quote

3. ## 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.  Reply With Quote

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

Time (May 13th, 2010)

5. ## 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  Reply With Quote

6. ## Re: Looking for a method, Originally Posted by copeg 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);
}``` Originally Posted by Json Have a look at Trigonometry Calculator - Visual Triangle Math Calculator

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

// Json

No,  Reply With Quote

7. ## 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));`  Reply With Quote

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

Time (May 13th, 2010)

9. ## Re: Looking for a method, Originally Posted by helloworld922 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)  Reply With Quote

10. ## 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.  Reply With Quote

11. ## Re: Looking for a method, Originally Posted by helloworld922 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),  Reply With Quote