Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D lighting - make light not go through wall

Tags:

c++

bitmap

2d

light

I have a collsion map, and some places that I want to be light sources. The light source provides light that is actually a shape where I can see the ground. It now looks like this:

enter image description here

So the light goes through the walls. I want to make it look like this:

enter image description here

(I marked the collisions with walls with dark yellow)

So the light rays stop when meeting the wall. I want to get the shape of the correct light, the best would be bitmap containing it)

My first idea was to cast rays from the source and check when they collide with the wall (I know how to do this), but then I would need to cast ray each 0.001 deg for example, so its too much time to generate lights. The next thing is that The light shape isn't always circle, sometimes it can be ellipse or half-ellipse, even triangle or part of the circle. Generally, I have the bitmap with light that doesnt collide anything, and I want to subtract it a bit to make it look like on the second image.

And the last thing, Im using allegro 4.2.1, but all previously mentioned bitmaps are 2-dimmension arrays with 0's and 1's.

Thanx for any help, sorry for long question and my bad english.

like image 697
noisy cat Avatar asked May 22 '12 12:05

noisy cat


People also ask

Does light pass through walls?

(And even with real-time shadows, light still "passes through" walls, which you can see if you have the shadow strength less than 100%.) Shadows require pixel lights.

How to add shadows to a light object?

In Unity 5 you just turn on shadows at your light object, and use the standard shader on any materials that are misbehaving (like your floor or wall). Click to expand...

Why doesn't light pass through walls in pixel art?

Pixel lighting by itself doesn't change the fact that lighting is computed on surfaces without regard to other objects; shadows are like a texture that's overlaid on top of things. i.e., light doesn't really "pass through" walls, it's just that walls are irrelevant to how lighting is computed.

How to detect if light is behind a wall?

In that case you'll need to be a bit more clever. You can simulate this in shaders or use raycasts to see if the light is behind a wall. These options have obvious edge cases that can't be easily resolved.


Video Answer


2 Answers

The basic idea is that you calculate the shadow region of your walls and just not color that.

This article should give you a good start.

like image 85
ltjax Avatar answered Oct 20 '22 06:10

ltjax


In your particular example you can easily brute-force it by checking the line-of-sight from each (empty) pixel to the center of your light source. If you have line-of-sight and the distance is within the falloff, then you have light there. If not, then it's dark.

like image 32
Joris Timmermans Avatar answered Oct 20 '22 06:10

Joris Timmermans