Is my angle computation logic correct? Should I normalize angles differently?
As @ravenspoint and @btilly pointed out, calculating precise angles with atan
can be prone to floating-point errors in this case, we could compare coordinates directly to check for horizontal, vertical, or 45° diagonal lines.
Given a
starting point) at (x1, y1)
nd a catcher (target point) at (x2, y2)
:
↑ North : x2 == x1
and y2 > y1
→ East: y2 == y1
and x2 > x1
↓ South: x2 == x1
and y2 < y1
← West: y2 == y1
and x2 < x1
(x2 - x1)
is equal to (y2 - y1)
, it means "run" (horizontal distance) is equal "rise" (vertical distance).
↗ North East: (x2 - x1) = (y2 - y1)
and x2 > x1
↙ South West: (x2 - x1) = (y2 - y1)
and x2 < x1
↖ North West: (x2 - x1) = -(y2 - y1)
and x2 < x1
↘ South East: (x2 - x1) = -(y2 - y1)
and x2 > x1
A line with a slope of 1 is a 45° diagonal.
Is my method for selecting the closest player correct? How can I verify it?
Calculating Euclidean distance should be enough.
Is there a more robust way to handle player rotation?
Clockwise rotation looks good.
----
Your code has a few logical issues:
The logic appears to check only the thrower initial direction and stops the turn if no catcher is there on the initial direction
The catcher's new throwing direction seems to be a rotation of the previous thrower's direction. The catcher's new orientation should be the opposite of the direction they received the ball from
The simulation does not seem to remove a player from the field after they have thrown the ball
Here are the animations that demonstrate this logic: