Field of view, limited by sprites

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
irok84
Prole
Posts: 4
Joined: Mon Nov 14, 2016 7:04 pm

Field of view, limited by sprites

Post by irok84 »

Hi.

On begining I want to say "thank you" for this nice engine. I realy love LÖVE :)

In my project I have level like this:
Image

And I want to limit the field of view of player to let him "see" only things not curtained by sprites. Like on screen below:
Image

I feel that I should use raycasting to find visible part and i'm think about using shaders to cover invisible part (for example: grain effect). But I don't have an idea how to do that. Is it even possible to do it using raycasting + shaders? Or maybe my idea is wrong and there is a better way to obtain this feature?

Any constructive answer will welcome.

Thanks
Jakub
User avatar
ivan
Party member
Posts: 1912
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Field of view, limited by sprites

Post by ivan »

Hello Irok, there is a simple way to achieve the effect you are going for without shaders or raycasting.
If you only have 1 light source (the position of the player) - you only need to draw the shadows.
This can be done by the following pseudo algorithm:
1.iterate each polygon in the environment
2.iterate each edge in the polygon
[optional optimization, ignore edges on the 'back' of polygons since they're already in the shadow, 'back' edges can be detected using the dot product]
3.draw the shadow for the edge
a) convert the vertex edge to local coords: localVector1 = vertex1 - playerPosition
b) find the length and normal of the local vector: normal = localVector1/length(localVector1)
c) extend/project the vertex away from the player using the normal vector: projected1 = normal1*castDistance
d) draw a trapezoid for each edge: vertex1,vertex2, vertex2+projected2, vertex1+projected1
code: viewtopic.php?f=14&t=78726#p173502

That's it, as long as you're working with 1 light source, the technique above should work even with concave polygons.
You can draw your shadows on top of a while radial gradient for a better effect.
Last edited by ivan on Tue Nov 15, 2016 8:15 am, edited 5 times in total.
irok84
Prole
Posts: 4
Joined: Mon Nov 14, 2016 7:04 pm

Re: Field of view, limited by sprites

Post by irok84 »

ivan, so tell me the secret :)

EDIT: I didn't see full post previously, sorry and thanks.
Last edited by irok84 on Tue Nov 15, 2016 8:15 am, edited 1 time in total.
drunken_munki
Party member
Posts: 134
Joined: Tue Mar 29, 2011 11:05 pm

Re: Field of view, limited by sprites

Post by drunken_munki »

irok84
Prole
Posts: 4
Joined: Mon Nov 14, 2016 7:04 pm

Re: Field of view, limited by sprites

Post by irok84 »

drunken_munki, thanks for link, I'll try this too.
irok84
Prole
Posts: 4
Joined: Mon Nov 14, 2016 7:04 pm

Re: Field of view, limited by sprites

Post by irok84 »

So, I show You progress :)
ATM, only raycasting.

Image

Image

Image
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Field of view, limited by sprites

Post by Positive07 »

for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests