It was a rainy Sunday and I found myself playing around with perlin noise for a couple of hours.
The idea was to create a simple flow field using the BitmapData perlinNoise method, and use this to influence the movement of autonomous agents. There is no complex vector math or steering behaviours, just some simple logic for how a particle or agent reacts to the current pixel it is positioned over.
Despite its simplicity, by changing parameters such as the level of offset per frame or the x and y frequency, it is possible to create some interesting effects which resemble the movement of flocking birds, shoals of fish or indeed more fittingly for this time of year; panicking Christmas shoppers desperately hunting for gifts.
At each update step, an agent will grab the value of the pixel it is on top of and alter its trajectory based on the brightness of the pixel. Throw in some variance relating to how strongly an individual agent will react to this value and some interesting patterns begin to emerge.
The basic logic I’m using in this demo is as follows:
// Get the pixel the agent is over var pixel:uint = noiseBitmap.getPixel( agent.x, agent.y ); // Find the brightness of the pixel var brightness:Number = pixel / 0xFFFFFF; // Set the angle and the speed according to brightness var speed:Number = brightness * agent.speed; var angle:Number = brightness * 360 * Math.PI / 180; // Update the agent's position / rotation agent.x += Math.cos( angle ) * speed; agent.y += Math.sin( angle ) * speed; agent.rotation = brightness * 360;
Feel free to download the source code and start tweaking the many variables. Adding further life to the agents by introducing collision or allowing additional individual traits beyond simply speed and wander might be one option worth exploring.Download: Perlin Noise Flow Field