Page 1 of 1

Field of view (or fog of war)

Posted: Sat Jul 03, 2021 10:13 am
by darkfrei
Hi all!

There is my try to make the field-of view in Lua.
Issue: the diagonal walls are transparent.

Use WASD to move the green point.




If uses the diagonal marching, the worth points are on the crossings of line, between source and target midpoints and diagonals of every tile.
Exactly one point per tile and all tiles have a point (except 45 degree lines).

Re: Field of view (or fog of war)

Posted: Mon Jul 05, 2021 9:44 am
by darkfrei
Second version:
fixed bugs, the diagonal wall is not more transparent;
the field of view is round;
the seen tiles have another color.


Re: Field of view (or fog of war)

Posted: Wed Jul 07, 2021 9:14 pm
by paco72
Very nice work!

Re: Field of view (or fog of war)

Posted: Fri Mar 31, 2023 11:44 am
by darkfrei
Diagonal Marching
:awesome:

Code: Select all

-- License CC0 (Creative Commons license) (c) darkfrei, 2023
function getDiagonalMarch(angle)
    local dx, dy = math.cos(angle), math.sin(angle)
    local k = 1 / (math.abs(dx) + math.abs(dy))
    return dx * k, dy * k, k
end
:awesome:
2023-03-31.png
2023-03-31.png (47.67 KiB) Viewed 1517 times

Re: Field of view (or fog of war)

Posted: Fri Mar 31, 2023 12:05 pm
by GVovkiv
link to video is probably broken

Re: Field of view (or fog of war)

Posted: Fri Mar 31, 2023 12:28 pm
by darkfrei
GVovkiv wrote: Fri Mar 31, 2023 12:05 pm link to video is probably broken
Thanks, probably fixed.

Also, the math for factor k:
dx + dy is always 1;
atan2 (dy, dx) is always as given angle.
But normally the cos (angle) + sin (angle) cannot always give the 1, but the squared cos and sin functions.

So we are need to make the factor k, that will be used to make dx+dy=1 to be true.
It give us the dx and dy distances between diagonals. With this steps dx and dy you can march by all diagonals and step every tile just once, but every tile (it works 100% if you are in the diagonals crossings or on the diagonal that is more perpendicular to your movement).

https://www.desmos.com/calculator/0h3tavpxmw
2023-03-31T14_27_12-Desmos _ Grafik-Rechner.png
2023-03-31T14_27_12-Desmos _ Grafik-Rechner.png (187.45 KiB) Viewed 1508 times
https://www.desmos.com/calculator/lu0axfvswn
2023-03-31T14_43_43-Desmos _ Grafik-Rechner.png
2023-03-31T14_43_43-Desmos _ Grafik-Rechner.png (170.44 KiB) Viewed 1492 times

Code: Select all

function getDiagonalMarch(angle)
	local nx, ny = math.cos(angle), math.sin(angle)
	local k = 1 / (math.abs(nx) + math.abs(ny))
	return nx * k, ny * k, k
end