On Fri, Apr 14, 2017 at 2:19 PM, James Cameron <quozl at us.netrek.org> wrote:
> On Fri, Apr 14, 2017 at 12:01:17PM -0700, Trent Piepho wrote:
>> I've got to wonder why some are offset by -90 degrees and some
>> aren't?
>
> Sign of y input.

My first thought was that's not right.  Inverting the y-axis will
change the sign of the angle.  I.e.,

atan2(y, x) == -atan2(-y, x)

Using normal trigonometry, where the positive x axis is 0 and positive
y axis is 90.  Angle increase counterclockwise.

But...

> Most code has pattern atan2(x - mx, my - y), but code for torpedo
> guidance t_track in ntserv/daemon.c uses atan2(x - mx, y - my).

The argument to atan2 are supposed to by y,x, not x,y, assuming one
wants the normal trigonometric convention.  But I recall the sin/cos
tables in netrek are different, and they're what defines the truth
about how to interpret an angle.

When one uses atan2(dx, -dy), one has in effect rotated the angle by
positive 90 degrees.  I.e.,

atan2(dy, dx) == atan2(dx, -dy) + 90deg  // notice x,y order!
alternatively:
atan2(dy, dx) - 90deg == atan2(dx, -dy)

The code in t_track() hasn't just inverted y, it has also swapped the
x and y axes.  Then it arrives at the same result by adding in a -90
rotation.  It'd be faster to do it the way other code does.

In a less fractured code base, there should have been some utility to
do this so it wouldn't keep getting re-implemented.