6

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.

noisy cat
  • 2,865
  • 5
  • 33
  • 51
  • Can you use the fact that the shadow created by a straight-line obstacle is going to be a sector? Start with your circular light, with radius depending on intensity, and then draw a trapezoid in the inverse color for each obstacle. – Ben Voigt May 22 '12 at 13:08
  • hm, can you explain it more a bit? – noisy cat May 22 '12 at 13:09

3 Answers3

7

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.

ltjax
  • 15,837
  • 3
  • 39
  • 62
2

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.

Joris Timmermans
  • 10,814
  • 2
  • 49
  • 75
2

The MadKeithV solution need O(number of pixels^2) time.

My solution is a expanded MadKeithV idea, but run in O(number of pixels) time. With some improvements, it will work in O(number of pixels in light)

First, start with the pixel containing the source of light. Then using BFS procedure 'infect' nearest pixels with light and store angle range of which direction the light can progress from each point.

In following BFS instances, repeat this procedure, considering only pixels in 'infect range'.

Marek Bardoński
  • 529
  • 1
  • 5
  • 14