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:
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:
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:
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:
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
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:
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:
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:
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:
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