This is what is sometimes referred to as a “flow field”. In this sketch I’m using Perlin noise to create a grid of vectors. This vector grid is then used to dictate the motion of individual particles through the system. The end result is the illusion of coordination. You can press the “G” key to see the underlying vector grid.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
boolean showsFPS = false; boolean showsField = false; PFont font; float gridSize = 70; int xRes, yRes; Node[] nodes; int numParts = 3000; Part[] parts; void setup() { size(940, 540); background(255); noStroke(); rectMode(CENTER); ellipseMode(CENTER); createField(); createParts(); font = createFont("Helvetica", 24); } void draw() { fill(0, 15); noStroke(); rect(width/2, height/2, width, height); updateField(); updateParts(); if (showsFPS) displayFPS(); } void scatter() { for (int i=0; i<numParts; i++) { Part part = parts[i]; part.vx += random(-1.0, 1.0) * 50.5; part.vy += random(-1.0, 1.0) * 50.5; } } void displayFPS() { textFont(font, 18); fill(255); String output = "fps="; output += (int) frameRate; text(output, 10, 30); } void updateField() { for (int i=0; i<yRes; i++) { for (int j=0; j<xRes; j++) { Node node = nodes[i*xRes+j]; float noiseScale = 0.1; float a = noise(i*noiseScale,j*noiseScale, frameCount*0.003)*TWO_PI*2; PVector vector = new PVector(cos(a), sin(a)); node.vector = vector; if (showsField) node.render(); } } } void updateParts() { noFill(); stroke(255, 70); for (int i=0; i<numParts; i++) { Part part = parts[i]; int xPos = floor(part.x/gridSize); int yPos = floor(part.y/gridSize); int currentIndex = yPos*xRes + xPos; part.vx += nodes[currentIndex].vector.x * 0.7; part.vy += nodes[currentIndex].vector.y * 0.7; part.vx += random(-1.0, 1.0) * 0.5; part.vy += random(-1.0, 1.0) * 0.5; part.update(); part.render(); } } void createField() { xRes = ceil(width/gridSize); yRes = ceil(height/gridSize); nodes = new Node[xRes*yRes]; for (int i=0; i<yRes; i++) { for (int j=0; j<xRes; j++) { float noiseScale = 0.1; float a = noise(i*noiseScale,j*noiseScale)*TWO_PI; PVector vector = new PVector(cos(a), sin(a)); float cx = j*gridSize + gridSize/2; float cy = i*gridSize + gridSize/2; PVector center = new PVector(cx, cy); Node node = new Node(vector, center); nodes[i*xRes+j] = node; } } } void createParts() { parts = new Part[numParts]; for (int i=0; i<numParts; i++) { Part part = new Part(random(width), random(height)); parts[i] = part; } } void keyPressed() { if (key == 'f') showsFPS = !showsFPS; if (key == 'g') showsField = !showsField; } boolean sketchFullScreen() { return false; } class Node { PVector vector; PVector center; color randColor; Node(PVector v, PVector c) { vector = v; center = c; selectNewColor(); } void update() { } void render() { noStroke(); fill(randColor); rect(center.x, center.y, gridSize, gridSize); PVector renderVector = vector.get(); renderVector.mult(gridSize/2); stroke(150); line(center.x, center.y, center.x+renderVector.x, center.y+renderVector.y); } void selectNewColor() { randColor = color(random(255), random(255), random(255), 40); } } class Part { float x, y, vx, vy, px, py; Part(float _x, float _y) { x = px = _x; y = py = _y; vx = 0.0; vy = 0.0; } void update() { px = x; py = y; vx *= 0.95; vy *= 0.95; x += vx; y += vy; wrap(); } void wrap() { if (x > width) x = px = 0; if (x < 0) x = px = width; if (y > height) y = py = 0; if (y < 0) y = py = height; } void render() { line(x, y, px, py); } } |


