Quick Answer: A browser sand simulator runs its grain physics on the GPU using WebGL2, updating a grid of state values every frame and then drawing the result as sand. Phones add tilt and shake input through the DeviceOrientation and devicemotion events. The Safe Sand project at av.github.io/safe-sand does this in a normal browser tab, with no install.

Most sand in games looks like colored confetti that falls and stops. Then you pour a heap, tilt the phone, and the pile slumps the way real sand does. The Safe Sand project is a browser sand simulator built with WebGL2 and JavaScript. You can push grains with your finger, tilt to make them slide, or shake to scatter them. Here's what's probably happening under the hood, and why it's harder than it looks.

What the Project Actually Does (and What the Page Doesn't Say)

The page lists five interactions: Pour, Lay it flat, Tilt, Shake, and Draw. It also has toggles for sound and haptics, and it claims to run on phones, tablets, and desktops. That's a solid feature set for a browser toy.

What it doesn't publish is the algorithm. Everything below about internals is inference from what the features require, not a reading of the source code. If you're deciding whether to copy the approach, treat it as a well-informed hypothesis and verify it yourself.

Why Sand Is Hard to Fake

Sand is thousands of grains that interact through friction and collisions. A pile holds together because each grain rests on its neighbours, and it stays stable only up to a certain slope. That slope is the angle of repose, and dry sand typically settles somewhere around 30 to 35 degrees.

Here's the cause-and-effect chain that makes simulation tricky. When a grain loses support, it slides into an adjacent empty cell. That shift removes support from the grains above it, and the avalanche propagates. You don't program the avalanche directly. You need local rules that produce it as a global behaviour.

The common advice is to treat every grain as a rigid body in a physics engine like Box2D or Bullet. That advice is incomplete. Rigid-body engines handle contacts well, but the contact count grows fast with thousands of grains, and a phone's frame budget disappears quickly. Most real-time sand toys avoid per-grain physics for exactly this reason.

Grid Simulation vs. Particles: The Core Trade-off

Three main approaches show up in browser sand projects. Each one makes a different trade.

ApproachHow it worksStrengthWeakness
Rigid-body particles (Box2D, Bullet)Each grain is a physics bodyAccurate collisions and frictionContact count scales badly; heavy on mobile
Falling-sand cellular automataEach cell is sand, air, or wall; per-frame rules move cellsCheap, huge grain counts, stable pilesBlocky look; repose depends on rule design
HeightfieldStore one surface height per columnVery fast; good for piles and slopesNo overhangs, no pouring into holes

Cellular automata are the approach most people associate with the genre. The Noita falling sand engine from Nolla Games popularised pixel-rule physics in a mainstream game, and the same family of rules powers many browser toys. The GPU version of this idea is where WebGL2 comes in.

Here's where most guides go wrong: they present the cellular approach as a simpler version of real physics. It's really a different model. You're tuning rules until the behaviour looks right, not solving equations.

How WebGL2 Makes the Grid Fast

WebGL2 is based on OpenGL ES 3.0, and it gives you float textures, multiple render targets, and instanced drawing. What it doesn't give you is compute shaders. Those belong to WebGPU, which browsers are still rolling out. So a WebGL2 simulation uses fragment shaders as its parallel workers, typically with a technique called ping-pong.

The loop works like this:

  1. Read the previous state from one texture. Each texel represents a cell.
  2. A fragment shader computes the cell's new value from its neighbours.
  3. Write the result into a second framebuffer.
  4. Swap the two buffers, then run a shading pass to draw the sand.

Every pixel runs step 2 in parallel, which is why a grid of hundreds of thousands of cells can run at interactive rates. The practical tip: keep the state texture as compact as the simulation allows. Smaller formats cost less bandwidth, and mobile GPUs feel that cost first.

A failure mode that catches people: float render targets in WebGL2 require the EXT_color_buffer_float extension. Without checking for it, the framebuffer is incomplete and you get a black screen on some devices. You'll see it on a laptop, then discover it on a phone. Check the extension and fall back gracefully.

Making Tilt and Shake Feel Real

Tilt uses the DeviceOrientation API. Shake uses devicemotion, which reports acceleration. On iOS 13 and later, both require explicit permission through DeviceMotionEvent.requestPermission(), and that call has to come from a user gesture like a button tap. If you call it on page load, it silently fails.

The common advice says to map tilt angle straight onto gravity. That's incomplete. Raw sensor readings are noisy, and a direct mapping makes grains jitter whenever your hand trembles. Smooth the readings with a low-pass filter, and add a small dead zone so a resting phone doesn't keep the pile moving. Both are standard signal-processing moves, and they matter more here than the physics itself.

Haptics deserve a caveat. The Vibration API (navigator.vibrate) is not supported in iOS Safari as far as I'm aware, so a haptics toggle may do nothing on iPhones. Check support at runtime and hide the control when it's unavailable. Don't leave a switch that silently does nothing.

Why Sand Looks Fake (and What Actually Fixes It)

Why does sand look fake in most games? Usually the answer is colour and shading, not grain count. Uniform colour, identical sprites, and flat lighting make even a correct pile look like a plastic toy.

The fixes are mostly cheap. Add per-grain colour variation. Use a normal map or simple directional lighting so slopes catch light. Darken the crevices where grains meet, which approximates ambient occlusion. None of these require more physics. In practice, the look comes from shading, not from simulating more grains.

That's a design observation, not a measured result, so test it on your own pile before betting a project on it.

Trade-offs and Limits You Should Know

Be realistic about what a browser sand simulator can do:

  • Performance varies widely. Flagship phones and low-end Android devices can differ by a large factor. The page doesn't publish benchmarks, so expect to test on hardware you own.
  • Cohesion is hard. Damp sand clumps and holds steep walls. Basic cellular rules struggle with this unless you add extra state per cell.
  • Audio needs a gesture. Browsers block autoplay, so the sound toggle has to start after a user interaction.
  • Edge cases multiply on touch. Multi-touch pours and palm rejection on tablets are where most of the bugs live.

None of these are dealbreakers. They're the difference between a demo that impresses for thirty seconds and one people keep playing.

Frequently Asked Questions

what is a browser sand simulator?

A browser sand simulator is a web app that models sand grains and their interactions in real time, using JavaScript and the page's graphics API. Most use WebGL2 to run the simulation on the GPU. You open a URL and play, with no download or install required.

how does a browser sand simulator work with WebGL2?

It stores the sand state in textures and updates them every frame with fragment shaders. Two framebuffers swap roles each frame, a technique called ping-pong. Each cell reads its neighbours and computes its next state, then a shading pass draws the result as visible sand.

why does sand look fake in most games?

Usually because of uniform colour, flat lighting, and repeated sprites rather than wrong physics. Adding per-grain colour variation, directional lighting, and darkening in crevices makes a pile read as real sand. These changes are cheap compared with simulating more grains.

how do I enable tilt controls on an iPhone?

On iOS 13 and later, call DeviceMotionEvent.requestPermission() (and the orientation equivalent) from a button tap, not on page load. Once granted, listen for deviceorientation and devicemotion events. Smooth the readings with a low-pass filter to avoid jitter.

can I build my own sand simulation in WebGL2?

Yes. Start with a small grid, a falling-sand rule set, and two float textures for ping-pong. Check EXT_color_buffer_float before relying on float targets. Test on a real phone early, because mobile GPUs expose problems that desktop hardware hides.

Start with a grid and local rules before you reach for a physics engine, since the grid model is what makes a browser sand simulator fast enough for phones. Open the Safe Sand simulator on your phone this week, pour a heap, then tilt until it slides and note how that angle compares to real sand. Pass this to a friend who's been trying to make sand look right in their own browser game.