If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

[WIP]Dynamic 2d Lighting [as3.0]

Hello all! Finally to post something constructive:

For the game I'm working on, another nice, creepy, horrorific zombie shooter, I wanted to get some real nice special effects going on to top off the whole apocalyptic experience. So, I thought what would be better than creeping around an old mansion, while shooting zombies, with awesome lighting effects!

I thought about different methods that this would be possible, and finally, since I've become a fan of using math over some bitmapData tricks, decided to do it using objects defined by a set of points, and math. The advantage I saw with this is that I can keep more global information for not only lighting, but eventually hit detection, and other effects.

During my first knock at it, I thought that I could simply calculate the radians from each point in an object to the light source, and choose the max and min ones. I quickly discovered that the rotational "wrapping" around -180-180 degrees got in the way.
I proceeded to writing a lot of if statements, of if the light source is above the object, use these radians, otherwise, etc. etc.
While drawing diagrams, I quickly realized something I for some reason hadn't seen before.

If you draw a line from the light source to the point you're currently checking, the point will cast a shadow if both of it's adjacent points are on the same side of the line. I figured this can be done through vector math, so I did! The good thing I saw with this was also a major performance improvement, since now I didn't need to call Math.atan2() for each point of each object.

I made a vector from the light source to the point you're currently checking, and to it's 2 adjacent points (3 vectors in total). I'll call the vector from the light source to the current point the lineVector, and the other 2 vectors vector1 and vector2 (the order does not matter).
You will have found an edge point if the cross products of the lineVector and both of the other vectors have the same sign, or if one or both of them are 0.

After determining the edge points, I drew the object layer into a bitmapData, so I only had the objects in it. From that, using bresenham's line algorithm, I drew lines from all the edge points to the edges of the bitmapData, and used floodFill on the light source point to get my light!

Here are the 2 examples, the first one with no special effects, but you can see the drawing in action, and the second with it actually looking pretty cool, like lighting up the night!

WASD to move the light source. I personally like the alley way if you go outside the walls.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.