Okay, so I’ve been learning about raycasting for 1 or 2 days now and I feel like that I have most of it down.
I’ve already gone through most of the online resources that I could find, so please avoid linking Google’s top results for the subject, those won’t help.
Raycasting is a great thing with many uses, and luckily it’s easy to implement in systems where you have something like a tilemap to make sure where you objects begin and end.
However, when you don’t have such a system but objects are dynamically positioned on your map (e.g. they can be anywhere), and they’re not guaranteed to be quad shaped we have to check against line segments (polygons).
Such a system is demonstrated in tutorials like on Red Blob Games’ 2D visibility tutorial, or in Sight & Light’s tutorial.
As far as I understand, implementing such a solution is not hard: Cast a ray for every line segment’s every point with 2 additional rays biased with a minimal value like 0.00001 radians, so the ray will not stop at the wall’s corner but goes through until it hits the next wall behind it (<- this by the way isn’t even discussed in Red Blob Games’ tutorial, but it’s definitely there in the source code), keep track of the hit points and then build a mesh from the point list using triangles.
My question is however, about how to decide if a wall occludes another?
Since my English is not the best when it comes to technical explanations I will use a picture to demonstrate my issue:
How does the demo on Red Blob Games’ site know that the points marked with red are being occluded by other line segments?
I image that I would have to check every of my rays against every line segment in the game, but how would I know if a ray is going through a line segment?
I’m sure there are many mathematical approaches for this kind of problem, but none of them are discussed in the online tutorials (all Red Blob’s site says is that it’s too complex to be discussed in the tutorial which is kind of ironic, seeing that that’s the reason why a tutorial would be needed in the first place). ??? I downloaded the source code, but honestly it’s kind of a mess and I couldn’t get much out of it.
Any help would be greatly appreciated.