Using ARCTAN to find an angle... using an unusual computer game coordinate system

greenapplestoo

New member
Joined
Jan 12, 2018
Messages
5
Hello all,

I am a computer programmer and for fun, I'm rewriting an old "Star Trek" computer game. In the game, you play as the Enterprise, flying through the galaxy, fighting Klingons:

trigpost1-jpg.5

Note the unusual coordinate system here. For reason too complex for this post, my gameboard considers the upper-leftmost square to be coordinate (0,0). Also, in this system, coordinates are expressed as (Y, X)... so the Enterprise would be at (2, 1) and that big black circle (a planet) would be (3, 4). Again, this is a programming thing and I have to live with it. It makes the math a little disorienting, but hopefully no less confusing.

As part of the game, I want the visual display to show the user the distance and heading to each Klingon ship. Distance was easy to work out - that's the Pythagorean Theorem - but heading is proving to be a headache. Given that I know the Enterprise's coordinates and the Klingons' (Y, X) coordinates, heading should just be a matter of computing the angle to each Klingon:

trigpost2-jpg.6

Those angles are educated guesses; I need to calculate the actual angles here. So this is boiling down to a right-triangle ratio problem. Given the example above, here is my calculation for the first Klingon at (1, 4), using the actual coordinates from the first picture, above:

trigpost3-jpg.7

Eyeballing it, this looks about right. I'm using opposite/adjacent to compute tan(x), then using arctan to find x. The answer "feels" right, too... That first Klingon looks about 18 degrees from the Enterprise.

The second Klingon at (4, 2), however, is where I'm having problems. Consider:

trigpost4-jpg.8

63 degrees??? That can't be right. I know I could just 360 - 63 = 297, and that's probably right... but a computer program will never know to do that. I need a sure-fire algorithm which (given any Enterprise/Klingon coordinate combo) will calculate the correct angle every time.

I think that (A) the weirdness of my coordinate system is muddying up the substitutions in the second calculation, or (B) I'm not using right-triangle ratios correctly here. Does anyone see my mistake?

Thanks,
-RAO
 
But the bearing to the second Klingon IS 63 degrees! Why do you think that is wrong?

The first one is at -18 degrees, meaning counterclockwise as far as this coordinate system is concerned; +63 degrees is the natural way to describe the second as a clockwise angle. And adding or subtracting 360 yields a coterminal angle, so -297 degrees means the same thing.

So your work is all correct, apart from your having ignored signs. The issue is, how are you going to USE the angle you get? If you need the angle to be always positive, with small positive angles being counterclockwise as you imply, then just change the sign (-18 becomes +18, 63 becomes -63), and, if the result is negative, add 360. But that system is contrary to the coordinate system you are using (which I am familiar with from past programming).

So, what are the angle requirements for whatever you are going to use to aim in the desired direction?
 
Helping math students is really our thing. I'll give you one free one if you donate some of the proceeds of the sale of the game to the website. :)

The arctangent FUNCTION is defined to produce values between -90º and +90º. You can use the arctangent to obtain only these values. This is the typical setup on your basic scientific calculator. Built in programming functions may also be defined in this way.

It is your responsibility to know what QUADRANT things are in. You can do this by examining your two linear differences.

Up and Right? Quadrant I - Take the result the arctangent gives.
Up and Left? Quadrant II - Add 180º to the negative result given by the arctangent.
Down and Left? Quadrant III - Add 180º to the positive result given by the arctangent.
Down and Right? Quadrant IV - Add 360º to the negative result given by the arctangent.

There is no substitute for knowing what your calculator produces.
 
I neglected to mention that in programming this sort of thing, you typically want to use the atan2() function, if it is available. Depending on the language, this takes atan2(x,y) or atan2(y,x) and gives you the appropriate angle in any quadrant, doing for you what tkhunny suggested doing manually. In particular, this is necessary if you are shooting at someone behind you. Of course, you have to make appropriate adjustments for your coordinate system.
 
...in programming this sort of thing, you typically want to use the atan2() function, if it is available. Depending on the language, this takes atan2(x,y) or atan2(y,x) and gives you the appropriate angle in any quadrant, doing for you what tkhunny suggested doing manually.
*blink*

I did not know this. Fascinating! [she said, cocking one eyebrow]

:lol:
 
But the bearing to the second Klingon IS 63 degrees! Why do you think that is wrong?

The first one is at -18 degrees, meaning counterclockwise as far as this coordinate system is concerned; +63 degrees is the natural way to describe the second as a clockwise angle. And adding or subtracting 360 yields a coterminal angle, so -297 degrees means the same thing.

So your work is all correct, apart from your having ignored signs. The issue is, how are you going to USE the angle you get? If you need the angle to be always positive, with small positive angles being counterclockwise as you imply, then just change the sign (-18 becomes +18, 63 becomes -63), and, if the result is negative, add 360. But that system is contrary to the coordinate system you are using (which I am familiar with from past programming).

So, what are the angle requirements for whatever you are going to use to aim in the desired direction?

Thanks Dr. Peterson,

I read your post and immediately thought, "Of course!" In my programming environment, the Y-axis is effectively flipped. What was once up is now down. I should have realized that immediately. My unit circle effectively looks like this:
TrigPost5.jpg

To answer... well, address your other question, I haven't fully thought out how I would use the angle once it was calculated. The classic versions of this game usually had a heading interface that looked like this:
TrigPost6.jpg

I would ideally like to recapture the "feel" of this interface, but I haven't thought out the details yet. Calculating the angle was the first step. I'll figure out the rest.

BTW, I'm programming in Java, and I do see an atan2() method. Thanks, I never would have thought to look at it otherwise. :)
 
Helping math students is really our thing. I'll give you one free one if you donate some of the proceeds of the sale of the game to the website. :)

The arctangent FUNCTION is defined to produce values between -90º and +90º. You can use the arctangent to obtain only these values. This is the typical setup on your basic scientific calculator. Built in programming functions may also be defined in this way.

It is your responsibility to know what QUADRANT things are in. You can do this by examining your two linear differences.

Up and Right? Quadrant I - Take the result the arctangent gives.
Up and Left? Quadrant II - Add 180º to the negative result given by the arctangent.
Down and Left? Quadrant III - Add 180º to the positive result given by the arctangent.
Down and Right? Quadrant IV - Add 360º to the negative result given by the arctangent.

There is no substitute for knowing what your calculator produces.

Thanks tkhunny,

Your points make a world of sense. Originally I thought if I just used arctan, the angle I wanted would, well, magically appear. Adding coding logic to check the quadrants shouldn't be too difficult.

Many thanks!
 
Top