Skip to content
Go back

Traffic Intersection Simulator

3 min read

Traffic Intersection Simulator 🚦

This post demonstrates and compares two different approaches to simulating a traffic intersection in React:


Classic Logic-Based Simulator

This approach models each car as an object with a continuous position and explicit state (approaching, waiting, crossing, etc.). Cars move smoothly along the road, obey traffic lights, and maintain realistic gaps. The logic is more complex but allows for fine-grained control and smooth animation.

Traffic Intersection Simulator

NSEW

2D Grid-Based Simulator

This approach represents the intersection as a 2D grid. Cars occupy discrete cells and move from cell to cell, with traffic lights controlling entry into the intersection. The logic is simpler and more extensible for grid-based or cellular automata simulations. Animation is handled by interpolating car positions between cells.


Comparison

FeatureClassic Logic-Based2D Grid-Based
Car MovementSmooth, continuousDiscrete, cell-to-cell
Car StateExplicit (waiting, crossing, etc.)Implicit (by cell position)
VisualsRealistic, SVG roads/carsStylized, grid overlay
Logic ComplexityHigher (more conditions)Lower (simple grid rules)
ExtensibilityGood for realismGood for grid/CA research
AnimationCSS/SVG, per-pixelInterpolated per cell
Best ForRealistic traffic, demosGrid/cellular automata, teaching

When to Use Each


Try both simulators above and see which approach fits your needs!


Author’s Note

I have to admit, I really struggled to “vibe code” the first (classic logic-based) approach. Getting all the car states, edge cases, and smooth movement to work together was a lot trickier than I expected. It felt like every time I fixed one bug, another would pop up! In contrast, the grid-based approach just clicked for me—breaking the world into cells made the logic so much more manageable and fun to experiment with. If you ever feel stuck, try changing your model or abstraction—it might just unlock your creativity!


Share this post on:

Next Post
Kubernetes Quiz: Test Your Knowledge!