Hi Bagels, I've also been working on a raycaster since I read your post over at blitzcoder! I have been following
this tutorial:
http://www.permadi.com/tutorial/raycast/When you are already knee-deep ;) into your own approach this might not be so helpful but maybe there are some
pointers there that you can use. My program is working now but I really had to sit down and think about the maths
myself (lol...) in addition to just copying it from the site.
In this approach you find the box you collided with *after* having found the location of the collision. So you already
know exactly where the collision happened, there's no need to figure this out from the box location. Once you know the
spot of the collision, you also have the box that belongs to it, and you can check if it is solid (making it a real
collision).
The potential collision points are always on borders between boxes. You compute these locations by "jumping"
from one border to the next. The angle of the ray of course determines the direction of the jump. Furthermore,
computation is also dependant on whether you are at that moment looking for a collision with a horizontal or a
vertical border in the grid (as that is how the method works).
For instance, suppose you are checking for a collision with a horizontal border, and the current ray is exactly 90
degrees (which is North in my case). This means the jump will always have a deltaY of -SECTOR_SIZE (= from one
border to the next, upwards), and a deltaX of 0 (as the ray goes straight up). If the ray is at another angle the
deltaX has to be computed using tan().
And similar computations are used for looking for collisions with vertical borders.
Then when you have found both, the nearest one is used. You then know the point of collision so you can compute the
distance. (Sometimes there will be only 1 point; for instance when you look for collisions with horizontal
borders and the current ray is at 0 or 180 degrees (going horizontal) there will never be a collision; at other times
the collision will be outside of the map as in your code too). Then the distance is used to compute the height of
that slice of wall.