//! Nature of Code, Example 5.11: Flocking. Drag the mouse to add boids. const std = @import("std"); const sk = @import("sketch"); const vec = sk.math; pub const name = "flocking"; pub const width = 640; pub const height = 360; /// Processing's ArrayList grows without bound; State needs a number. const max_boids = 2048; const Boid = struct { position: vec.Vec, velocity: vec.Vec, const r = 3.0; const maxspeed = 3; const maxforce = 0.05; fn init(rng: std.Random, x: f32, y: f32) Boid { return .{ .position = .{ x, y }, .velocity = .{ std.math.lerp(-1, 1, rng.float(f32)), std.math.lerp(-1, 1, rng.float(f32)) } }; } /// flock() + update() + borders(). `boids` includes `self`, and those /// before it have already moved this frame, as in the original. fn run(self: *Boid, boids: []const Boid) void { const sep = vec.scale(self.separate(boids), 1.5); const ali = self.alignment(boids); const coh = self.cohere(boids); self.velocity = vec.clampLength(self.velocity + sep + ali + coh, maxspeed); self.position += self.velocity; self.borders(); } fn seek(self: *const Boid, target: vec.Vec) vec.Vec { const desired = vec.withLength(target - self.position, maxspeed); return vec.clampLength(desired - self.velocity, maxforce); } fn borders(self: *Boid) void { const p = &self.position; if (p[0] < -r) p[0] = width + r; if (p[1] < -r) p[1] = height + r; if (p[0] > width + r) p[0] = -r; if (p[1] > height + r) p[1] = -r; } fn separate(self: *const Boid, boids: []const Boid) vec.Vec { const desired_separation = 25; var steer: vec.Vec = .{ 0, 0 }; var count: f32 = 0; for (boids) |other| { const d = vec.distance(self.position, other.position); if (d > 0 and d < desired_separation) { steer += vec.scale(vec.normalize(self.position - other.position), 1 / d); count += 1; } } if (count > 0) steer = vec.scale(steer, 1 / count); if (vec.length(steer) > 0) steer = vec.clampLength(vec.withLength(steer, maxspeed) - self.velocity, maxforce); return steer; } fn alignment(self: *const Boid, boids: []const Boid) vec.Vec { const neighbor_distance = 50; var sum: vec.Vec = .{ 0, 0 }; var count: f32 = 0; for (boids) |other| { const d = vec.distance(self.position, other.position); if (d > 0 and d < neighbor_distance) { sum += other.velocity; count += 1; } } if (count == 0) return .{ 0, 0 }; return vec.clampLength(vec.withLength(vec.scale(sum, 1 / count), maxspeed) - self.velocity, maxforce); } fn cohere(self: *const Boid, boids: []const Boid) vec.Vec { const neighbor_distance = 50; var sum: vec.Vec = .{ 0, 0 }; var count: f32 = 0; for (boids) |other| { const d = vec.distance(self.position, other.position); if (d > 0 and d < neighbor_distance) { sum += other.position; count += 1; } } if (count == 0) return .{ 0, 0 }; return self.seek(vec.scale(sum, 1 / count)); } }; pub const State = struct { rng: std.Random.Xoshiro256 = .init(11), boids: sk.List(Boid, max_boids) = .{}, clock: sk.Clock = .{}, }; pub fn init(s: *State) void { for (0..120) |_| s.boids.appendBounded(.init(s.rng.random(), width / 2, height / 2)) catch {}; } pub fn update(s: *State, in: *const sk.Input) void { // mouseDragged: a boid per pointer move with the button down. if (in.buttons & 1 != 0) for (in.events) |e| if (e.kind == .mouse_move) { s.boids.appendBounded(.init(s.rng.random(), e.x, e.y)) catch {}; }; for (0..s.clock.steps(in)) |_| { const boids = s.boids.slice(); for (boids) |*boid| boid.run(boids); } } const shape = [_]vec.Vec{ .{ Boid.r * 2, 0 }, .{ -Boid.r * 2, -Boid.r }, .{ -Boid.r * 2, Boid.r } }; /// Every boid's body in one shape command and every outline in another, /// rather than two commands per boid. Processing draws each boid's outline /// right after its body; here all outlines come after all bodies, which /// shows only where boids overlap. pub fn draw(s: *const State, f: *sk.Frame) void { f.clear(sk.Color.white); var bodies: sk.draw.Tessellation = .empty; var outlines: sk.draw.Tessellation = .empty; for (s.boids.constSlice()) |boid| { const xf: vec.Affine = .place(boid.position, vec.angle(boid.velocity)); bodies.fillPolygon(f.scratch, &shape, xf) catch break; outlines.strokePolygon(f.scratch, &shape, 1, xf) catch break; } f.shape(&bodies, .gray(127)); f.shape(&outlines, .black); }