Using Primitives to make a Lighting System which shows the first visible wall--

Hychan

Member
I followed that one tutorial (https://www.yoyogames.com/blog/419/realtime-2d-lighting-in-gamemaker-studio-2-part-1) to achieve a dynamic lighting system in my top-down game (which was a pain, because the article refuses to load, and I had to find an archive of it. Do y'all know why they took this tutorial down?). With the knowledge in that tutorial, I was able to reach this point:
not showing walls 2.gif
My game's collision is grid-based, since I'm generating procedural dungeon layouts, which is why I decided to just use Gamemaker's vertex functions instead of using a shader. If the tutorial above doesn't load for you, basically I'm looping through all visible grid cells and building a primitive which casts a shadow behind each wall cell relative to the player's position. This runs decently enough (still haven't dropped under 60 fps).

My problem, though, is that I need the first wall to be visible to the player, while every wall behind that needs to still be hidden by the shadow of the first wall, to hide secret rooms and such. I tried offsetting each grid tile's x and y positions relative to the player before casting the shadow, using this code:
GML:
var px1 = (i*16);     // top left
var py1 = (t*16);
var px2 = ((i*16)+16);    // bottom right
var py2 = ((t*16)+16);
var center_x, center_y;
center_x = mean(px1,px2);
center_y = mean(py1,py2);
px1+=lengthdir_x(24,point_direction(x,y,center_x,center_y));
py1+=lengthdir_y(24,point_direction(x,y,center_x,center_y));
px2+=lengthdir_x(24,point_direction(x,y,center_x,center_y));
py2+=lengthdir_y(24,point_direction(x,y,center_x,center_y));
but that gives this result, which is atrocious:
bad solution 3.gif
I also tried just increasing the size of each grid cell, to fill in the gaps between squares, which gave me this result:
better solution.gif
This is ALMOST the effect I'm looking for, but there are still gaps if you get close enough to the walls.

Am I approaching this problem from the wrong angle? Should I try using a shader instead, or is there a different way to build a primitive which would fill in the gaps between cells?
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
It sounds like you are trying to achieve a similar effect to Teleglitch? There's an article about it, complete with source code:
Teleglitch – Viewcones | Simon schreibt.

As for your existing implementation, it doesn't line up since you are taking direction from the center of the block - should rather pick directions for each corner of the tile.
 

Hychan

Member
It sounds like you are trying to achieve a similar effect to Teleglitch? There's an article about it, complete with source code:
Teleglitch – Viewcones | Simon schreibt.

As for your existing implementation, it doesn't line up since you are taking direction from the center of the block - should rather pick directions for each corner of the tile.
Yeah, that looks exactly like what I want! Unfortunately I don't quite understand that code, but from what it looks like and what you said it seems I can't get away with this effect with just quads, and I'll need to implement some more difficult shapes? I'm not sure how to do that, but I'm looking into it now--

EDIT, I actually did figure it out!
ta da.gif
Your advice is what keyed me off to the solution, thank you so much! All I had to do was individually define the x and y coordinates for each corner, not just the top left and top right corners. Again, thank you! This has been irritating me for days!
 
Last edited:
Top