Realistic Lighting in a 2D game

I did some flashlight effect in J2D for my unfinished entry at LD48.

In pseudo code:


define cone of light;
use cone for clipping;
draw scene;
define gradient (transparente to black) in the direction of light
paint the cone with the gradient;

The result is something like this image:

http://www.demegames.cl/images/screen1.png

Then I added the same logic for a square around the player, and some options to control the ligh focus(distance v/s spread) getting this:

http://www.demegames.cl/images/screen2.jpg

http://www.demegames.cl/images/screen3.jpg

Don’t know how is the performace in old machines or old JVM, but in my PC (athlon 64 3000Mhz, 1GB DDR2, nVidia7600GS 256 MB, jdk1.6_XX) it works fine.

hey rdcarvallo, you wouldnt happen to mind sending me/ posting the source code for that would you?

thank you for the algorithm

Here goes the code:


//g2 is a Graphics2D object
//p is a Player object
//map is a Map(TileMap) object
Arc2D.Double arc = new Arc2D.Double();
arc.setArcByCenter(p.getLightX(), p.getLightY(), radius, p.getAngle()-spread, 2*spread, Arc2D.PIE);
GradientPaint gp = new GradientPaint(p.getLightX(),p.getLightY(),new Color(0,0,0,0),
    p.getLightX()+radius*(float)Math.cos(Math.toRadians(p.getAngle())),p.getLightY()-radius*(float)Math.sin(Math.toRadians(p.getAngle())),new Color(0,0,0));
g2.clip(arc);
map.drawTo(g2);
g2.setPaint(gp);
g2.fill(arc);
g2.draw(arc);
g2.setClip(null);

And the Demo, could you post how it runs on your machines?

http://www.demegames.cl/demos/Journey08.jar
Controls:
LEFT, RIGHT: walk
UP, DOWN: flashlight angle
Z, X: flashlight focus
A: Run
S: Jump
Space: Pause/MiniMap

It freezes right away:P

windows XP

same here, windows XP also

the JVM doesnt even start (the small java icon at the bottom right of the screen…)

Strange… I tested in my home (WinXP SP2, JDK1.6_XX).

I’ll recompile for 1.4 and upload again later, now I’m at work.

that wont help, i have 1.6.

the problem is that the main screen appears, but then I get stuck there…

the secret is to press s !!!

Ha… Press “S” to start ::), thanks Hansdampf!!

i must be retarded

hehe yeah me too.

umm, am I supposed to just fall eternally??

Doh!

I exported the project directly to a jar, but I had the resources hard-coded (d:/eclipse/workspace…) in the map loading code. In my PC the jar worked, since the resources were in place.

Here goes a version fixed and compliled for jdk1.4 http://www.demegames.cl/demos/Journey081.jar

Next version goes to the showcase.

to me its more like floating in the middle of an ocean, no land at sight
(out of topic for last time , promise )

If the Lighting is killing your performance, you can make the layer with the mult objects lower res without any problems. 1024x768 mainlayer, 160x120 light layer.

Congratulations on necroing a 1188 day old thread.

;D

Its in the wiki… :slight_smile:

Hi!
I’m using JOCL (OpenCL binding for Java) and my own algorithms as OpenCL-C kernels or just java parallel stream of int[] array of pixels. This is the effects:
some tests:



and final results in game:

Congratulations on necro’ing a thread that was last posted on 1,829 days ago.

Yeah, ‘you guys’ didn’t like the automated old-topic locking :point: