Search code examples
c++game-enginegame-physics

Find all cells on bullet's trajectory in grid cell game


In simple game, where map (terrain) is divided in grid cell I have soldier which stands on one cell ( with indexes row=5, column=4 ) and soldier aims another soldier at cell row=8 column=5 (soldiers standing at the middle of cell point 5.5 , 4.5 and 8.5 , 5.5. How to find all cells on bullets's trajectory from soldier one to soldier two ? I have board representation like matrix. Is possible to efficient solve this with ray tracing algorithm ? ( I need only suggestion of algorithm or idea or example, not code ). Did anyone have similar problem ?


Solution

  • Try taking a look at Bresenham's line algorithm. It seems like you could use a modification of that to determine which cells were traversed.