# how to do left 4k dead visual effect

ok, I am writing a game(obviously). And I would like to use the “field of view” effect in left4kdead.

I know that it is open source, however the source is all crammed into like 3 lines, and hardly understandably(no offence).

h3ckboy

I’ve gotten several requests to explain that algorithm. I’ll probably write a tutorial about it soon.

is it really complicated?

Well, kind of. The short version is:

I project lines from the center of the screen to each pixel along the border, stepping one pixel at a time starting from the center. Whenever a “blocking” map pixel is found, I stop tracing that line. All other pixels get marked as lit.
A dot product check skips all lines outside the flashlight view, and a simple ramp function dims the light at the sides.

(This is superior to the otherwise often used technique of just raycasting a lot of rays at regular spacing away from the center since this uses the absolute minimum of rays required to cover 100% of the game screen. There still is a lot of repeated pixel checks which can be optimized away but didn’t fit in the 4k space.)

Is that this chunk?

``````
int j = 0;
for (; j < dist; j++)
{
int xx = xt * j / 120 + 120;
int yy = yt * j / 120 + 120;
int xm = xx + xCam - 120;
int ym = yy + yCam - 120;

if (map[(xm + ym * 1024) & (1024 * 1024 - 1)] == 0xffffff) break;

int xd = (xx - 120) * 256 / 120;
int yd = (yy - 120) * 256 / 120;

int ddd = (xd * xd + yd * yd) / 256;
int br = brightness[ddd] * brr / 255;

if (ddd < 16)
{
int tmp = 128 * (16 - ddd) / 16;
br = br + tmp * (255 - br) / 255;
}

lightmap[xx + yy * 240] = br;
}

``````

Whew. That source is some seriously dense old-school coding. You packed every bit of code you could into that 4K… you da man… 8)

doy ou know of any tutorials or is the one you plan ot write gunna be the only one

Yeah, that looks like the line tracing. The loop outside that is where it find the target pixels (xt, yt).

Look for articles on Roguelike Line of Sight. Most of them describe the broken raycasting method, but it might work.

There are MUUUCH more efficient ways to do shadowcasting based on geometry if you store it as line data instead of a bitmap. Going per pixel only really works for very low resolutions.

I think this is the method oyu were refering to. Tehre for some reason is some sqaure blcoking the screen.

Here’s an interesting round-up of some well known methods and their corner cases: http://roguebasin.roguelikedevelopment.org/index.php?title=Comparative_study_of_field_of_view_algorithms_for_2D_grid_based_worlds

Interesting that no algorithm presented is suitable in all situations - you’d think people would have cracked it by now.

thx, that looks very informative. I will have to take a deeper look ;D

EDIT: from what I could see though, there were no algorithms at all, or is that what you mean by they are not suitable for all situations?

Links to the actual algorithms are at the bottom of the page (and there’s a few more lurking in the wiki too).

But what I found surprising was that out of the “well known” algorithms none of them seem to produce the desired output in all cases.

the links at the botto are pretty good.

except that the shadowcasting one has the slop formula wrong :P.

it says it is x2-x1/y2-y1 it is actually y2-y1/x2-x1

but it is great besides that. Thx for the link once again

EDIT: I finished this tutorial. And I think that I understand it (I think lol). I am going to try this out crossing fingers

Well, it’s a wiki - edit it and fix it!