4

I have a lighting system already working and (mostly) functional. I'm trying to implement translucency so that certain blocks (like water) only remove a fraction of light passing through them, and it works, but it stops working at the whole number limit of the lightmap (in my case, 20).

This is what it looks like:
translucency glitch

and here is my code:

for(int x=0;x<mapX;x++){
for(int y=mapY-1;y>-1;y--){
    try{
       if(map[x][y] instanceof LightSource)
                    lightmap[x][y]=19;
       else{
          else{
                if(x-1>-1&&lightmap[x][y]<lightmap[x-1][y])
                     lightmap[x][y]=lightmap[x-1][y]-map[x][y].translucency;
                if(x+1<lv.map.length-1&&lightmap[x][y]<lightmap[x+1][y])
                     lightmap[x][y]=lightmap[x+1][y]-map[x][y].translucency;
                if(y+1<lv.map[0].length-1&&lightmap[x][y]<lightmap[x][y+1])
                     lightmap[x][y]=lightmap[x][y+1]-map[x][y].translucency;
                if(y-1>0&&lightmap[x][y]<lightmap[x][y-1])
                     lightmap[x][y]=lightmap[x][y-1]-map[x][y].translucency;
            }
      }
  }catch(Exception e){}
}
}

Does anyone see what I'm doing wrong? Is there another system I could use to support transparency? Can anybody tell me at least why this is happening?

Community
  • 1
  • 1
Mike Fitz
  • 101
  • 1
  • 9
  • 1
    What do you mean by "stops working?" Stops working in what way? – BillRobertson42 Sep 14 '17 at 16:24
  • 1
    You're eating any `Exception` that may be thrown. The first step in troubleshooting this problem is logging the exceptions or using a simple `e.printStackTrace()`. Once you've confirmed there are no exceptions then focus on the code. – Paul Sep 14 '17 at 16:25
  • I really dont think the exceptions are the problem. They exist to prevent the script from failing at the edges of the map. If I print the stack trace, it will just give me a myriad of `ArrayIndexOutOfBoundsException`s around the edges. – Mike Fitz Sep 14 '17 at 16:49
  • And it stops working by setting the light values of the water to 0 in places they shouldn't be, as shown by the graphic. – Mike Fitz Sep 14 '17 at 16:49
  • It's a `float[][]`. The value is cast to `int` before being output as light. `translucency` is also a `float`, being 1 for most blocks and 0.2 for water. – Mike Fitz Sep 14 '17 at 17:00
  • I removed the unnecessary exception catching. I put a .printStackTrace() in the one that is necessary (that wraps around the whole script), but an Exception isnt even thrown in this case, because it's buffering the entire lightmap at `GameState` initiation. That `catch` is only necessary for updating the lightmap. – Mike Fitz Sep 14 '17 at 17:04

1 Answers1

1

I fixed it by changing for(int y=mapY-1;y>-1;y--) to for(int y=0;y<mapY-1;y++). Absolutely no idea why that worked, though. Also, I'm afraid this same system might yield similar results if the light was coming from the bottom up instead of the top down. Haven't tested that yet, but it would make sense since all I did was change the order of the cellular automata.

Mike Fitz
  • 101
  • 1
  • 9