I remain uncertain about the method you’re using, but as for the immediate problem, here are two suggestions. First, in this line:
spriteBatch.setColor(tileColor);
Try temporarily changing the color to a known, fixed value of your choosing (e.g. purple), just to make sure the color is actually having an effect and that the problem isn’t later in the rendering process.
If that doesn’t tell you anything, then in this code (or whatever it looks like now):
Use either the debugger or debug output to see if, at the end of this code, ‘tileColor’ has the value you expect it to have.
(The optimal solution may just be to take a different approach, as others are suggesting, but it might still be useful to understand why your current code isn’t working as you expect.)
The setColor is fine and works as expected but what i am not sure about is my mix colour method and whether i am meant to use transparency and what colour i sould have the pixmap?
my method will not work for you because Im not actually calculating distance from my lights. All of my lighting is created from editing the r,g and b values and editing the intensity of lights. It give me less control over my lights but I get pretty lights plus I can make shadows from the absence of light without any raycasting
On paper (literally or figuratively), work out the math for the effect you want. That is, given the input parameters and the output you want, what math will yield that?
Implement the math in your code (this would presumably go where you’re lerp-ing currently).
If the result isn’t what you expect, use the debugger and/or debug output to determine where things are going wrong.
If that doesn’t lead to a solution, post the relevant code, the input values, the expected output values, and the actual output values for us to take a look at.
Maybe you’ll be able to fix it without going through these steps, but if you continue to find yourself stuck, a systematic approach as described above might be a good idea.
Been working on it all morning and have had no luck and i have redone the whole thing…
It doesn’t help when i am not completely sure how the lerp is working but when using my mix method it works perfect other than the fact that i cannot chnage the colour of the lighting
Color tileColor = sun_light;
tileColor = tileColor.lerp(new Color(1,1,1,sun_light.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);
private Color sun_light = Color.BLACK;
public LightEngine(OrthographicCamera camera, SpriteBatch spriteBatch) {
Pixmap tilePixel = new Pixmap(1, 1, Format.RGB888);
tilePixel.setColor(new Color(1,1,1,0));
tilePixel.fill();
pixelTexture = new Texture(tilePixel);
this.camera = camera;
this.spriteBatch = spriteBatch;
}
spriteBatch.setColor(Color.RED); // Or maybe something with alpha < 1...
It works, and the light is tinted correctly. Is that correct?
If so, that suggests the problem is occurring earlier. In that case, try this. First, temporarily change your loop:
for (int x = startX; x < toX; x++) {
for (int y = startY; y < toY; y++) {
So it just renders the light for one tile for the time being. That way you can just focus on one tile and its values for the moment, which will simplify debugging.
Then, here’s what you want to find out:
Color tileColor = new Color(0,0,0,0);
tileColor.lerp(Color.BLACK, tileBrightness);
// What is the value of 'tileColor' right here?
spriteBatch.setColor(tileColor);
You can examine the value in the debugger, or print it to the console (here is where having only one tile to deal with will make things easier).
If it’s still not working, post the updated code (making sure we can see what the inputs are), and also post the value you got for ‘tileColor’ from the debugger or debug output.
Maybe the problem is obvious from what you’ve posted so far and someone will point it out. If not though, taking the above steps should help in diagnosing the problem.
One thing I noticed was that your pixmap for the tilePixel was filled transparently. It should be filled with an opaque colour (white). Another thing I noticed was that you’re treating “brightness” and the colour alpha as the exact same thing, but in reality brightness is the inversion of alpha (1.0f - alpha = brightness).
Either you have a new brightness array that keeps track of brightness independently from the colour alpha, or invert alpha when you’re about to render.
In your latest post you make a Color object (you should really just have some reused object that’s not created so often) that is black and then attempt to lerp it… with black?
I’ll try to go step by step what the order should be.
set a brightness variable to the ambient (if it's day, set it to 1) brightness
using a stored colour variable that's not re-created constantly, set it to the ambient colour. ignore alpha for now
loop through the lights
get the distance away from a light, calculate what the brightness WOULD be
if the calculated brightness is more than the current brightness, replace it
lerp your stored colour variable with the light's colour, with a factor of the distance
set the stored colour variable's alpha to the inverse of the brightness (1.0f - brightness)
render the square over the tile
That should work, at least I hope it does. It might be best for you to redo the inner for loop rather than try to fix it. It will be less confusing that way.