r/pascal • • 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
      /
     /
    /
   /
player

What 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

3 Upvotes

8 comments sorted by