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.
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
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
No,
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));
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)
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.
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),