Last active
August 29, 2015 14:15
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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