Dynamic lighting from scratch

I have been playing around with trying to get lighting work and i can get day/night working using


RescaleOp op = new RescaleOp(horizon.getGame().getLevel().getDaylightIntensity(), 0, null);
			image = op.filter(image, mage);

But im not sure how to go about rendering lights. i don’t want to have the lighting blocky like in minecraft, but rather increase its intensity pixel by pixel the closer you get to the source. anyone know how this is done?

Are you using shaders?

This is in the Java2D section, so I’m going to assume you’re using Java2D and not OpenGL or shaders.

The easiest way is to just use an image mask that is completely transparent at the light source, then fades to black further from the light source. Overlay that on top of your game, and voila, you’ve got lighting.

Do a google image search of “game development lighting source” or something. Or here’s a StackOverflow question with more info: http://gamedev.stackexchange.com/questions/22159/can-i-achieve-a-torchlight-effect-lighter-area-around-a-light-source-in-a-2d-g

I discovered a video explaining light maps as soon as you guys sent this xD why dose this keep happening, but thanks this is just what i needed!

Oops didn’t notice that

Np man :slight_smile: Time to get shadows working! hehe

Maybe because you don’t spend enough time searching before asking questions?

I have a working light system for the game but it causes a little bit off lag and cant shake it, any suggestions?


package com.horizon.game.gfx;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import com.horizon.game.horizon;
import com.horizon.game.entitys.Player;

public class Light {

	private int x;
	private int y;
	private int radius;
	private int intencity;
	private BufferedImage image;

	public Light(int x, int y, int radius, int intencity) {
		this.x = x;
		this.y = y;
		this.radius = radius;
		this.intencity = intencity;

		image = new BufferedImage(radius * 2, radius * 2,
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = (Graphics2D) image.getGraphics();

		int step = 4;
		int numSteps = radius / step;
		g2.setColor(new Color(0, 0, 0, intencity));
		for (int i = 0; i < numSteps; i++) {
			g2.fillOval(radius - i * step, radius - i * step, i * step * 2, i
					* step * 2);
		}
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public int getRadius() {
		return radius;
	}

	public int getIntencity() {
		return intencity;
	}

	public BufferedImage getImage() {
		return image;
	}

	public void render(Graphics2D g2, int width, int height,horizon horizon) {
		Player p = horizon.getGame().getPlayer();
		g2.drawImage(
				image,
				(width / 2)
						- image.getWidth()
						/ 2
						- ((p.x * horizon.SCALE) - (((x * 16) + 8) * horizon.SCALE)),
				(height / 2)
						- image.getHeight()
						/ 2
						- ((p.y * horizon.SCALE) - (((y * 16) + 8) * horizon.SCALE)),
				image.getWidth(), image.getHeight(), null);
	}
}


Time to profile your code and figure out what’s causing the lag. Try adding print statements with the elapsed time of each section, see which section takes the most time. Then go into that section and see which part of that section takes the most time. Repeat until you know what’s causing the lag. Good luck!

If you want more specific help, you’ll have to provide an MCVE.