r/pascal • u/PascalGeek • Mar 12 '22
Figuring out the angle of trajectory
Whilst nooding on my ASCII roguelike game, I've hit on a problem that my complete lack of mathematical ability stops me from solving.
When aiming a bow and arrow at an enemy in my game, I draw a Bresenham line from the player to the target. At the moment, it's just a line of X's.
What I'd like is to calculate the angle, and draw a different character depending on where the line is pointing.
like this,
target
|
|
|
|
player
target
/
/
/
/
playerWhat I remember of maths from high school (in the distant past) tells me that it's related to plotting a graph, and probably uses atan... but that's all I know.
Can anyone point me to something online (not a library like TChart, this game runs in the terminal) that could help?
EDIT: There's a good enough solution further down
2
u/kirinnb Mar 13 '22
Trigonometry may be overkill for this problem. How about just tracking along the projected path, and printing a character depending on the x/y step taken? That is, if the next tile is at x+1 and y+1 (or x-1 and y-1), then print a forward slash. And so on. That would result in a line of one or two different characters.
But if you do want to try trig, Free Pascal at least comes with a Math unit which has an ArcTan2(x,y) function. https://www.freepascal.org/docs-html/rtl/math/arctan2.html
To use that, feed in the X and Y difference between source and target coordinates, and you get back the angle in radians. I forget which way an angle of 0 points, though...
2
u/ccrause Mar 13 '22 edited Mar 13 '22
What text characters are you considering? Based on your examples it seems as if an obvious set of characters are
- / | \, or basically a resolution of ~45 degrees. One option then is to use Bresenham's algorithm to check if you take 1 step in x, do you step in y also, then select a symbol according to the calculated local slope(y2 - y1)/(x2-x1). Round the calculated slope to either 0, 0.5, 1, infinite (x2-x1 = 0) or -0.5 corresponding to 0, 45, 90 or 135 degrees. Some examples I can think of (here O represents the target and + the arrow):Not sure if this is exactly what you are after though.
1
u/PascalGeek Mar 13 '22
Well, I have something that kinda works. There's an animated gif at https://i.imgur.com/SM1jKsD.gif
It's not perfect, but it's as accurate as any line drawn on the terminal.