I’m stuck for days at a (first trivial…) problem which I can’t solve right now which is why I am asking here.
For my next game I need to calculate the sight for each unit. The game shows a fog of war as long no unit is in range to see through. First I had a naive approach where I simply removed the fog around a unit. This was not sufficient since certain objects can hide a units line of sight.
Back to the drawing board I’ve implemented raycasting with Bresenham’s algorithms.
First I calculate the circle of sight with the circle algorithm, then I do a raytrace with the line algorithm from the center (units position) to every point on the circle.
This approach works as expected but has some artifacts in the corners where the algorithm doesn’t catch by design:
My question is:
Does somebody know how modify the algorithm for this case or has a alternative?
I’ve stuck since two days and can’t figure it out. Tested around with some state tables but the math is not strong with me…
I have created a sample project for this which you can find on Github
arrow keys you can control the character, with
A/B you can change the sight distance.
The issue is visible starting at a distance of 4.
The project is made in platformio. To build it with Arduino IDE just change the name of the “src” directory to “2DRaycast”
There is also a Hex file available.