xxxxxxxxxx
108
// TODO:
// - add corners to rotation-dependent buffers
// - fix rotation handle and line segment on reshape
let obstacle, agent;
let inflateSlider;
let inflatedRegionCheck, offsetEdgesCheck;
function setup() {
createCanvas(400, 400);
inflateSlider = createSlider(0, 32);
inflateSlider.position(10, 10);
inflateSlider.size(80);
offsetEdgesCheck = createCheckbox();
offsetEdgesCheck.position(10, 25);
inflatedRegionCheck = createCheckbox();
inflatedRegionCheck.position(30, 25);
agentOffsetEdgeCheck = createCheckbox();
agentOffsetEdgeCheck.position(50, 25);
const handleRadius = 10;
obstacle = new Obstacle([
{x: 150, y: 50},
{x: 300, y: 50},
{x: 300, y: 350},
{x: 250, y: 350},
{x: 250, y: 100},
{x: 150, y: 100},
], handleRadius);
agent = new Agent([
{x: 50, y: 300},
{x: 90, y: 300},
{x: 70, y: 340},
], handleRadius);
}
function draw() {
const inflate = inflateSlider.value();
background(220);
obstacle.draw(inflate, inflatedRegionCheck.checked(), offsetEdgesCheck.checked());
// Draw agent-rotation-dependent buffer
// for each obstacle
// for each edge
// for each agent vertex
// represent vertex in frame defined by edge (rotate)
// get furthest points in local frame y
const segmentDiffs = [];
const n = obstacle.points.length;
for (let idx = 0; idx < n; idx++) {
const curr = obstacle.points[idx].position;
const next = obstacle.points[(idx + 1) % n].position;
const segmentAngle = p5.Vector.sub(next, curr).heading();
const newPoints = agent.points.map(p => p.position.copy().rotate(segmentAngle));
const minY = min(newPoints.map(p => p.y));
const maxY = max(newPoints.map(p => p.y));
segmentDiffs.push(abs(maxY - minY));
}
if (agentOffsetEdgeCheck.checked()) {
const ps = obstacle.points.map(p => p.position);
drawInflatedRegion(ps, segmentDiffs);
}
agent.draw();
}
function mousePressed() {
obstacle.handleMousePressed();
agent.handleMousePressed();
}
function mouseReleased() {
obstacle.handleMouseReleased();
agent.handleMouseReleased();
}