Skip to content

Instantly share code, notes, and snippets.

@tiborsimon
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiborsimon/8a11aefb977702f61751 to your computer and use it in GitHub Desktop.
Save tiborsimon/8a11aefb977702f61751 to your computer and use it in GitHub Desktop.
Java Shooter Game - FieldDirector:: calculateAngleForTwoPoints(Circle base, Circle target) - http://tibor-simon.com/programming/auto-aiming-logic-for-games/
public static float calculateAngleForTwoPoints(Circle base, Circle target) {
float px = base.getCenterX();
float py = base.getCenterY();
float ex = target.getCenterX();
float ey = target.getCenterY();
float angle = 0;
if (px < ex && py < ey) {
angle = -(float) (Math.atan(Math.abs(px-ex) / Math.abs(py-ey))*180 / Math.PI);
} else if (px < ex && py > ey) {
angle = (float) (Math.atan(Math.abs(px-ex) / Math.abs(py-ey))*180 / Math.PI)-180.0f;
} else if (px > ex && py > ey) {
angle = -(float) (Math.atan(Math.abs(px-ex) / Math.abs(py-ey))*180 / Math.PI)-180.0f;
} else if (px > ex && py < ey) {
angle = (float) (Math.atan(Math.abs(px-ex) / Math.abs(py-ey))*180 / Math.PI)-360.0f;
}
return angle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment