Thank you for your help guys, I really appreciate it.
I’m pretty tired now so i’m going to help to bed and redesign my lighting in the morning as I don’t believe it will work with multiple lights at the moment. It took me 10 minutes to create this lighting so i’m not bothered really.
Hopefully I can come up with a better way of storing the tile information (Maybe even a class that creates a new “tile” in the map and store the shadow inside here). That will then allow me to compare the tiles light value with what i’m trying to replace easier
I chose recursion because I tried it before in the past. I actually don’t like how it looks but recursion is obviously the simplest way to do spread lighting. I would actually prefer a rounded lighting type like Sauron’s, but I don’t know how to implement it.
Don’t create new objects for each tile! You should store some primitive value instead of making a ton of objects.
The anti-object mentality is very usecase-dependent. Yes, for a 3D freeroam game there are better methods, but for a 2D topdown tilebased game like what OP seems to be making creating objects for each tile is not a bad idea, especially for a quick, easily tweakable setup.
You could also take the approach of iterating through each tile and iterating through light sources to find the light values, which is easier to implement than recursion and spread lighting. Even better, write a shader that takes the light sources and creates the blocky lighting you need!
It’s working! I just spent 30 mins rewriting everything
I followed what Cougar said (It seemed simpler for what I need right now) and it all works! The code needs to be changed as it’s a little messy right now and I need to figure out if there is a better way of drawing the shadows over the tiles. I’m currently drawing sprites over them as this allows easy manipulation of the alpha values
Edit: I hate code ._.
Stupid player lighting now means that once he walks into an area it stays lit forever
Second edit:
I’m actually going to keep this style of lighting I think, I actually think it works pretty well for my dungeon explorer style game. I’m defiantly going to try using shaders next time for this sort of stuff (I’m just not very good with math :persecutioncomplex:)
for(Light light : lights){
for(Cell cell : cells ){
float lightShadow = Math.min(cell.getLightAlpha(), light.getLightValue(cell.getShadowX(), cell.getShadowY())); // Get the min value from the 2 arguments
cell.setShadowValue(lightShadow);
}
}
Generating the light value for each tile:
public float getLightValue(int tileX, int tileY) {
double tx = ((xPos - tileX) / 32); // xPos of the light minus tileX. Dividing by 32 due to the tile coord system
double ty = ((yPos - tileY) / 32);
double hDistance = (tx * tx) + (ty * ty); // A^2 * B^2 for distance
double distance = Math.sqrt(falseDistance); // Real distance from the square root
float shadowValue;
if(distance < 9){
if(distance < 2){
shadowValue = 0;
}else if(distance >= 2 && distance < 3f){
shadowValue = 0.2f;
}else if(distance >= 3 && distance < 4f ){
shadowValue = 0.3f;
}else if(distance >= 4 && distance < 5f ){
shadowValue = 0.4f;
}else{
shadowValue = 0.88f;
}
}else{
shadowValue = 1f;
}
return shadowValue; // Return the shadow value that is needed;
}
There are issues with the code still (still trying to stop areas being lit forever) but the code generates that nice circle pattern.
Edit: I know the issue related to checking if a tile has any lights around it and if not then setting the value, I’m just not sure how to do that yet…
So I’ve spent the day testing out different lighting and trying to learn more about shaders. I finally come across something interesting online that said I could simply use Box2DLights fairly easily and takes very limited setup. Well… It was correct, I had lighting setup within 10 minutes and everything looks great (Not got my block effect atm :persecutioncomplex:).
I do have an issue that the light seems to stay in one place on the screen, instead of appearing to move as the player moves away. I’m sure it’s a simple fix though :point:
Fixed it: Simple error on my part. I put the projection matrix outside of the render
Yup! You just need to create a “world” with Box2D (even if you don’t use the world, you need one for the lights). Then simply use the RayHandler class to create the lights and finally call “rayHandler.renderAndUpdate();” At the bottom of your render
If you didn’t create your game with the needed jars at the start, just simply create a folder called “libs” (call it whatever really) and drop the jars inside here. Then go to build path, add jars and select the files. Finally, go to order and export inside the build path and select the jars :point:
EDIT: You will not get shadows without Box2D bodies btw. I’m just using the lighting for ambient lighting and fires. You could use shaders etc but they require more knowledge and they confuse me at the moment
Unfortunately not using Box2D bodies as not needed for this game I’m doing. I have used Box2D in the past for
games I’ve done on iOS.
Could I not just use a sprite and the blender functions, draw this sprite over the top of the tiles? Failing that, may look at using Box2DLights as I only think you need the bodies if wanting shadows. I think Box2DLights would also be quicker than using the FBO technique.