Attaching Custom Data to Particles.

This section is a fragment of a forum thread

Basically the ITagBinder interface is a way of attaching custom data to Particles as they are released by the Emitter. It exposes two methods:

object GetTag(ref Particle particle): This method should return a custom data tag for the particle.
void DisposeTag(object tag): This method is called when the Particle is retired and the tag is no longer needed.

Of course attaching custom data to Particles alone is not very useful, you also need to be able to make use of that data. So now the custom data tags are also passed to the Modifier process methods so they can be utilised by specialised Modifiers. This opens up a lot of possibilities for extending the engine with extra functionality, and also for interaction with other engines.

Here's a quick example is psuedo code:

using FarseerGames.FarseerPhysics.*

public class BodyTagBinder : ITagBinder
    public PhysicsSimulator Simulator { get; set; }

    public object GetTag(ref Particle particle)
        Body body = BodyFactory.CreateCircleBody(this.Simulator, particle.Scale, 1f);

        Geom circleGeom = GeomFactory.CreateCircleGeom(this.Simulator, body, particle.Scale, 8);

        body.Position = particle.Position;

    public void DisposeTag(object tag)
        Body body = (body)tag;


And there is a relatively simple way of adding Farseer rigid bodies to Mercury particles :D

Then of course you'd need a modifier to use the data:

public class FarseerPhysicsModifier : Modifier
    public override void Process(..., ..., ref Particle particle, object tag)
        Body body = (Body)tag;

        particle.Position = body.Position;
        particle.Rotation = body.Rotation;
        particle.Velocity = particle.Momentum = Vector2.Zero;

Now to set up the Emitter:

Emitter emitter = new Emitter();
emitter.TagBinder = new BodyTagBinder { Simulator = myPhysicsSim };
emitter.Modifiers.Add(new FarseerPhysicsModifier());

Now all the Particles released by the Emitter will be fully under the control of Farseer :D

Last edited Aug 24, 2009 at 4:37 PM by JimJams, version 1