[WIP]Dynamic 2d Lighting [as3.0]
A Flash Developer Resource Site

Results 1 to 5 of 5

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

Hybrid View

  1. #1
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913

    [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.

    PHP Code:
    c1 vector1.crossProductlineVector);
    c2 vector2.crossProductlineVector);

    if (
    c1 <= && c2 <= || c1 >= && c2 >= 0)
         
    //currentPoint is an edge point; 


    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.

    Basic Example
    Night FX Example

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140
    started on something similar end of last year, haven't touched it since.

    http://littlemofo.soap.com.au/gamedev/lights/

    doing the same kind of thing you're doing. My plan was to also do a Zombie game, how original
    lather yourself up with soap - soap arcade

  3. #3
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    Ah, nice! I saw that example, but I thought it was graphics based or something...

    You should be the one sharing!

    Anyways, if anyone else has any ideas, suggestions, pointers, flames, I'd love to hear it! Also I'd like to roughly hear the performance (noticeable/unnoticeable)?

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  4. #4
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    first of all, very nice share! Next, what about curved areas? say a circle in the center of your map?
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    1
    Light and water are the trickiest things I find when it comes to making a game. Where are some good tutorials on lighting? Anyway, How did your Zombie game turn out in the end?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center

Poll by Flashkit.com