Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (604) Games in Android Showcase (171) games submitted by our members Games in WIP (654) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Tiled Based Lighting not working up and left  (Read 1325 times) 0 Members and 1 Guest are viewing this topic.
chrislo27
 « Posted 2013-04-06 18:24:50 »

I have an issue where my tile based lighting code is not functioning properly going up and left. It works properly going right and downward (see image), but only goes one tile to the left/up and doesn't continue like it should. http://i.imgur.com/DAM1pg2.png

My code seems correct; I have placed print statements to check values (removed in sample) and it seems alright.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88 `public void light(){                  for(int i = 0; i < limity; i++){         for(int x = 0; x < limitx; x++){            light[i][x] = 0; //darken to current time of day         }      }            int ty = (p.desiredy/32)-16;      int tx = (p.desiredx/32)-16;      if(ty > limity){         ty = limity - 1;      }      if(ty < 0){         ty = 0;      }      if(tx > limitx){         tx = limitx - 1;      }      if(tx < 0){         tx = 0;      }      int my = ty + 32;      int mx = tx + 32;      if(my > limity){         my = limity - 1;      }      if(my < 0){         my = 0;      }      if(mx > limitx){         mx = limitx - 1;      }      if(mx < 0){         mx = 0;      }            for(int by = ty; by < my; by++){         for(int bx = tx; bx < mx; bx++){//for each block in the area            if(layer2[by][bx].lightlevel > 0){ //for each LIGHT SOURCE               light[by][bx] = layer2[by][bx].lightlevel;                           }else light[by][bx] = 0;         }      }                        for(int by = ty; by < my; by++){         for(int bx = tx; bx < mx; bx++){//for each block in the area, do adjacent check                        if(light[by][bx] > 1){                              //now do blocks adjacent               //left, right, up, down               //must do a check for blocks, otherwise exception               //then check if block is DARKER than itself               if(bx - 1 != -1){                  if(light[by][bx - 1] < light[by][bx]){                                          light[by][bx - 1] = light[by][bx] - 1;                  }               }               if(by - 1 != -1){                  if(light[by - 1][bx] < light[by][bx]){                     light[by - 1][bx] = light[by][bx] - 1;                  }               }                              if(bx + 1 != limitx){                  if(light[by][bx + 1] < light[by][bx]){                     light[by][bx + 1] = light[by][bx] - 1;                  }               }                              if(by + 1 != limity){                  if(light[by + 1][bx] < light[by][bx]){                     light[by + 1][bx] = light[by][bx] - 1;                  }               }                           }         }         //System.out.println("end of y loop, " + ty + " " + my);      }         }`

matheus23

JGO Kernel

Medals: 122
Projects: 3

You think about my Avatar right now!

 « Reply #1 - Posted 2013-04-06 18:34:58 »

Phew... Can't read that code that well... I suggest to use recursive methods for that purpose, just how I did it with WorldOfCube.

So it looks like you have these resources:
 1  2 `private int[][] light;private int[][] light2; // Wow... light values are stored here? Okey...`

I don't know what your
 `int tx, ty`
and
 `int mx, my`
are, so I'll just skip them.
So lets go on building the recursive function. It's gonna be readable much better
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `public void lightSource(int x, int y, int lightValue) {    if (light[x][y] >= lightValue && lightValue > 0) { // Well, the light has already been set here, we can stop here.        return;    }    light[x][y] = lightValue;    lightValue -= darkenAmmount; // need to implement yourself    // recursiveness:    lightSource(x-1, y, lightValue);    lightSource(x+1, y, lightValue);    lightSource(x, y-1, lightValue);    lightSource(x, y+1, lightValue);}public void light() {    // for each light source:    lightSource(x, y, brightness);}`

Hope this helps

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
chrislo27
 « Reply #2 - Posted 2013-04-06 22:52:35 »

Thanks! The very long code section was checking boundries for the world limit >.<

This really helps! Thanks!

EDIT: sorry, layer2[][] is a block layer, and each block has a lightvalue of how much they give off.

chrislo27
 « Reply #3 - Posted 2013-04-06 22:56:41 »

I implemented the code and it only seems to make a ray of light going up. I'll look at it again, and fix it
EDIT: got a ton of errors of ArrayIndexOutOfBounds -1, and sometimes StackoverFlow errors.

HeroesGraveDev

JGO Kernel

Medals: 360
Projects: 11
Exp: 3 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

 « Reply #4 - Posted 2013-04-06 23:09:07 »

@matheus23: You forgot to stop recursion when lightValue <= 0.

chrislo27
 « Reply #5 - Posted 2013-04-06 23:18:25 »

Also, my arrays go [Y][X], and not [X][Y]

HeroesGraveDev

JGO Kernel

Medals: 360
Projects: 11
Exp: 3 years

┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻

 « Reply #6 - Posted 2013-04-06 23:29:06 »

Then you should probably make it go [X, Y] like everyone else to avoid confusion.

http://en.wikipedia.org/wiki/Cartesian_coordinate_system#Notations_and_conventions

chrislo27
 « Reply #7 - Posted 2013-04-06 23:30:42 »

Works all fine and dandy, also fixed the lightValue <= 0 thing, but now it looks like this:

I also cut out the boundary checker here, but it's still in the code (to reduce lines)
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52 `public void lightSource(int x, int y, int lightValue) {      if(lightValue <= 0) return;      if(x == -1 && x > limitx && y == -1 && y > limity){         //System.out.println("out of bounds " + x + " " + y + " " + limitx + " " + limity);         return;      }       if ((light[y][x] >= lightValue) && lightValue > 0) { // Well, the light has already been set here, we can stop here.          // System.out.println("light already set");          return;       }             light[y][x] = lightValue;       lightValue -= 1; // need to implement yourself       // recursiveness:                   if(x - 1 != -1){         lightSource((x - 1), y, lightValue);      }      if(y - 1 != -1){         lightSource(x, (y - 1), lightValue);      }            if(x + 1 != limitx){         lightSource((x + 1), y, lightValue);      }            if(y + 1 != limity){         lightSource(x, (y + 1), lightValue);      }          }   public void light() {       // for each light source:      for(int i = 0; i < limity; i++){         for(int x = 0; x < limitx; x++){            light[i][x] = 0; //darken to current time of day         }      }                        for(int by = ty; by < my; by++){         for(int bx = tx; bx < mx; bx++){//for each block in the area            if(layer2[by][bx].lightlevel > 0){ //for each LIGHT SOURCE               lightSource(bx, by, layer2[by][bx].lightlevel);                           }else light[by][bx] = 0;         }      }   }`

I'm guessing it has something to do with the return statements not processing the rest of the lighting.

chrislo27
 « Reply #8 - Posted 2013-04-07 00:39:49 »

This is just an optional question, but is there a way to make the framerate not so pathetic (used to be about 80 fps, now with lighting is 11 fps avg.)?

chrislo27
 « Reply #9 - Posted 2013-04-07 01:33:29 »

I have solved the issue. it might be a temporary one, but it works! I just put my original code after the recursive code, and it seems to work. I'll work out a better solution later.

matheus23

JGO Kernel

Medals: 122
Projects: 3

You think about my Avatar right now!

 « Reply #10 - Posted 2013-04-07 08:19:07 »

@matheus23: You forgot to stop recursion when lightValue <= 0.

Argh... I wrote
 `lightValue > 0`

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21 `public void lightSource(int x, int y, int lightValue) {    if (x < 0 || y < 0 || x >= limitx || y >= limity) {        // out of bounds        return;    }    // Fixed the > 0, which was wrong and supposed to be <= 0, as gravedev pointed out...    if ((light[y][x] >= lightValue) && lightValue <= 0) {        // Well, the light has already been set here, we can stop here.        return;    }        light[y][x] = lightValue;    lightValue -= 1;    // recursiveness:           // No need to check positions anymore: (I think)    lightSource((x - 1), y, lightValue);    lightSource(x, (y - 1), lightValue);    lightSource((x + 1), y, lightValue);    lightSource(x, (y + 1), lightValue);}`

This is just an optional question, but is there a way to make the framerate not so pathetic (used to be about 80 fps, now with lighting is 11 fps avg.)?

Oh yeah.... I faced this issue myself A LOT. Well, it was pretty hard to get it working fast. I've used Threading and only processed visible chunks of my world. So yeah, it was possible to have the game itself run at about ~200fps and the lighting at ~80... but it was very, very hard. Try to optimize it somehow in another way. Threading will definitely help you, but is probably not so easy.

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 bilznatch (27 views) 2015-08-04 11:03:17 SHC (44 views) 2015-08-01 03:58:20 Jesse (25 views) 2015-07-29 04:35:27 Riven (48 views) 2015-07-27 16:38:00 Riven (26 views) 2015-07-27 15:35:20 Riven (28 views) 2015-07-27 12:26:13 Riven (19 views) 2015-07-27 12:23:39 BurntPizza (42 views) 2015-07-25 00:14:37 BurntPizza (56 views) 2015-07-24 22:06:39 BurntPizza (35 views) 2015-07-24 06:06:53
 theagentd 50x wessles 48x basil_ 33x KaiHH 26x Riven 21x mooman219 17x orangepascal 17x ags1 16x bornander 16x KudoDEV 13x CelestialCreator 11x princec 11x klaus 11x pquiring 11x israelg99 9x philfrei 8x
 List of Learning Resourcesby gouessej2015-07-09 11:29:36How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21
 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