Partilhar via


Microbes: emergent behavior and "fake" antialiasing with XNA

I was playing around with particle behavior algorithms using XNA and came up with an app that's fun to play with, so I thought I'd share it.  Getting code ready to post on my blog forces me to simplify it and clean it up, kind of like having to clean the house before having guests come over.  I'm planning to extend this program into a game, but it's entertaining to play around with as it is.  Everyone I've shown it to says it looks like a view through a microscope, so I'm calling it "Microbes."  Or, if you make all the discs the same size and change the colors a bit, it looks like a bowl of Cheerios floating in milk.  Microbes requires 2.0 shader support from your graphics hardware.

Red microbes are attracted to green microbes and vice-versa, but red microbes repel other red microbes and green microbes repel other green microbes.  (My apologies to any readers who are red/green colorblind...)  The particles exert force on each other that diminishes with distance.  The net result is an apparently endless dance as particles swirl around, forming chains or small groups, then diverging -- they never quite settle into a stable state.  The white microbe is under your control.  Use the left controller stick or arrow keys to move it.  Pressing "A" causes your microbe to repel all others; "B" causes it to attract all others.  With no button pressed, it doesn't exert any force on the other microbes.

Like the Mandelbrot Set or my Feedback program, Microbes is an example of complex behavior emerging from simple rules.  You don't need a lot of A.I. code or a full art department to make interesting effects and games, if you're willing to just explore and allow yourself to be surprised by what emerges from the code, rather than starting with a fixed vision of what you want the game to be before writing any code.  I had no idea what I was doing when I started fooling around with these particles...now I have an idea for a unique and (hopefully) fun game.

I use a simple but expensive n-squared algorithm to update all the discs.  If you add too many more objects (search for "numDiscs"), it bogs down.  With some added code complexity (e.g., spatial partitioning), it could be made more efficient, but for a just-for-fun program I prefer to keep the code simple.

One effective way to reduce aliasing effects is to use the alpha channel to blur / fade out the edges of the objects you render.  This gives you a low-frequency edge instead of high-frequency, so you get edges that look much smoother without having to turn on Direct3D's multisampling anti-aliasing.  The pixel shader in the Microbes code has an option to turn off this blur effect, so you can see how much of a difference it makes:

This is easiest to do when you're programmatically generating your content (as with my simple discs), but even authored 2D models can use this technique.  It's harder with true 3D apps though. 

By the way, I'm out of town for the holidays and don't have access to an Xbox 360 (only my laptop PC), so I can't verify whether any of my XNA apps work on the 360 yet (I've heard specifically that Feedback has a problem).  I'll post updates if needed when I get a chance to try them on the 360 and work through any issues.

Let me close by giving a shout-out to a microbe with a truly awesome name, Wigglesworthia glossinidia , named for Sir Vincent Brian Wigglesworth.

-Mike

Microbes_1_0.zip

Comments

  • Anonymous
    December 19, 2006
    The Manders vs. Machine blog recently added a nice entry on Microbes: emergent behavior and "fake" antialiasing

  • Anonymous
    January 06, 2007
    The comment has been removed

  • Anonymous
    January 12, 2007
    There are some nice texture-based solutions out there for drawing text in XNA. I've used the font code

  • Anonymous
    January 14, 2007
    I'm finally done writing an XNA game called "Microbe Patrol" based on the Microbes sample I wrote and

  • Anonymous
    January 15, 2007
    While I've been busy working on our broader community plans, Mike Anderson (Manders vs. Machine)