Saturday, December 26, 2009

Stardust v1.1 - Now with Linked Lists

Stardust project homepage
Particle collection documentation

After a few days of struggle, I've finally done it! Now Stardust implements linked lists internally for particle collections instead of arrays. Linked lists are better than arrays for rapidly adding and removing elements at random positions, theoretically. I found the performance to remain pretty much the same after this major change. However, the internal particle list implementation is now hidden from clients, which makes it much safer, preventing direct access to the particle array as in version 1.0.

To iterate through particles in a particle list, you use the ParticleIterator interface. You shall be familiar with this interface if you have worked with the C++ STL library or the Java collection APIs.

I'll illustrate this iterator interface by some sample code. Here's how you set all the particles' positions in an emitter to (0, 0), the origin.

var particles:ParticleCollection = emitter.getParticles();
var iter:ParticleIterator = particles.getIterator();
var particle:Particle2D;
while (particle = iter.particle as Particle2D) {
particle.x = particle.y = 0;;

And this is how you add completely-customized particles to an emitter.

var particles:ParticleCollection = new ParticleList();
for (var i:int = 0; i < 100; i++) {
var p:Particle2D = new Particle2D();
p.x = 100 * Math.random();
p.y = 100 * Math.random();

Pretty clean and self-explanatory code, isn't it? I'm quite satisfied with how the interface ended up. From now on, I can tweak the internal implementation and algorithm of the particle list without having to change the code outside of the black box. Gosh, I should have done this long ago.

No comments: