The continuing adventures in Flash Dynamic Shadows
In the last couple of weeks I haven't had as much time to work on my own projects as I would like. However, that isn't to say I have been completely without progress. So without further disemination...
I began by refactoring my lighting system a bit. I'm preparing for integration into a game editor so I needed a clear way to seperate out data from behaviour. I decided to go with a factory system, and have a Create() and Destroy() method for my lights and shadow casters. I'm currently instantiating the lights and casters directly. Physics and lighting are something I haven't quite decided how exactly I'm going to set up, but I have it right now that my helper functions for my physics world return a unique ID, and I assign that to the light or caster.
I'm not currently using an actor system for the physics, I'm just assigning the unique ID to the b2Body:m_userData and then returning it. In my custom contact listener I have a simple method:
override public function BeginContact(contact:b2Contact):void
{
super.BeginContact(contact);
var light:LightComponent;
var casterID:int = -1;
if ( contact.GetFixtureA().IsSensor() && !contact.GetFixtureB().IsSensor() )
{
light = LightSystem.Instance.GetLight( contact.GetFixtureA().GetBody().GetUserData() );
casterID = contact.GetFixtureB().GetBody().GetUserData();
}
else if ( contact.GetFixtureB().IsSensor() && !contact.GetFixtureA().IsSensor() )
{
light = LightSystem.Instance.GetLight( contact.GetFixtureB().GetBody().GetUserData() );
casterID = contact.GetFixtureA().GetBody().GetUserData();
}
if ( light != null )
{
light.AddShadowCaster( casterID );
//trace( "Adding contact with light:", light.Id, casterID);
}
}
Sometimes code is just cleaner than words. In this case, I wanted to keep the BeginContact() callback lean, so I'm simply adding the id from any casters to each light. The lookup for the light is as cheap as possible and is using a Dictionary( modelled as a hashmap in AS. ) The AddShadowCaster(id:int) method is also as cheap as possible, it simply adds the id to a ToBeAdded list on the light. The EndContact() callback is very similar and simply adds an ID to the ToBeRemoved list.
Once I had this in place, my lights are now constructed of a sensor shape that is the size of the light's extents and an optional solid physical core. The core is optional because not all lights need a physical representation and work just fine as an environmental light. However every dynamic light needs a sensor shape so it can tie into the physics engine's broadphase. Since all shadow casters are physics objects, they will generate Begin and End contact events which make it easy for the lights to know whom to cast shadows against.
Which brings me finally to the last bit I ended up doing. I put everything into my first attempt at a package structure. I have to warn though, I am not an AS developer at heart, I'm a c++ and c# developer... so my packager structure is a bit unconventional compared to most of what I've seen in the AS community.
The last thing I've done so far is start work on a very basic camera and rendering system. So far all I have is a prototype camera movement( hooked to arrow keys ) that shows off just how bare my test setup is. At least everything is moving correctly.
Demo on next page...

Post new comment