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
|
//! 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);
}
|