- Joined
- Jan 12, 2018
- Messages
- 1
- Reaction score
- 0
Hello all,
I am a computer programmer and for fun, I'm rewriting the old "Star Trek" computer game where you play as the Enterprise, flying through the galaxy, fighting Klingons:
You'll note the unusual coordinate system here. For reason too complex for this post, my gameboard considers the upper-leftmost square to be (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 an 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' coordinates, heading should just be a matter of computing the angle to each Klingon:
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, using the actual coordinates from the first picture, above:
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, however, is where I'm having problems. Consider:
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 Enterprise/Klingon coordinates) 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 here. Does anyone see my mistake?
Thanks,
-RAO
I am a computer programmer and for fun, I'm rewriting the old "Star Trek" computer game where you play as the Enterprise, flying through the galaxy, fighting Klingons:
You'll note the unusual coordinate system here. For reason too complex for this post, my gameboard considers the upper-leftmost square to be (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 an 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' coordinates, heading should just be a matter of computing the angle to each Klingon:
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, using the actual coordinates from the first picture, above:
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, however, is where I'm having problems. Consider:
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 Enterprise/Klingon coordinates) 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 here. Does anyone see my mistake?
Thanks,
-RAO