Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Tiled Based Lighting not working up and left  (Read 747 times)
0 Members and 1 Guest are viewing this topic.
Offline chrislo27

Junior Newbie





« Posted 2013-04-06 20: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);
     }
     
   }
Offline matheus23

JGO Wizard


Medals: 96
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2013-04-06 20: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 Smiley
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 Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline chrislo27

Junior Newbie





« Reply #2 - Posted 2013-04-07 00:52:35 »

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

This really helps! Thanks! Cheesy

EDIT: sorry, layer2[][] is a block layer, and each block has a lightvalue of how much they give off.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline chrislo27

Junior Newbie





« Reply #3 - Posted 2013-04-07 00: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 Tongue
EDIT: got a ton of errors of ArrayIndexOutOfBounds -1, and sometimes StackoverFlow errors.
Online HeroesGraveDev

JGO Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


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

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

Offline chrislo27

Junior Newbie





« Reply #5 - Posted 2013-04-07 01:18:25 »

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

JGO Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« Reply #6 - Posted 2013-04-07 01: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

Offline chrislo27

Junior Newbie





« Reply #7 - Posted 2013-04-07 01: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.
Offline chrislo27

Junior Newbie





« Reply #8 - Posted 2013-04-07 02: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.)?
Offline chrislo27

Junior Newbie





« Reply #9 - Posted 2013-04-07 03: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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Wizard


Medals: 96
Projects: 3


You think about my Avatar right now!


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

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

Argh... I wrote
lightValue > 0
instead :/ My mistake...

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.... Cheesy 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (10 views)
2014-04-15 03:46:01

UprightPath (24 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (107 views)
2014-04-08 23:06:04

tom_mai78101 (207 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!