Search code examples
algorithmpath-finding

Algorithm to find grid spaces in between two cells


I am making a grid based game which has "line of sight" targeting. Often times a game engine would use Raycast for this but I don't want to use an engine so I am trying to "roll my own" solution.

So basically, given P1,P2 pairs I want to find all those spaces between them (marked X).

I am having a big of a hard time figuring out how to do this. Somehow I have to find out which sides are closest together and use those as my starting points for "raycast". Then I guess I could take "samples" at cell-size increments and compare those with the indexes of the cells.

Unfortunately, I don't have any code yet ... I was hoping some could help with some pseudocode just to get the algorithm. I think if I could figure out how to get the start and end points for each of the pink lines then I could use that to find the orange squares.

enter image description here


Solution

    1. Apparently, Bresenham algorithm is a good choice here.

    enter image description here

    I wish I could post some content in addition to the link but it wouldn't help me give a complete context. So, better to visit the link and use the information there. Don't miss out on the comments section. There are good insights there as well.

    1. Please check this as well Elegant/Clean (special case) Straight-line Grid Traversal Algorithm?.

    enter image description here