Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (806)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  Tile by tile lighting how would i do this  (Read 18788 times)
0 Members and 1 Guest are viewing this topic.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #30 - Posted 2015-06-22 19:57:40 »

How about just a linear interpolation?

1  
2  
3  
4  
5  
double maxDistance = 4.0;

...

alpha =  Math.max(0, 1 - distance / maxDistance)


https://www.desmos.com/calculator/2okgm0p5gl

EDIT: you can also try messing around with the curvature for various fall-off types: https://www.desmos.com/calculator/jfi7kcyu9j
Offline tommohawkaction
« Reply #31 - Posted 2015-06-22 20:19:17 »

Wow that is a better way... how does it work???

Also I am still having the same problem as before where if i have a torch about 3 tile away from another torch one of those tiles will be completely dark doesnt look nice
Offline tommohawkaction
« Reply #32 - Posted 2015-06-22 20:22:32 »

Just in case it is hard to visualize

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #33 - Posted 2015-06-22 20:26:11 »

You aren't summing the lights together.

Lights pseudocode in pretty much any framework:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
for each tile visible {
     tileBrightness = ambient; // ambient = 0 if you want total darkness outside of lights
   
    for each light visible {
        lightValueAtDistance = /* interpolation from before */
        tileBrightness += lightValueAtDistance;
    }
   
    // probably clamp tileBrightness here
   
    drawTile(tile, tileBrightness);
}



EDIT: example, here's a system of two lights, at positions p1 and p2: https://www.desmos.com/calculator/ua4b5gab8f
Offline tommohawkaction
« Reply #34 - Posted 2015-06-22 20:37:35 »

Well



Code
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  
// Lights
         spriteBatch.begin();
         {

            for (int x = startX; x < toX; x++) {
               for (int y = startY; y < toY; y++) {
                  if (x < 0 || x > TileRenderer.map.getWidth() - 1 || y < 0 || y > TileRenderer.map.getHeight() - 1)
                     continue;

                  float tileBrightness = sunlight.a;

                  for (int i = 0; i < lights.size; i++) {

                     MasterTile tile = lights.get(i);
                     if (tile == null)
                        continue;
                     float distance = Vector2.dst(tile.getX()/MasterTile.TILE_WIDTH,tile.getY()/MasterTile.TILE_HEIGHT, x, y);
                     tileBrightness += Math.max(0, distance / MAX_DISTANCE);

                  }
                  Color tileColor = Color.WHITE;
                  tileColor.a = tileBrightness;

                  spriteBatch.setColor(tileColor);
                  spriteBatch.draw(pixelTexture, x * MasterTile.TILE_WIDTH, y * MasterTile.TILE_HEIGHT, MasterTile.TILE_WIDTH, MasterTile.TILE_HEIGHT);
                  spriteBatch.setColor(Color.WHITE);

               }
            }
         }

         spriteBatch.end();
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #35 - Posted 2015-06-22 20:41:29 »

1  
2  
-tileBrightness += Math.max(0, distance / MAX_DISTANCE);
+tileBrightness += Math.max(0, 1 - distance / MAX_DISTANCE);


You want an negative slope, brightness is inversely proportional to distance.

You also aren't clamping tileBrightness prior to assigning it to tileColor.a, although SpriteBatch might do it anyway, I don't remember.
Offline tommohawkaction
« Reply #36 - Posted 2015-06-22 20:47:24 »

What do you mean by "clamping tileBrightness"?
and it kinda works but the light is going outside in so it is dark in the middle and light going outwards?
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #37 - Posted 2015-06-22 20:50:41 »

Clamp in this context: to limit to a range

1  
tileColor.a = Math.min(1, tileBrightness); // don't exceed 1.0



The inside-out lighting is because of the missing '1 - ...' in 1 - distance / MAX_DISTANCE

Try taking it out of the graphs I linked and see what happens.
Offline tommohawkaction
« Reply #38 - Posted 2015-06-22 20:59:48 »

Well i changed it on the graph website and it is giving a positive number but when attempting it in the code it doesn't work

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  
            for (int x = startX; x < toX; x++) {
               for (int y = startY; y < toY; y++) {
                  if (x < 0 || x > TileRenderer.map.getWidth() - 1 || y < 0 || y > TileRenderer.map.getHeight() - 1)
                     continue;

                  float tileBrightness = sunlight.a;

                  for (int i = 0; i < lights.size; i++) {

                     MasterTile tile = lights.get(i);
                     if (tile == null)
                        continue;
                     float distance = Vector2.dst(tile.getX() / MasterTile.TILE_WIDTH, tile.getY() / MasterTile.TILE_HEIGHT, x, y);
                     tileBrightness += Math.max(0, distance / MAX_DISTANCE);

                  }
                  tileBrightness = Math.min(1, tileBrightness);
                 
                  Color tileColor = Color.WHITE;
                      tileColor = Utils.mixColours(tileColor, sunlight, sunlight.a);
                      tileColor = Utils.mixColours(tileColor, Color.WHITE, tileBrightness);

                  spriteBatch.setColor(tileColor);
                  spriteBatch.draw(pixelTexture, x * MasterTile.TILE_WIDTH, y * MasterTile.TILE_HEIGHT, MasterTile.TILE_WIDTH, MasterTile.TILE_HEIGHT);
                  spriteBatch.setColor(Color.WHITE);
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #39 - Posted 2015-06-22 21:01:52 »

I'll point it out very explicitly this time:

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  
for (int x = startX; x < toX; x++) {
               for (int y = startY; y < toY; y++) {
                  if (x < 0 || x > TileRenderer.map.getWidth() - 1 || y < 0 || y > TileRenderer.map.getHeight() - 1)
                     continue;

                  float tileBrightness = sunlight.a;

                  for (int i = 0; i < lights.size; i++) {

                     MasterTile tile = lights.get(i);
                     if (tile == null)
                        continue;
                     float distance = Vector2.dst(tile.getX() / MasterTile.TILE_WIDTH, tile.getY() / MasterTile.TILE_HEIGHT, x, y);
-                     tileBrightness += Math.max(0, distance / MAX_DISTANCE);
+                     tileBrightness += Math.max(0, 1 - distance / MAX_DISTANCE); // you really do need that 1 -

                  }
                  tileBrightness = Math.min(1, tileBrightness);
                 
                  Color tileColor = Color.WHITE;
                      tileColor = Utils.mixColours(tileColor, sunlight, sunlight.a);
                      tileColor = Utils.mixColours(tileColor, Color.WHITE, tileBrightness);

                  spriteBatch.setColor(tileColor);
                  spriteBatch.draw(pixelTexture, x * MasterTile.TILE_WIDTH, y * MasterTile.TILE_HEIGHT, MasterTile.TILE_WIDTH, MasterTile.TILE_HEIGHT);
                  spriteBatch.setColor(Color.WHITE);
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PlainBug
« Reply #40 - Posted 2015-06-22 21:03:06 »

Hi Smiley,
You can also make use of a 2D-array for the "shadow"-tiles that you will draw over the scene.
In this array you can store the alpha-value for each tile (by default the alpha-value should be 1f).
Then you calculate (with the help of the light's position) in which part of the array your light is.
After that you can decrease the alpha-value of the tiles next to your light and the tiles next to these tiles and so on...
Maybe like this:

1  
2  
3  
4  
5  
6  
7  
8  
if(tiles[x][y].getPosition() == light.getPosition()) {
    tiles[x][y].setAlpha(0f);
    tiles[x-1][y].setAlpha(0.5f);
    tiles[x+1][y].setAlpha(0.5f);
    tiles[x][y-1].setAlpha(0.5f);
    tiles[x][y+1].setAlpha(0.5f);
    // and so on..
}


When this works, you can try to create a recursive or even iterative algorithm for "the tiles next to the tiles next to the tiles and so on".
Another possibility is to make the background-colour black and then you can decrease the alpha-value of your normal tiles (cf. this)

Offline KaiHH

JGO Kernel


Medals: 798



« Reply #41 - Posted 2015-06-22 21:03:49 »

I'll point it out very explicitly this time:
Sorry, had to appreciate that post. Made me laugh. Cheesy
Offline tommohawkaction
« Reply #42 - Posted 2015-06-22 21:04:07 »

I have and I am still getting the same thing where it is dark in the light instead of being light

You said have a look on the graphics (take it out)
which i did and the results were positive

Quote
The inside-out lighting is because of the missing '1 - ...' in 1 - distance / MAX_DISTANCE

Try taking it out of the graphs I linked and see what happens
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #43 - Posted 2015-06-22 21:06:38 »

You can also make use of a 2D-array for the "shadow"-tiles that you will draw over the scene.
...

Let's not over-complicate things before tommohawk has a working naive solution.
Besides, the next step is using a light texture and additive blending instead of faking it with an array.
EDIT: actually I'm not sure what that method is trying to fake, but it sure isn't as robust as the simple way.
Offline tommohawkaction
« Reply #44 - Posted 2015-06-22 21:09:38 »




....... Huh Huh Huh Huh Huh Huh Huh Huh Huh
Offline tommohawkaction
« Reply #45 - Posted 2015-06-23 18:14:35 »

Hello thanks for the help from people but its day 4 now and i haven't figured it out yet



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  
if (ENABLED) {
         int camX = (int) (camera.position.x + (MasterTile.TILE_WIDTH / 2) - camera.viewportWidth / 2) / MasterTile.TILE_WIDTH;
         int camY = (int) (camera.position.y + (MasterTile.TILE_HEIGHT / 2) - camera.viewportHeight / 2) / MasterTile.TILE_HEIGHT;
         int camZoomFixX = (int) (((camera.zoom - 1) * 100) / (MasterTile.TILE_WIDTH / 10));
         int camZoomFixY = (int) (((camera.zoom - 1) * 100) / (MasterTile.TILE_WIDTH / 10));
         int offset = 1;

         if (camZoomFixX < 0)
            camZoomFixX = 0;
         if (camZoomFixY < 0)
            camZoomFixY = 0;

         int startX = camX - (camZoomFixX + offset);
         int toX = (camX + TileRenderer.AMOUNT_WIDTH) + (camZoomFixX + offset);
         int startY = camY - (camZoomFixY + offset);
         int toY = (camY + TileRenderer.AMOUNT_HEIGHT) + (camZoomFixY + offset);

         // Lights
         spriteBatch.begin();
         {

            for (int x = startX; x < toX; x++) {
               for (int y = startY; y < toY; y++) {
                  if (x < 0 || x > TileRenderer.map.getWidth() - 1 || y < 0 || y > TileRenderer.map.getHeight() - 1)
                     continue;

                  float tileBrightness = sunlight.a;

                  for (int i = 0; i < lights.size; i++) {

                     MasterTile tile = lights.get(i);
                     if (tile == null)
                        continue;
                     float distance = Vector2.dst(tile.getX() / MasterTile.TILE_WIDTH, tile.getY() / MasterTile.TILE_HEIGHT, x, y);
                     tileBrightness += Math.max(0, 1 - distance / MAX_DISTANCE);
                     

                  }
                  tileBrightness -= 2;
                  tileBrightness = Math.min(1, Math.abs(tileBrightness));
                  System.out.println(tileBrightness);
                 
                  Color tileColor = Color.WHITE;
                      tileColor = Utils.mixColours(tileColor, Color.WHITE, tileBrightness);
                      tileColor = Utils.mixColours(tileColor, sunlight, sunlight.a);
                     
                     

                  spriteBatch.setColor(tileColor);
                  spriteBatch.draw(pixelTexture, x * MasterTile.TILE_WIDTH, y * MasterTile.TILE_HEIGHT, MasterTile.TILE_WIDTH, MasterTile.TILE_HEIGHT);
                  spriteBatch.setColor(Color.WHITE);

               }
            }
         }

         spriteBatch.end();
      }
Offline tommohawkaction
« Reply #46 - Posted 2015-06-23 18:19:02 »

OMFG been doing tests and I thought to myself as i was getting darker colors that i would change the + to - in this equation and it works perfect

1  
tileBrightness -= Math.max(0, 1 - distance / MAX_DISTANCE);


Thanks guys... sorry for the hassle
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #47 - Posted 2015-06-23 18:20:27 »

Question: what is the code for Utils.mixColours?
Offline tommohawkaction
« Reply #48 - Posted 2015-06-23 18:23:14 »

It is quite a simple code but here you go
1  
2  
3  
4  
5  
6  
   private static Color color = new Color(1,1,1,1);
   
   public static Color mixColours(Color color1,Color color2,float amount){
      color.set((color1.r * color2.r) * amount, (color1.g * color2.g) * amount, (color1.b * color2.b) * amount, (color1.a * color2.a)*amount);
      return color;
   }
Offline chrislo27
« Reply #49 - Posted 2015-06-23 18:38:34 »

Maybe you could try using the built-in libgdx Color lerp method. In my lighting engine I use lerp with 0.5 strength so it's a 50% interpolation from X colour to Y colour.
Offline KaiHH

JGO Kernel


Medals: 798



« Reply #50 - Posted 2015-06-23 19:28:01 »

It is quite a simple code but here you go
1  
2  
3  
4  
5  
6  
   private static Color color = new Color(1,1,1,1);
   
   public static Color mixColours(Color color1,Color color2,float amount){
      color.set((color1.r * color2.r) * amount, (color1.g * color2.g) * amount, (color1.b * color2.b) * amount, (color1.a * color2.a)*amount);
      return color;
   }

Where did you get this code? Smiley
You need to multiply color2 with amount and color1 with (1.0 - amount), component-wise, if you want to interpolate between color1 and color2.

So, in other words:
1  
2  
3  
4  
color.set(color1.r * (1.0f - amount) + color2.r * amount,
          color1.g * (1.0f - amount) + color2.g * amount,
          color1.b * (1.0f - amount) + color2.b * amount,
          color1.a * (1.0f - amount) + color2.a * amount);

This will ensure that the result is color1 if amount equals 0.0, and color2 if amount equals 1.0 (which is what you want).
Offline tommohawkaction
« Reply #51 - Posted 2015-06-23 19:43:56 »

It is quite a simple code but here you go
1  
2  
3  
4  
5  
6  
   private static Color color = new Color(1,1,1,1);
   
   public static Color mixColours(Color color1,Color color2,float amount){
      color.set((color1.r * color2.r) * amount, (color1.g * color2.g) * amount, (color1.b * color2.b) * amount, (color1.a * color2.a)*amount);
      return color;
   }

Where did you get this code? Smiley
You need to multiply color2 with amount and color1 with (1.0 - amount), component-wise, if you want to interpolate between color1 and color2.

So, in other words:
1  
2  
3  
4  
color.set(color1.r * (1.0f - amount) + color2.r * amount,
          color1.g * (1.0f - amount) + color2.g * amount,
          color1.b * (1.0f - amount) + color2.b * amount,
          color1.a * (1.0f - amount) + color2.a * amount);

This will ensure that the result is color1 if amount equals 0.0, and color2 if amount equals 1.0 (which is what you want).



I do have one problem and that is that i am having lines go in between my tiles... how would i fix such a thing I did look at padding but I don't know if that would work?

Offline KaiHH

JGO Kernel


Medals: 798



« Reply #52 - Posted 2015-06-23 19:46:33 »

Yeah, well. That is not *my* color mix method, that is standard old-school linear interpolation, and is quite intuitive. Just think about it for a second. So, did it work then now? All I see is purple text, which is hopefully not what you intended the end result to look like. Smiley
Pages: 1 [2]
  ignore  |  Print  
 
 

 
Riven (593 views)
2019-09-04 15:33:17

hadezbladez (5546 views)
2018-11-16 13:46:03

hadezbladez (2428 views)
2018-11-16 13:41:33

hadezbladez (5806 views)
2018-11-16 13:35:35

hadezbladez (1240 views)
2018-11-16 13:32:03

EgonOlsen (4672 views)
2018-06-10 19:43:48

EgonOlsen (5689 views)
2018-06-10 19:43:44

EgonOlsen (3208 views)
2018-06-10 19:43:20

DesertCoockie (4106 views)
2018-05-13 18:23:11

nelsongames (5135 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!