diff options
Diffstat (limited to 'examples/noc/src/flow_field.zig')
| -rw-r--r-- | examples/noc/src/flow_field.zig | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/examples/noc/src/flow_field.zig b/examples/noc/src/flow_field.zig new file mode 100644 index 0000000..0452c56 --- /dev/null +++ b/examples/noc/src/flow_field.zig @@ -0,0 +1,104 @@ +//! Nature of Code, Example 5.4: Flow Field Following. Space toggles the +//! field's arrows; a click makes a new field. +const std = @import("std"); +const sk = @import("sketch"); +const vec = sk.math; + +pub const name = "flow_field"; +pub const width = 640; +pub const height = 360; + +const resolution = 20; +const cols = width / resolution; +const rows = height / resolution; + +/// The field is a function of its seed, so State keeps only the seed: +/// Processing's `noiseSeed(random(10000))`. +const FlowField = struct { + seed: u32 = 0, + + fn init(rng: std.Random) FlowField { + return .{ .seed = rng.uintLessThan(u32, 10000) }; + } + + /// The arrow in column `i`, row `j`. + fn at(ff: FlowField, i: usize, j: usize) vec.Vec { + const xoff: f32 = @floatFromInt(i); + const yoff: f32 = @floatFromInt(j); + return vec.unit(std.math.lerp(0, std.math.tau, vec.noise(ff.seed, xoff * 0.1, yoff * 0.1, 0))); + } + + fn lookup(ff: FlowField, position: vec.Vec) vec.Vec { + const column: usize = @intFromFloat(std.math.clamp(@floor(position[0] / resolution), 0, cols - 1)); + const row: usize = @intFromFloat(std.math.clamp(@floor(position[1] / resolution), 0, rows - 1)); + return ff.at(column, row); + } +}; + +const Vehicle = struct { + position: vec.Vec, + velocity: vec.Vec = .{ 0, 0 }, + maxspeed: f32, + maxforce: f32, + const r = 4; + + fn follow(v: *Vehicle, flow: FlowField) void { + const desired = vec.scale(flow.lookup(v.position), v.maxspeed); + const steer = vec.clampLength(desired - v.velocity, v.maxforce); + // applyForce + update: acceleration lasts one step, so it is not State. + v.velocity = vec.clampLength(v.velocity + steer, v.maxspeed); + v.position += v.velocity; + } + + fn borders(v: *Vehicle) void { + const p = &v.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; + } +}; + +pub const State = struct { + rng: std.Random.Xoshiro256 = .init(5), + debug: bool = true, + flowfield: FlowField = .{}, + vehicles: [120]Vehicle = undefined, + clock: sk.Clock = .{}, +}; + +/// setup(): defaults cannot call the PRNG, so the random parts happen here. +pub fn init(s: *State) void { + const rng = s.rng.random(); + const lerp = std.math.lerp; + s.flowfield = .init(rng); + for (&s.vehicles) |*v| v.* = .{ + .position = .{ lerp(0, width, rng.float(f32)), lerp(0, height, rng.float(f32)) }, + .maxspeed = lerp(2, 5, rng.float(f32)), + .maxforce = lerp(0.1, 0.5, rng.float(f32)), + }; +} + +pub fn update(s: *State, in: *const sk.Input) void { + if (in.pressed(sk.Key.space)) s.debug = !s.debug; + for (in.events) |e| if (e.kind == .mouse_down) { + s.flowfield = .init(s.rng.random()); + }; + for (0..s.clock.steps(in)) |_| for (&s.vehicles) |*v| { + v.follow(s.flowfield); + v.borders(); + }; +} + +const arrow: sk.draw.Pen = .{ .stroke = .black, .width = 1 }; +const body: sk.draw.Pen = .{ .fill = .gray(127), .stroke = .black, .width = 2 }; +const triangle = [_]vec.Vec{ .{ Vehicle.r * 2, 0 }, .{ -Vehicle.r * 2, -Vehicle.r }, .{ -Vehicle.r * 2, Vehicle.r } }; + +pub fn draw(s: *const State, f: *sk.Frame) void { + f.clear(sk.Color.white); + if (s.debug) for (0..cols) |i| for (0..rows) |j| { + const centre = vec.scale(.{ @floatFromInt(i), @floatFromInt(j) }, resolution) + vec.splat(resolution / 2); + sk.draw.line(f, centre, centre + vec.scale(s.flowfield.at(i, j), resolution / 2), arrow); + }; + for (&s.vehicles) |*v| sk.draw.polygon(f, &triangle, body.transformed(.place(v.position, vec.angle(v.velocity)))); +} |
