# Looking for a method,

• May 12th, 2010, 06:33 PM
Time
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:

http://i44.tinypic.com/e04wax.jpg

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

I hope you get what I mean, (%)
• May 12th, 2010, 08:29 PM
copeg
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.
• May 13th, 2010, 02:38 AM
Json
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
• May 13th, 2010, 06:46 AM
Time
Re: Looking for a method,
Quote:

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.

Code :

``` 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
Have a look at Trigonometry Calculator - Visual Triangle Math Calculator

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

// Json

No,
• May 13th, 2010, 08:57 AM
helloworld922
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).

Code :

`System.out.println("The angle from the positive x-axis is: " + Math.atan2(yDistance, xDistance));`
• May 13th, 2010, 09:03 AM
Time
Re: Looking for a method,
Quote:

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).

Code :

`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)
• May 13th, 2010, 06:39 PM
helloworld922
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.
• May 15th, 2010, 12:24 PM
Time
Re: Looking for a method,
Quote:

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),