Easy lightning (and chunk system)

Hello there,

I’m currently working on a game that’s using squares and it is in 2D. However I want to make it a bit more realistic and maybe larger… So I got some small questions how I can make some smooth lightning that isn’t really flat and maps that aren’t at maximum 200*200(blocks not pixels, 1 block = 13pixels.)

Lightning System
For the lightning system I tried to do something with variables and just a simple lightning from above. I made the lightning with the fillRec functie and the Color functie the color is basicly (0,0,0,lightning) and in the level tick I made the lightning system like this:

[spoiler]

					try { //Lightning effect in the air and contact on ground.
						if(y != 0) {
							if(block[x][y].id == Tile.air && block[x][y - 1].id == Tile.air || block[x][y].id != Tile.air && block[x][y - 1].id == Tile.air) {
								block[x][y].lightning = block[x][y - 1].lightning;
							}
						}
					} catch(Exception e) { }
					
					try { //Lightning effect spreading on blocks that aren't air.
						if(y != 0) {
							if(block[x][y].id != Tile.air && block[x][y - 1].id != Tile.air && block[x][y - 1].lightning != 250 || block[x][y].id == Tile.air && block[x][y - 1].id != Tile.air && block[x][y - 1].lightning != 250) {
								block[x][y].lightning = block[x][y - 1].lightning + 50;
							}
						}
					} catch(Exception e) { }
					

[/spoiler]

As you can see, I basicly made it the if there’s a block above the block and that block isn’t equal to the id Tile.air that the lightning += 50. At my first opinion this was a great and easy idea to make a small but working lightning system. It now looks like this:
[spoiler]
http://img152.imageshack.us/img152/3415/lightningn.png
[/spoiler]
Some small things I saw and that were that the lightning on not fully squared blocks isn’t the best and the lightning isn’t really good when there’s a platform like this image:

http://img560.imageshack.us/img560/7279/23667768.png

And hints of tips are welcome, I know lightning doesn’t spread from top to bottom only but from left and right either.

Chunk System
Is there a good way to make your small chunks loop? like this small prototype map is 50*50 blocks and if you’re on block x 30 that it makes a new chunk/level on that place with a looping map not that there’s a mountain on that spot and 1 block left of it there are plains…

thanks for reading it already.
-RoseSlayer

For lighting, the light should be determined by relation to an above-ground air block, and if it is not adjacent to one, add 50 to the alpha for the shadow-map.

Also, why 13 px? Seems kind of random, and making a power of 2 (8,16,32,etc.) makes it easier for the computer to understand and will cause less issues in the future.

As for the chunks, I’m not too sure what you’re trying to accomplish. Are you tryin to repeat the same level, over and over again, infinitely? I’m not sure how to do that :confused: anyway, hope this helps a little bit.

Also, nice terraria sprite :wink:

Terraria sprites where for testing porpuse :smiley: glad you like it! But I was already going to make it 14x14 because that is better for liquids either you can’t do 13:2 so it isn’t the greatest random number that I had choosen. and for the power, that is a great option. I’m going to add that aswell. I think I am going to create an extra class for the lightning aswell. For the chunk system it’s great to make it all over again because it’s random generated so the new chunck will also be random generated etc. so repeating it might be an excellent idea.

Please, Please, PLEASE! spell it lighting.

But lightning is cool. And it emits light. xD

Haha! that’s true… I don’t know why I had called it lightning but lighting is a better word for it (probably because I’m horrible at my English)… Thanks but for my lightning question I solved it almost all. Is there only a way to set the “mask” of the rectangle as example you got a character and you can color it in with 1 color, how is this possible? also here’s a small picture:

http://img5.imageshack.us/img5/6568/21321312312.png

The question: It’s a rec… Is there anyway how the rectangle can “fit” the sprite?
Cause this looks awfull…

Already thanks!
-RoseSlayer
(P.S: It’s darker because it’s the back layer!)

It looks like you are just drawing a semi transparent black square over the tiles.

If this is not with java2d you can just give it a color value of like .5f, .5f, .5f Basically, scaling down rgb.

If it is with java2d then there are many hacks with AlphaComposites you can use to get fast lighting.

I actually found a way to almost colorize a sprite like opengl without losing acceleration.

Thanks for your reaction, I’m fearly new to programming but I know the basics etc. but however could your give me some more information about the .5f? how I use it as a code etc.

Thanks!
-RoseSlayer

It does look that way! Stop doing that, though. You can tint the sprite in Slick2d using

Color.grey.bind();

and in LibGDX using

spriteBatch.setColor(Color.grey);

It’s a floating point. You can (in most API’s) specify a color using three or four floating point values, representing the red, green, blue and sometimes alpha component of a color. They range between 0 and 1. 1 means full blown power of that color, and 0 means no color.

For instance,

0f, 0f, 1f

gives a nice blue color.

0f, 1f, 1f

gives a margenta color.

The last floating point (the alpha one), specifies how transparent the color is - also ranging between 0 and 1. 1 for no transparency, and 0 for full transparency.

0f, 0f, 1f, 0.5f

gives a nice see-through blue color.

This is just a way to specify color though. If you tint your images like I described above, it’ll tint everything you draw thereafter with the color you specified.

The same call again, with parameters

0f, 0f, 0f

will reset this, and you can continue drawing your sprites without having to worry about tinting.

Good explanation but to reset it do not set it to (0,0,0) as this should kill all color. It would be (1,1,1) to keep what ever the color of the sprite is. At least that is opengl, I do not know how slick does things under the covers.

Any time you render anything you can specify a tint or color. The default I always thought was (1,1,1,1) which is basically whatever the texture is. Note that I am referring to rendering textured quads/triangle Strips (sprites) in opengl and this does not all extend to 3D graphics.

This maybe sounds really stupid to you, but how to install Slick2d properly? I used: http://code.google.com/p/gate541/wiki/SlickAndEclipse but it doesn’t seems to work, still gives an error at importing Slick2D…

Thanks
-RoseSlayer