Vision cone

Aim across the middle cabinet. When the patrol disappears behind it, switch off occlusion and compare the same view.
Interactive demo#TopDownDarkwood
A sector that marks how far and how wide a character can see.
In this Darkwood-style room the dim floor and cabinets stay visible everywhere. A second pass draws the room and its patrol through the vision cone. Each solid cabinet cuts a wedge out of that cone. As the patrol passes behind a corner, only the part of the courier with a clear line to the observer is revealed.
Code
namespace vision_cone_lesson;
import stage;
import std;
import math;
var patrol = CourierTurn {};
var body = CourierTurn {};
const eye_x = 55.0;
const eye_y = 52.0;
const reach = 68.0;
struct Cabinet { x: f64, y: f64, w: f64, h: f64 }
let cabinets = [Cabinet { x: 23, y: 20, w: 20, h: 12 },
Cabinet { x: 79, y: 46, w: 11, h: 12 },
Cabinet { x: 71, y: 78, w: 16, h: 12 }];
fn room() {
tiles();
for c in cabinets { cabinet(c.x, c.y, c.w, c.h); }
}
// Extend a near edge away from the eye until its far edge lies beyond the cone.
fn block_edge(ax: f64, ay: f64, bx: f64, by: f64, distance: f64) {
let scale = 1 + reach / distance;
clip_out_quad(ax, ay, bx, by,
eye_x + (bx - eye_x) * scale, eye_y + (by - eye_y) * scale,
eye_x + (ax - eye_x) * scale, eye_y + (ay - eye_y) * scale);
}
fn block_cabinet(c: Cabinet) {
let right = c.x + c.w;
let bottom = c.y + c.h;
if eye_x < c.x { block_edge(c.x, c.y, c.x, bottom, c.x - eye_x); }
if eye_x > right { block_edge(right, c.y, right, bottom, eye_x - right); }
if eye_y < c.y { block_edge(c.x, c.y, right, c.y, c.y - eye_y); }
if eye_y > bottom { block_edge(c.x, bottom, right, bottom, eye_y - bottom); }
}
export "vision-cone" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
let target = if hover { atan2(py - eye_y, px - eye_x) } else { 0.18 * sin(time * 0.9) };
let aim = steer_courier(body, time, eye_x, eye_y, 0, 0, target);
room();
rect(0, 0, W, H, "rgba(10,19,32,0.65)");
// Reveal a second copy of the room and its moving occupant through the remaining mask.
save();
clip_sector(eye_x, eye_y, aim, number_control("width", 66) * PI / 180.0, reach);
if toggle("occlusion") {
for c in cabinets { block_cabinet(c); }
}
room();
rect(0, 0, W, H, "rgba(163,236,213,0.27)");
let phase = time * 0.85;
let facing = atan2(23 * cos(phase), -6 * sin(phase));
steer_courier(patrol, time, 102 + 6 * cos(phase), 52 + 23 * sin(phase), facing, facing, facing);
draw_steering_courier(patrol);
restore();
draw_steering_courier(body);
}

Comments
No comments yet.