When you are developing a game one thing is for certain, you will need math! Especially games with some sort of physics involved rely heavily on geometry calculations. I really regret that I didn't pay more attention in high school when we went over the basics of geometry, it would have been useful when I started building my game etchNride.

I needed the ray box intersection test for a "selection tool" I created for my 2D game. I thought it might be useful to someone so I decided to post it here.


There are two different functions included in the source that perform the actual intersection test. The first one is just a simple line segment - line segment intersection test which is executed 4 times, for each ray of the axis aligned rectangle box. The second function is based on "slabs" where a slab is the space between two parallel planes. I found the theory of this technique in the book "3D Computer Graphics" from Alan Watt and created a function from it. If performance is a must in your application you should implement the second function, it's slightly faster than the first.

Download the source: RayBoxIntersection.zip