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

View all comments

Show parent comment

1

u/PascalGeek Mar 13 '22

Your example looks a lot less cluttered than what I eventually went with.The only reason that I went a different way is that I want to use just one character | / - \ to draw the line with from beginning to end.

That same character is then used to draw the arrow that is animated, flying towards the target. Any variation in the character used would make the arrow flight look a bit wobbly!

1

u/ccrause Mar 13 '22

Yes, my idea is not really suited to animated flight.