Published
Edited
Jul 6, 2022
4 stars
Insert cell
Insert cell
vis = {
// simulation
const sim = new AA.Simulation({
width: 920,
height: 600,
gridStep: 40,
beforeTick: () => sim.state.workTime = sim.tickIndex % 1800 < 900,
});
invalidation.then(() => sim.end());
// house, grass, road
const xHouse = new Set([1, 3, 5, 9, 11, 13, 17, 19, 21]);
const yHouse = new Set([1, 3, 5, 7]);
const xRoad = new Set([7, 15]);
const yRoad = new Set([2, 6]);
for (let x = 0; x <= 22; x++) {
for (let y = 0; y <= 8; y++) {
sim.squareAt(x, y).label('land',
xRoad.has(x) || yRoad.has(y)
? 'road'
: xHouse.has(x) && yHouse.has(y)
? 'house'
: 'grass'
);
}
}
const houses = sim.withLabel('land', 'house');
const grass = sim.withLabel('land', 'grass');

// works
const works = [
[0, 4, 11, 14],
[6, 7, 12, 14],
[9, 17, 14, 14],
[21, 22, 9, 11],
[20, 22, 13, 14],
].map(indexLimits => {
return new AA.Zone({indexLimits})
.label('land', 'work')
.vis({image: 'bricks-gray.png', tile: true})
.addTo(sim);
});
for (let sq of sim.squaresInRect([0, 22, 9, 14])) {
sq.label('land', 'stone');
}

// people
for (let house of houses) {
house.populate({
n: AA.random.int(1, 10)(),
padding: 4,
radius: 3.5,
setup: p => {
p.maxSpeed = AA.random.uniform(1.2, 1.7)();
p.state = {
house,
work: works[AA.random.int(works.length)()],
}
p.label('person', true);
p.vis({image: 'boid.png'})
}
});
}
const people = sim.withLabel('person');

// 'go behavior' house <-> work
const paths = sim.paths(
[
...works.map(w => [w, w]), // to works
...[...houses].map(h => [h, h]) // to houses
],
[
[grass, Infinity]
]
);
for (let p of people) {
p.steer.set('go-work', {
disabled: () => !sim.state.workTime,
behavior: 'go',
paths: paths.get(p.state.work)
});
p.steer.set('go-home', {
disabled: () => sim.state.workTime,
behavior: 'go',
paths: paths.get(p.state.house)
});
}

// repel forces
sim.interaction.set('person-person-repel', {
group1: people,
behavior: 'repel',
off: 3,
strength: 1
});
sim.interaction.set('grass-person-repel', {
group1: grass,
group2: sim.actors,
behavior: 'repel',
off: 10,
strength: 2
});
sim.interaction.set('stay-at-home', {
disabled: () => sim.state.workTime,
group1: houses,
group2: sim.actors,
behavior: 'repel',
inward: true,
off: 5,
strength: 1
});
sim.interaction.set('stay-at-work', {
disabled: () => !sim.state.workTime,
group1: works,
group2: sim.actors,
behavior: 'repel',
inward: true,
off: 5,
strength: 1
});

// vis
const squareImageLookup = {
grass: 'nature-grass.png',
road: 'road.png',
stone: 'stones.png',
house: 'bricks-yellow.png'
};
for (let sq of sim.squares) {
sq.zIndex = -Infinity;
sq.vis({image: squareImageLookup[sq.label('land')]});
}
return AV.visObs(sim, {
images: [
'https://cdn.jsdelivr.net/gh/gjmcn/sprites/sprite-sheets/outside.json',
'https://cdn.jsdelivr.net/gh/gjmcn/sprites/sprite-sheets/shapes.json'
],
});
}
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more