Flow Field with Perlin Noise

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

Have fun!…

Posted on 14 Dec 2008
28 Comments
4 Trackbacks

Meta

Perlin Noise Flow Field was posted on December 14th 2008 in the category Lab / Actionscript 3.0, Code, Flash, Lab, Open Source and tagged; , , , , , , .

You can Leave a comment.

Twitter <follow>

February 7th 2012 - 5:37pm

RT @_leonardsouza_: 37signals to stop developing for IE8 » http://t.co/wL6dtZSg

Discussion

32 Responses to Perlin Noise Flow Field

Leave a Reply

Pingbacks / Trackbacks

  1. 3 years ago Weekly Shared Items - 19. December, 2008 « toxin 2.0

    [...] Perlin Noise Flow Field [...]

  2. 2 years ago mostly a codeBlog » Webcam Flow field in Flash

    [...] was lurking about over at Justin Windle’s blog Soulwire, wish my blog had an alter ego with a cool name.. It was a bit to dark, the background is [...]

  3. 1 year ago suche einfaches partikelsystem (schwarm) mit dynamischer anzahl - Flashforum

    [...] haste dir schonmal FLINT angeschaut? oder hier ein nettes perlin noise flow field cheers, sal __________________ NKUNITED.DE – FLASH [...]

  4. 7 months ago Aerodynamik mit AS3 Ideen? - Flashforum

    [...] [...]

  1. wongwaiting 3 years ago

    just cool~love it!

    Reply to this comment

  2. peko 3 years ago

    RESPECT !!!
    Its a beautiful example of an application a perlin noise fractals!

    Reply to this comment

  3. Kelvin Luck 2 years ago

    Hey :)

    I’ve just discovered your blog and you’ve got some cool stuff – great work :)

    I did something similar to this example after seeing seeing Robert Hodgin talk at FOTB 07 – check it out here:

    http://www.kelvinluck.com/2007/11/flash-on-the-beach-and-some-perlin-noise/

    Just under the image is a link to a page where you can see the steps I went through to get there which is also quite interesting…

    Keep up the good work,

    Kelvin :)

    Reply to this comment

  4. Michael 2 years ago

    I saw your music visualization work. This combined with that might be very useful for children with ASD. Have you seen the work of Hailpern, Karahalios and Halle.

    Reply to this comment

  5. felix 2 years ago

    this is supercool especially the blitted version. perlin noise ftw!

    Reply to this comment

  6. Brent 2 years ago

    Love your work. Its beautiful. I was keen to get under the hood and have a play but the file my Flash CS3 exports from your FLA is only 24k (Compared to the 72k of the pre-exported .swf in your download) and does nothing.

    I read through above and no one else seems to have had this problem which makes me feel pretty stupid. I am still learning AS3, am I missing sometihing really obvious?

    Thanks.. I really enjoy your site. Very inspiring stuff.

    B.

    Reply to this comment

  7. Soulwire 2 years ago

    Cheers Brent. Sounds like you might having problems with your document class. In the properties menu for your FLA, hit the pen icon next to the document class (which should say FlowField). If it complains that it can’t find it then browse to the file. If it opens in the IDE it’s something else, but the filesize issue suggests something isn’t being compiled into the swf you’re creating.

    Reply to this comment

  8. Jan 2 years ago

    wow. this is pretty inspiring stuff. I’m new at actionscript and I couldn’t help notice that the version online constantly adds new agents to the stage, while the version in the source files runs out of agent after a while. Would you be able to share how one might go about make the program constantly add more agents to the stage?

    Reply to this comment

  9. Soulwire 2 years ago

    Hi Jan, thanks for the comment.

    It should be keeping the agents within the stage bounds. perhaps they’re getting stuck as It’s these lines that are handling the boundaries (they check if the agent is off the left of stage and if so move it to the right of stage – same principle with the Y axis)

    private static const MARGIN:int = 10;
    
    if ( particle.x < -MARGIN ) particle.x = stage.stageWidth + MARGIN;
    else if ( particle.x > stage.stageWidth + MARGIN ) particle.x = -MARGIN;
    
    if ( particle.y < -MARGIN ) particle.y = stage.stageHeight + MARGIN;
    else if ( particle.y > stage.stageHeight + MARGIN ) particle.y = -MARGIN;
    


    It’s pretty rudimentary. If you want to add your own logic it works along these lines though – i.e. check an agents position and if its x is less than the left of stage – its width, either remove it and spawn a new one or move it somewhere on stage.

    Reply to this comment

  10. Roma 2 years ago

    Soulwire, your blog is just a trunk with treasures.
    Thank you so much.

    Reply to this comment

  11. Ben 2 years ago

    Hello

    Should try Perlin Noise function using frocessing library ( frocessing.math.PerlinNoise )
    It’s a simple mathematic function that do the same , much more faster !

    Reply to this comment

  12. NICK 2 years ago

    [...]I’m new at actionscript and I couldn’t help notice that the version online constantly adds new agents to the stage, while the version in the source files runs out of agent after a while.[...]

    If your bandwidth profiler is up then this would cause the problem due to the stage bounds being offset when the profiler opens and the constraints written in the code relating to the initial stage size.

    I love this, very interesting using brightness to offset movement. Would be interesting to see generated art over a photograph or other piece of art. Maybe mix with music to generate over the course of a song for velocity and direction using brightness and color change. hmm…

    Reply to this comment