So after fixing that crazy bug in Box2D that had my code crashing all over the place, I jiggered a few things about and made a push to the last “big” thing I wanted to do: Lighting!
Now, god only knows real-time lighting is pretty damn hard to do, and super-duper expensive when you have no shaders (and ultimately no control over the end product of your code, which is the curse of a convenient multiplatform language), so I’m just cheating and doing canned effects. All I have running right now is lights reflecting on the ball, but it works pretty well I think.
I have a new manager in my code which handles lighting, or more precisely the *reflections*. Actual illumination of light-bulbs is done by just putting another Sprite with the lit-up image over the top and playing with the alpha. I had to make a few more things at the same time to help make it look cool (and for other effects) , my own Timer system because I wanted some custom stuff so I could have the lights fade in/out, a load of changes to my playing field (which I haven’t really tested yet) to accomodate the access needed to game assets, and some nasty scaling code which spends half its life crashing.
The alpha of the reflection varies linearly with distance, and each light also holds two “attenuation” variables – one of which is publicly accessible, one internal – so the alpha of the light source can also have an effect (i.e, alpha = (1 – (objectDistance / MAX_DISTANCE)) * attenuation1 * attenuation2 ). To rotate the Sprite to the correct angle was also a pain, since ActionScript3/NME use the top-left corner as their origin and it can’t be altered! Luckily in Sprite.transform you can manipulate the transformation matrix directly, which makes it all better.

Pretty though, huh? Right now there’s no additional code to check for objects between the target and source, but that shouldn’t be hard (since everything that COULD obscure it is on the table and collidable, I can just skim the list of objects kept by Box2D). Performance seems solid however, no drop in FPS… Although I’ve yet to try it all in motion (and obviously, I have about 2% of my graphics done). Here’s the code I run to update the lights — not hard but I’m sure it’ll be useful to someone (I always have trouble remembering angular stuff). Continue reading →