Visualizations
165 / 500

165 · Algorithms

Particle Filter Robot

Thousands of pose guesses condense out of fog onto a lost robot's true position.

A robot with a noisy laser rangefinder wanders a blueprint floor plan without knowing where it is. Its belief is a cloud of particles, each a guess at its position and heading: every move shifts them all by the odometry plus noise, every scan weights each one by how well the readings fit the map from its pose (the likelihood field model, using a precomputed distance transform), and low-variance resampling keeps the good guesses. The four labs are the same room translated or rotated, so the belief splits into eerie identical clusters until the robot reaches the corridor and the hall and sees something unique. Adaptive tempering keeps those clusters from collapsing by luck, and augmented MCL sprays fresh random guesses when the readings stop making sense, which is how it recovers from being kidnapped.

Try it. Click any free spot on the plan to kidnap the robot there and watch the filter notice and recover. Scatter resets the belief to uniform fog, and the beam button switches between 12, 36 and 72 laser beams. Click the view to drive with the arrow keys or WASD; K kidnaps, R scatters, B changes the beams. Left alone it explores, and kidnaps itself every half minute.

  • Monte Carlo localization
  • Likelihood field sensor model
  • Euclidean distance transform
  • Adaptive tempering and low-variance resampling
  • Augmented MCL kidnap recovery

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a robot localization demo in JavaScript and the HTML canvas element, where a cloud of particles figures out where a lost robot is. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Draw a floor plan from wall rectangles in metres on a sharp, high-DPI canvas, and rasterize the walls into an occupancy grid at 10 cm per cell.
- Add a round robot that drives between waypoints. Give it a 360 degree laser: cast 24 rays by stepping through the grid until each hits a wall, add a little Gaussian noise, and draw the rays.
- Create 3,000 particles, each a random (x, y, heading) in free space. When the robot moves, move every particle by the same forward distance and turn in its own frame, plus random noise.
- Weight each particle by how well the scan fits from its pose, using a likelihood field: precompute each grid cell's distance to the nearest wall, project each beam's endpoint from the particle's pose, and score it with a Gaussian of that distance. Sum the log scores.
- Resample with the low-variance method: lay N evenly spaced pointers over the cumulative weights and copy the particles they land on.

Once that works, make it beautiful:
- Blueprint styling: deep blue background, a fine grid, glowing white walls, dashed furniture outlines. Draw particles as tiny amber heading ticks with additive blending so dense clusters glow.
- Make two rooms identical and watch the belief split into two clusters until the robot leaves them.
- Add a kidnap button that teleports the robot, and recover by injecting a few random particles whenever the average likelihood suddenly drops.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as KLD sampling to adapt the particle count, a proper beam model with raycasting per particle, or building the map at the same time (SLAM).
PreviousVocal TractA physically modeled throat that sings vowels, drawn as a midsagittal anatomy plate. NextSPH WaterThousands of particles that slosh, splash and pour like a liquid.

Related visualizations

  • Rebound RobotsAlgorithms Robots slide until they hit something. A* finds the fewest moves to the target.
  • Lidar Scan3D A spinning lidar maps a forest point by point, with ICP SLAM fixing its drift.
  • Diffusion SketchpadMachine learning A diffusion model trains live and condenses pure noise into whatever shape you draw.
  • Blocks World PlannerAlgorithms A robot arm plans with A* over STRIPS states, then stacks the blocks to match your goal.
  • Ghost RacersMachine learning Sixty neural-net cars evolve to race a track, then face one you draw.
  • Evolution Strategy LandersMachine learning Fifty ghost landers learn soft touchdowns by natural evolution strategies.
  • Particle LifeEmergence Colored particles with simple attractions that assemble into living cells.
  • Spatial IndexesAlgorithms Quadtree, k-d tree, BVH and grid rebuilt every frame over thousands of moving points.
  • Sokoban ForgeAlgorithms Crate puzzles forged by playing backward from the goals, then solved by A*.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs