Realistic Lighting in a 2D game

We are talking about a platformer here, imagine Castlevania.

And what I would like to create is Lightning that is really realistic and reflective.
I mean a room, there is a lightsource, objects in the room are lightend the way the should.

If you create just a tile based game, there is not much lightning… but if you were to insert lightsources and stuff like that.

imagine even a broken lamp that flickers. that would really increase authentic and atmosphere

Was something like that ever done / is there even a game like that ?
how would you do it ?
are there “tricks” ?

Depends on your technology, however I’ve found calculating vertex colours of the tiles when using OpenGL how some great effects.


I do per-pixel lighting in Rescue Squad 2. Basically there’s three passes, first a full bright pass consisting of the visible sprites:

Then there’s a lightmap pass where sprites and other light geometry are drawn to:

Both of those are captured to a texture, and the final pass just multiplies the two together:

The nice thing about this is that lights can be any shape (like the search light’s cone of light) and quite detailed, since really they’re just another sprite. And it works on pretty much any pc hardware too.

ya but how do you subtract darkness from the lightning map

I mean, most likely you would do it kinda professional =D
I would take a black color with an alpha value which is overall very high, and very low at lightsources

which means, since I’m using Java2D Graphics.setColor and fill methods
only problem is that I cant render a square which a oval round which is empty =P
and I dont even wanna think about reflection in this regard

But you don’t have to. :slight_smile: If you do it like Orangy Tang explained, you can additively combine lights on top of an ambient light, and finally multiply the whole shebang with the unlit scene. You also get colored lights for free with this technique, unlike when you subtract alpha from black.

Although if you’re stuck with Java2d then that’s an entirely different kettle of fish - I doubt that method would run fast enough (although I’d be interested in seeing how fast it does run).

If you’re stuck with j2d, then your best bet is to do the lighting manually per-pixel in a big working buffer - I believe L4kD does this so you might want to poke around the source for that. But you’ll probably be limited to quite low resolutions with that method.

I just looked into L4kD

seems awfully tedious to do in j2D
Overall I’m not that good of a programmer to do it like he did.

How difficult / how much is it to “convert” a j2d game in JOGL or slick or whatever you guys would suggest ?
I mean of course that depends on my game, but afterall its just the rendering processes which isnt as much

Its probably pretty quick to convert to slick since the api is very similar to java2d. However if you decide to use JOGL or LWJGL you’ll have to use the opengl api which will take longer to convert too.

If you still want software rendering you could try pulpcore:
It might have what your looking for.

Try this java2d library:

It has additive blending, and it can be accelerated by the java2d d3d pipeline.

I’ve used it once just to test it out and it worked pretty good, but I didn’t use additive blending, i just used the bloom effect.

Where’s more info about this library?
I couldn’t find much by searching for it…

Yeah it’s pretty hard to track it down. It’s geared up for javafx so it’s gone under the radar as a java2d API so far…

Check out this:

why cant this multiply be done in j2d?? without considering the performance
can someone plz give me a hint or link to the math behind this (pixel by pixel) multiplication??

thank you in advance

I’m actually trying to create that affect. =) But, I’m not in that part of my development yet. I’ve always enjoyed the retro look of sprites, and I wanted to meld this with realistic lighting and shadow affects.

The j2d api only* provides out-of-the-box functionality for compositing using the alpha channel, not for colour compositing. You can easily implement your own custom ColorComposite**, but it obviously won’t be hw accelerated, so will be all-but useless in relation to performance.

I’m sure there is an RFE somewhere in the bug database on the subject, as its absence significantly restricts the usefulness of j2d for many interesting effects.

(* Technically there is one color composite operation supported; XOR mode. It’s a legacy bit of functionality from the early days of Java; it doesn’t use the more modern Composite interface. Also I remember reading somewhere that it doesn’t work [very well/at all] in the hw pipeline.)

( ** Though you also need to be aware you will run into all sorts of problems with custom composites if you try and use them when drawing onto a VolatileImage or BufferStrategy.)

ok, so i wont use it in a game,

but i asked “without considering the performance, can someone give me a hint or link or something about the math involved in this type of ColorComposition?”

i mean, does it just uses the src and dest pixel and " +" , or “*”, or some binary operator, or… ?

thank you

Each channel of each pixel in the source is multiplied with the corresponding channel of each pixel in the destination, and the result is placed in the destination.

Colorresultant = (Rsrc*Rdst,Gsrc*Gdst,Bsrc*Bdst)

A quick google found this, which in turn pointed to this

btw, I guess everybody has noticed but been too polite to mention the mistake in the original question?

The two are very different problems =) (though not entirely unrelated!)

thank you very much for your reply and links

I guess nobody noticed. Fixed.

@ Topic, this is all above my head at this point.

if the values are float, then i suppose this works, (with a step of "1.0 (minus) “final result” " perhaps?)
if the values of the three channels are integers, i cant seem to work it out
for example, a light red, value70, and the light source with value 200. multiplying them, the final result is out of the permitted range [0,255],
so i guess there are two options:
the multiplication is a logical AND
or the programmer decides upon the ratio of the two values (destination, light), perhaps 3/7 meaning that the destination number will be multiplied by 0.3 and the light number by 0.7, bigger values for the destination mean that the difference between illuminated areas and non-illuminated will be smaller…

i believe i should make a code example to prove it (to myself mainly, and to all who are interested secondly)… damned university, taking all of my sober time away…