So I’ve decided to play around a bit for my own giggles and make some swarm stuff. Emergent behavior is the bee’s knees and I think it will be a lot of fun to code up some visual representations of it.
This is the very first step in the processes. It’s not quite a “framework,” but more of a template. Click to activate the ants, slide to change the speed.
You can right click and view source. The code is very rudimentary and the only slightly interesting part is how the ants decide to move.
var stuckCounter:int = 90;
do
{
var randomAngle:uint = rotation + ((Math.random() * stuckCounter) - (stuckCounter / 2));
var randomDistance:uint = Math.random() * 70 + 10;
var randX:int = randomDistance * Math.cos(randomAngle * (Ant.TO_RADIANS));
var randY:int = randomDistance * Math.sin(randomAngle * (Ant.TO_RADIANS));
stuckCounter++;
} while (environment.isObstructionInPath(x, y, x + randX, y + randY));
The code is pretty self explanatory (hopefully), but basically the ant is choosing a random direction within its scope (45 degrees both left and right of it’s current rotation), generates a random distance to travel, and checks with the environment to make sure there’s no obstructions within its projected trajectory. If there’s an obstruction, add 1 degree to the randomizer and repeat. Using randomly chosen angles along with a slow increase in the randomized range helps give a more realistic look of obstacle avoidance than other methods (say, flipping rotation).
I’ll be adding more to this section when I get some motivated down time to play, but this week it’s all about the R&R.