Boid Simulation Code

So, finally, here’s the code I wrote for my Boid simulator in C# using the XNA framework. Keep in mind: not everything is optimized, documented, and so on and so forth. The code is probably ugly in general, and I’m not using the best C# coding practices. I understand how to code 80% of C# because it’s in the same family of languages as Java, C++, etc., but I don’t know the true ins and outs. If you are a C# aficionado with 1337 C# skillz or any other programming skillz for that matter and would like to pass on helpful information pertaining to coding in C#, why I would be very glad to listen and absorb your expertise.

I suppose I should release this software under some license. So I will. Not to screw you, the end-user, over but to generally absolve me anything detrimental that might happen to you, your loved ones, your pets, and your computer if this software horrendously goes wrong and say, kills you instead of performing the flocking algorithm. It’s a very slim, minute and improbable probability that such an event will happen but I can do nothing about the aberrant possibilities that may bring about these circumstances. So you are thusly forewarned.

The Boid Simulation code for XNA/C# is released under the “Courne Supremacy Licensing Agreement.” By clicking on the download link below you agree to everything I said in the second paragraph of this blog post:

You can download my boid simulation code here.

Oh, now you’re complaining that you can’t run it. Well…you might also need to get the Microsoft XNA Framework Redistributable 1.0 Refresh download.

Or you can get the XNA Game Studio Express 1.0 Refresh and start check out the project workspace and modify the code and get yo learn on.

Happy flocking.

I took the day off to code

As my good friend, Scrabcake, make a record of saying, I am a nerd. Here’s proof: I took the day off from work, where I am a software engineer, to stay at home, and…engineer software. Why engineer software at home for free, when the company in which I work for, would gladly pay me to engineer software for them? Well that’s the difference between me and them. I code, that’s what like doing. I also happen to enjoy drawing, writing, and so on, and the Courne Supremacy, which I thought would be a repository of all my short stories, has now become just whatever cool piece of shit I’m working on at the moment.

This week’s cool shit: Flocking/Herding Simulation.

For those who don’t know what it is check out Craig Reynold’s website. For those of you who can’t be bothered to click on a link, the idea is that you have a bunch of little bots and using a simple set of rules you can govern how they herd or flock together. There are three rules to having these autonomous boids flock:

1 . Center of Mass. For boids to flock together, each individual member of the flock, must move towards the center of mass.

2. Keep Apart. Everybody needs their personal space, and boids are no different. Each boid needs their room, so this test is to make sure that two boids don’t collide when herding/flocking together.

3. Maintain Flock Velocity. If a boid is slowing down, then he needs to speed up, if he’s going to fast, then he needs to slow down.

These three rules work with all the members of a flock and the emergent behavior is that the individual boids move together in a herd. For each of these rules, you use some vector manipulation and summation between the entirety of the herd, and come up average velocities for each of those rules. You then integrate the velocity to figure out your boids’ new positions. Of course, you can also add your own custom rules. I’ve added code to have boids flee at the sight of predators and collision avoidance with the sides of the screen to keep the boids in bounds, and there’s code to have them follow a mouse cursor around.

Why are they called boids? I can’t remember, I believe it has to do with the concatenation and shortening of the word “android” and “birds” since these are artificial birds. That’s just a guess, I’m sure there’s another, more correct, wikipedia, mother-approved reason why they are named boids.

The second part of this week’s cool shit: I wrote the whole thing in one day using XNA.

XNA is Microsoft’s XBox360, XP, and Vista C# platform for hobbyist game development. You can download it here and start making games. If you’re new to programming, XNA will be a steep learning curve. You’ll need to learn C#. For a veteran coder, someone who’s done their rounds with C++, Java, and the ilk, you’re 80% there with C#, the extra 20% is getting down the syntactical sugar and wrapping your head around some of C#’s more unique features. I have, what I will consider, a very primitive knowledge of C#, and yet within a day I was able to build a very simple 2D demo, but then again, I’ve been coding for maybe a decade now.

If you want to make a quickie 2D game, XNA will get you up to speed very fast. Oh, and you might want to pick up an XBox360 controller. It’s very easy, almost braindead (once you get past the whole learning C# thing) to interface and start using the gamepad. So far every time I’ve run into something I’ve needed to do for my simulator, XNA has had the answer. Need a container to hold variables for a plane, XNA’s got you covered. Need to import sprites, no problem. XNA supports png, jpeg, and a plethora of other types. You just add it as a resource to your project and you’re on your way. Getting something 2D up and running is a piece of cake.

Now 3D…well, there’s a lot more of a learning curve there, but there are a lot of neat features involved: pixel and vertex shaders without the hassle of setting up pixel and vertex shaders. My paltry single day of coding couldn’t scale that cliff, but that will be left for another day…maybe when I decide to take my Boids into the dreaded 3rd dimension.

And now, the picture. Some explanation. The happy rabbits (albinos) are flocking. Red Albinos mean that they are currently resting. I gave them an energy meter, but since XNA is notoriously bad at displaying text (okay, yeah, XNA has it’s draw backs…), I’m left with tinting sprites to show me the state of each boid. The big spiders (whorses, as we like to call them in these neck of the woods) are predators. They mainly wander around and scatter the flock. Click to enlarge:

boidsim xnaproject I took the day off to code xna simulation Programming c# boids

BTW, I’ll post the source code, once I figure out how I’m going to do that via the blog.