Page 1 of 1

Field of view, limited by sprites

Posted: Mon Nov 14, 2016 7:10 pm
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

Re: Field of view, limited by sprites

Posted: Tue Nov 15, 2016 7:49 am
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.

Re: Field of view, limited by sprites

Posted: Tue Nov 15, 2016 8:00 am
by irok84
ivan, so tell me the secret :)

EDIT: I didn't see full post previously, sorry and thanks.

Re: Field of view, limited by sprites

Posted: Tue Nov 15, 2016 8:00 am
by drunken_munki

Re: Field of view, limited by sprites

Posted: Tue Nov 15, 2016 8:18 am
by irok84
drunken_munki, thanks for link, I'll try this too.

Re: Field of view, limited by sprites

Posted: Tue Nov 15, 2016 12:47 pm
by irok84
So, I show You progress :)
ATM, only raycasting.

Image

Image

Image

Re: Field of view, limited by sprites

Posted: Tue Nov 15, 2016 7:44 pm
by Positive07