Field of view (or fog of war)

Show off your games, demos and other (playable) creations.
Post Reply
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Field of view (or fog of war)

Post 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).
Attachments
diagonal-marching-01.png
diagonal-marching-01.png (26.29 KiB) Viewed 8308 times
2021-07-01T23_23_08-field-of-view-01.png
2021-07-01T23_23_08-field-of-view-01.png (86.53 KiB) Viewed 8310 times
field-of-view-01.love
(1.36 KiB) Downloaded 244 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Field of view (or fog of war)

Post 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.

Attachments
2021-07-02T11_33_23-field-of-view-02.png
2021-07-02T11_33_23-field-of-view-02.png (90.02 KiB) Viewed 7696 times
field-of-view-02.love
(1.47 KiB) Downloaded 242 times
Last edited by darkfrei on Thu Jul 08, 2021 2:30 am, edited 1 time in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
paco72
Prole
Posts: 6
Joined: Wed Jul 07, 2021 8:58 pm

Re: Field of view (or fog of war)

Post by paco72 »

Very nice work!
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Field of view (or fog of war)

Post 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 1375 times
Attachments
diagonal-marching-01.love
(1.75 KiB) Downloaded 63 times
Last edited by darkfrei on Fri Mar 31, 2023 12:50 pm, edited 4 times in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: Field of view (or fog of war)

Post by GVovkiv »

link to video is probably broken
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Field of view (or fog of war)

Post 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 1366 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 1350 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 33 guests