0

So here's my problem:

Currently I'm developing a primitive light system for a game. The stage is overlayed with a black mask to represent 'darkness,' its alpha is then adjusted from 0 (brightest, invisible) to 1 (total darkness, opaque.) As the 'hero' gets closer to a light, I subtract the total amount of absorbed light from 1. To create a 'zone' of light around each light, with more light coming the closer the hero is, I used a quadratic expression that takes the 'distance' as the independent variable.

My code looks like this:

            var boost:Number = 0;
        var exponent:Number = 1-.1*(Lists.Lights.length)
        for (var ii:int = 0; ii < Lists.Lights.length; ii++) {
            var dist:Number = Lists.Lights[ii].getDistance()/100
            var addMe:Number = -Math.pow(dist, exponent) + Math.pow(dist, exponent / 2) + 1
            if(addMe>0){
            boost += addMe
            }
        }
        trace(boost)
        var b:Number = brightness - boost

The problem is adjusting the exponent for the number of lights. Currently the more lights, the more severe the adjustments are. More lights = lights with shorter range. This is because all the lights are cancelling each other out, with distant lights having little to no effect.

So, should I try to restructure my code to use a different method or is there something I'm not seeing here?

Charles
  • 50,943
  • 13
  • 104
  • 142

1 Answers1

0

In Reality(TM), the strength of a light is basically luminosity/(dist^2) (or luminosity * dist^(-2) if you like). I don't know that modeling it via a -dist^2 term is going to be very close to what you want (the shape of the graphs -x^2 and x^(-2) aren't all that similar). I would suggest doing something like that (although when you get very close to a light source, this is going to get very large, so you might want a cutoff for small distances).

tabstop
  • 1,751
  • 11
  • 10