Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  how to do left 4k dead visual effect  (Read 4937 times)
0 Members and 1 Guest are viewing this topic.
Offline h3ckboy

JGO Coder


Medals: 5



« Posted 2009-08-17 10:15:53 »

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

thx in advance,
h3ckboy
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #1 - Posted 2009-08-17 12:53:33 »

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

Play Minecraft!
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #2 - Posted 2009-08-17 19:29:48 »

is it really complicated?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #3 - Posted 2009-08-17 20:00:28 »

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

Play Minecraft!
Offline ManaSink

Senior Newbie





« Reply #4 - Posted 2009-08-17 20:14:00 »

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.

Is that this chunk?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
                            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....   Cool
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #5 - Posted 2009-08-17 20:21:38 »

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.)
doy ou know of any tutorials or is the one you plan ot write gunna be the only one Tongue
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #6 - Posted 2009-08-18 08:52:47 »

Is that this chunk?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
                            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....   Cool

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

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

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.

Play Minecraft!
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #7 - Posted 2009-08-18 10:30:01 »

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

http://groups.google.com/group/rec.games.roguelike.development/browse_thread/thread/75b862c2c9316d64?pli=1
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #8 - Posted 2009-08-18 10:41:13 »

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

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #9 - Posted 2009-08-18 11:51:05 »

Ooh, nice link. =D

Play Minecraft!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #10 - Posted 2009-08-18 12:59:20 »

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

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

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?
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #11 - Posted 2009-08-18 13:09:02 »

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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #12 - Posted 2009-08-18 19:35:31 »

the links at the botto are pretty good.

except that the shadowcasting one has the slop formula wrong Tongue.

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*
Offline ido

Junior Duke





« Reply #13 - Posted 2009-09-11 20:05:07 »

except that the shadowcasting one has the slop formula wrong Tongue.

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


Well, it's a wiki - edit it and fix it!  Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (67 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!