Search code examples
processingperlin-noise

How does a perlin noise field work?


I'm looking at this example in particular:

http://www.airtightinteractive.com/demos/processing_js/noisefield08.html

And here's the code for it:

http://www.airtightinteractive.com/demos/processing_js/noisefield08.pjs

I guess I need explaining for what these lines do in the particle class:

d=(noise(id,x/mouseY,y/mouseY)-0.5)*mouseX;  
x+=cos(radians(d))*s;
y+=sin(radians(d))*s;

I understand that noise calculates a value based on the coordinates given, but I don't get the logic in dividing the particles' x pos by the mouseY, or the y pos by the mouseY. I also don't understand what 'id', which seems to be a counter stands for, or what the next two lines accomplish.

Thanks


Solution

  • Move mouse to change particle motion.

    d seems to be the direction of motion. By putting mouseY and mouseX into the calculation of d it allows the underlying field to depend on the mouse position. Without a better understanding of the function itself I can't tell you exactly what affect mouseY and mouseX have on the field.

    By running cos(radians(d)) and sin(radians(d)) the code turns an angle (d) into a unit vector. For example, if d was 1 radian then cos(radians(d)) would be -1 and sin(radians(d)) would be 0 so it turns the angle 1 radians into the unit vector (-1,0).

    So it appears that there is some underlying motion field which determines the direction the particles move. The motion field is represented by the noise function and takes in the current position of the particle, the particle id (perhaps to give each particle independent motion or perhaps to remember a history of the particle's motion and base the future motion on that history) and the current position of the mouse.

    The actual distance the particle moves is s which is determined randomly to be between 2 and 7 pixels.