Slick Forums

Discuss the Slick 2D Library
It is currently Fri May 24, 2013 3:34 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Lighting TiledMaps?
PostPosted: Fri Apr 13, 2012 3:42 am 
Offline

Joined: Fri Apr 13, 2012 3:27 am
Posts: 3
I'm working on a dungeon crawler game that uses TiledMaps for the levels. I've looked at a few threads and examples with tile based lighting, but couldn't figure out how to get them to work with TiledMaps. I don't know all that much about how lighting systems work, so an example or explanation of how to do it would be very helpful.


Top
 Profile  
 
 Post subject: Re: Lighting TiledMaps?
PostPosted: Fri Apr 13, 2012 11:25 am 
Offline
Slick2D Developer

Joined: Thu Mar 31, 2011 5:06 am
Posts: 239
Here is my old working lighting code that I used with TiledMaps. If you want anything more precise, check out the source of kev's warpstone, or even more, look into Lighting via Shaders.

_________________
Liam (liamzebedee) Edwards-Playne
Cryptum


Top
 Profile  
 
 Post subject: Re: Lighting TiledMaps?
PostPosted: Wed Apr 18, 2012 10:17 pm 
Offline

Joined: Fri Apr 13, 2012 3:27 am
Posts: 3
Like I said, I don't know very much about how lighting systems work and your code is a bit hard to understand without comments. An explanation of what your code does would help a lot. As far as using shaders, I have no idea how to do that (I'm fairly new to Slick and game development in general).

Edit: So I looked through the code and (mostly) figured it out. I made it a bit more dynamic and got line of sight obstructions to work. However, I have a new problem: I want the light to go past obstructions a bit before fading, like in Terraria (see screenshot). I tried changing some numbers around, but couldn't get it to work. Any ideas on how to do this?

Images:
Terraria - Note how the light goes several tiles past the walls before disappearing:
Image

Mine - Light only goes one tile and disappears (grey tiles are "obstructions", and block light):
Image


Top
 Profile  
 
 Post subject: Re: Lighting TiledMaps?
PostPosted: Fri Apr 20, 2012 2:26 am 
Offline

Joined: Sun Jan 02, 2011 8:39 pm
Posts: 64
You'll want to focus on the part of your code that deals with grey "obstructed" tiles. You can see that 1 row of grey tile is lighted if you can figure out the segment of code that lights that 1 row then you could, hopefully, modify it to extend its radius based on it's range setting.

Edit: Terraria's lighting doesn't use obstruction its just range based. If you eliminate the obstruction code then you will have a basic light with a range.


Top
 Profile  
 
 Post subject: Re: Lighting TiledMaps?
PostPosted: Fri Apr 20, 2012 6:59 am 
Offline
Slick2D Developer

Joined: Thu Mar 31, 2011 5:06 am
Posts: 239
In this case you want to edit the LOS (Line of sight code) to use a static variable (say 8)

_________________
Liam (liamzebedee) Edwards-Playne
Cryptum


Top
 Profile  
 
 Post subject: Re: Lighting TiledMaps?
PostPosted: Mon Apr 23, 2012 10:52 pm 
Offline

Joined: Fri Apr 13, 2012 3:27 am
Posts: 3
I actually got it to work by only changing only a few lines of code:

Before/after screenshot:
http://i.imgur.com/biqbG.png

Toward the bottom of the update() method, where line of sight obstructions are checked:
Code:
int div = 1;
// Check objects, could use optimization, should not need to check them all?
for (Rectangle r : obstructions)
{
   if (lineOfSightLine.intersects(r))
   {
      div++; //Add one to "div" for each obstruction that is blocking this tile
   }
}

float[] effect;

for (int component = 0; component < 3; component++)
{
   // check effect on each corner
        // Divide the effect by "div" to make the tile darker

   // top left
   effect = light.getEffectAtCorner(new Vector2f(x * tileSize, y * tileSize), colouredLights);
   lightValue[x][y][0][component] += effect[component]/div;

   // top right
   effect = light.getEffectAtCorner(new Vector2f(x * tileSize + tileSize, y * tileSize), colouredLights);
   lightValue[x][y][1][component] += effect[component]/div;

   // bottom left
   effect = light.getEffectAtCorner(new Vector2f(x * tileSize,   y * tileSize + tileSize), colouredLights);
   lightValue[x][y][2][component] += effect[component]/div;

   // bottom right
   effect = light.getEffectAtCorner(new Vector2f(x * tileSize + tileSize, y * tileSize + tileSize), colouredLights);
   lightValue[x][y][3][component] += effect[component]/div;
}


It's probably a slow, inefficient way to do it, but it does give the desired effect (for the most part). It needs optimization, using larger TiledMaps murders the frame rate. The update() method takes the longest, but the render() method gets slower as more tiles are added. Any ideas on how to speed this up? I can post the full source code if necessary, but its mostly the same as the example code that liamzebedee posted earlier.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group