2D line trace over grid

RattyRatty Posts: 12Member

Hi, attached is a little project that demonstrates how to find all the points at which a 2D line intersects a grid (a tilemap in this case).
It correctly finds all intersections of the line with the X and Y grid (red for X, blue for Y) and even gives the distance the line has travelled in each grid cell.

This should be useful for 2D physics (bullets, line of sight etc) or it could be used for drawing lines. I have found various line trace implementations on the web but none were nearly as clean as this , or included the distance calc that is important to me.

I created this essentially as a bit of pseudo code, I intend to convert it to use in a shader for texel (terraria) style lighting and wanted something I could actually debug before trying to do this in the shader language. It looks like something that might be useful though hence why I post it here.

I hope someone finds this useful.


Tags :
Sign In or Register to comment.