I have an object located at (208,608) in my 480x800 screen (portrait orientation). I can touch anywhere on the screen (where y < 608
) and I will get touchX
and touchY
.
What is the angle I need to apply to my object so the it will face the point (touchX, touchY)?
I tried
double alfa = (float) Math.atan2(touchY-608,touchX-208) * 180 / PI;
myObject.setRotation(alfa);`
but this doesn't seem to work if I touch one half of my screen. I also think this might be because the point is located in different quadrants reletive to my start point?
The angle I am trying to get is the angle determined by the two points and the horizontal axis from the first point. What I am trying to do is something like a bow facing my moving finger on the screen. Can anyone help me get the right angle please?
EDIT: I found the answer:
// Calculates an angle a body has to have to face a certain point in space
public static float rotateFromPointToPoint(float pFromPointX,float pFromPointY,float
pToPointX,float pToPointY) {
float k1 = (float) (pToPointY - pFromPointY);
float k2 = (float) (pToPointX - pFromPointX);
float tan = k1 / k2;
float angle = (float) Math.atan(tan);
float rotation = (float) Math.toDegrees(angle);
if (pToPointX < pFromPointX) {
rotation = (float) Math.toDegrees(angle) + 180;
}
return rotation;
}
Maybe someone else will find this usefull.