I built a 3D Formula-style racing game in the browser (with online multiplayer) and the bugs nearly broke me
I wanted to see how far a browser could go as a racing game: real 3D cars, tyre wear, DRS, weather, a championship, and online races with friends. No install, no app store, just a link. Play it here: apex-gp9.vercel.app
I wanted to see how far a browser could go as a racing game: real 3D cars, tyre wear, DRS, weather, a championship, and online races with friends. No install, no app store, just a link.
Play it here: apex-gp9.vercel.app
What it is
Apex GP is a free 3D open-wheel racing game that runs in the browser on phone, tablet and desktop.
- 22 cars on the grid, tyre wear, DRS, rain and changing weather
- Single race, quick race and championship modes
- Online races with friends using a room code
- A podium ceremony with trophies and champagne
- Touch controls, tilt steering, and a keyboard mode on desktop
How I built it
- three.js for all the 3D: cars, track, scenery, drivers and the podium
- Plain JavaScript modules and CSS, no framework. I wanted full control over the frame loop
- PeerJS (WebRTC) for multiplayer. It only does the first handshake, then the players' devices talk directly
- A rigged driver model with my own animation system, so drivers can sit in the cockpit, celebrate, and adjust their helmets
- Vercel for hosting
The hard parts (and how I got through them)
1. The multiplayer bug where everyone thought they won
This one took the longest to understand. In an online race, if my friend won, my screen never showed the right result. And sometimes we both saw ourselves as P1 with different times.
I assumed it was a timing problem. It wasn't. The real cause was tiny:
- Every car sends its position about 20 times a second.
- When a player crossed the finish line, the "I finished" message was stored in the same record as the position data.
- The very next position packet overwrote the record and erased the finish message.
So the other phone never learned that anyone had finished. Each phone decided on its own that it had won.
The fix: I stopped letting each device decide.
- Every player reports their finish time to the host.
- The host adds half the connection delay for guests, since their clocks started slightly later.
- The host sorts everyone and sends one official result that every phone shows.
- A player who disconnects mid-race is classified as DNF, so they never block the results.
I tested it with several simulated clients against a local PeerJS server, and all of them printed the identical classification.
2. A driver animation that looked awful
My driver-select animation had hands crossing the chest and going through the head. It looked cringe, and the podium dance was worse.
I stopped guessing at numbers. I rendered the scene in a headless browser, took screenshots of each pose, and adjusted the hand targets until nothing overlapped. The final version is just a helmet adjustment plus a small cartoon look-around. Less animation looked far better.
3. A UI that grew messy
After many updates my CSS was a pile of overrides stacked on overrides. Glass panels, mixed button styles, and everything tiny on tablets.
I added one design layer loaded last, with a single size unit that scales to the screen:
- solid dark panels with a thin orange edge, no blur
- angled orange buttons for main actions
- segmented option bars where the selected choice is filled orange
- labelled GAS and BRAKE pedals for touch
Tablets finally got properly sized controls instead of tiny phone UI.
How I stayed calm when it all broke
- Reproduce before you fix. I built small tests for the network logic instead of guessing.
- Look at it, don't imagine it. Screenshots of every screen at phone and tablet sizes showed problems I had stopped noticing.
- One change at a time. When I changed the podium, the animations and the UI together, I could not tell what broke what.
- Walk away from the stubborn bug. The finish-message bug made sense the moment I came back to it.
What I'd do differently
- Make the host the authority for results from day one, not as a patch.
- Start with one UI system instead of fixing screens one by one.
- Automate screenshot checks earlier.
Try it
If you like racing games or three.js, give it a go and race a friend online:
I'd love to hear what you think, especially bugs and ideas for the next update. Thanks for reading!
Built by Nandan Das.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.