Skip to content

Flow Fields

Use noise to choose a direction at every point, then let many particles each take small steps that way: the paths show the hidden pattern, like hair or water.

How it works

A flow field is a direction for every point of the picture. Take a noise value there, about 0..1, and multiply it by 4π to make an angle that can turn twice round. Because noise changes smoothly, nearby points point nearly the same way, so the field has currents and swirls instead of chaos.

A particle at (x, y) looks up the angle where it is and moves a short step that way, x += cos(a) × step, y += sin(a) × step, then looks again. Drawing a line from each step to the next shows its path. Many particles from random starting points fill in the whole field.

The zoom of the noise (0.004 here) sets how wide the currents are; the step size sets how smooth the lines are.

The first example draws every path in full each frame with the raylib shapes starter. The second is the raylib particles starter's own sketch: each particle takes one step a frame and the picture fades a little, so they leave trails; a particle starts again somewhere else when it dies or leaves the picture.

Paths: 400 particles following noise

static void draw(float t)
{
    clear(PAPER);
    for (int i = 0; i < 400; i++) {
        Vector2 p = { frand(0, W), frand(0, H) };
        for (int s = 0; s < 60; s++) {
            float a = noise2(p.x * 0.004f, p.y * 0.004f) * 4 * PI;
            Vector2 q = { p.x + 4 * cosf(a), p.y + 4 * sinf(a) };
            DrawLineEx(p, q, 2, Fade(INK, 0.16f));
            p = q;
        }
    }
}
function draw(t) {
  clear(PAPER);
  for (let i = 0; i < 400; i++) {
    let p = Vector2(frand(0, W), frand(0, H));
    for (let s = 0; s < 60; s++) {
      let a = noise2(p.x * 0.004, p.y * 0.004) * 4 * PI;
      let q = Vector2(p.x + 4 * cosf(a), p.y + 4 * sinf(a));
      DrawLineEx(p, q, 2, Fade(INK, 0.16));
      p = q;
    }
  }
}

Moving particles, coloured by a palette

Each particle has its own random k, 0..1, which picks its colour from the sea palette.

#define N 1500

static void spawn(Particle *p)
{
    p->pos = p->last = (Vector2){ frand(0, W), frand(0, H) };
    p->age = 0;
    p->life = frand(60, 240);
    p->k = frand(0, 1);
}

static float field(float x, float y, float t)
{
    return noise3(x * 0.004f, y * 0.004f, t * 0.05f) * 4 * PI;
}

static void frame(Particle *ps, float t)
{
    fade(PAPER, 12);
    for (int i = 0; i < N; i++) {
        Particle *p = &ps[i];
        float a = field(p->pos.x, p->pos.y, t);
        p->last = p->pos;
        p->pos = Vector2Add(p->pos, (Vector2){ 1.5f * cosf(a), 1.5f * sinf(a) });
        if (++p->age > p->life || p->pos.x < 0 || p->pos.x >= W || p->pos.y < 0 || p->pos.y >= H) {
            spawn(p);
            continue;
        }
        DrawLineV(p->last, p->pos, Fade(toColor(sea(p->k)), 0.7f));
    }
}
const N = 1500;

function spawn(p) {
  p.pos = p.last = Vector2(frand(0, W), frand(0, H));
  p.age = 0;
  p.life = frand(60, 240);
  p.k = frand(0, 1);
}

function field(x, y, t) {
  return noise3(x * 0.004, y * 0.004, t * 0.05) * 4 * PI;
}

function frame(ps, t) {
  fade(PAPER, 12);
  for (let i = 0; i < N; i++) {
    let p = ps[i];
    let a = field(p.pos.x, p.pos.y, t);
    p.last = p.pos;
    p.pos = Vector2Add(p.pos, Vector2(1.5 * cosf(a), 1.5 * sinf(a)));
    if (++p.age > p.life || p.pos.x < 0 || p.pos.x >= W || p.pos.y < 0 || p.pos.y >= H) {
      spawn(p);
      continue;
    }
    DrawLineV(p.last, p.pos, Fade(toColor(sea(p.k)), 0.7));
  }
}