Sagar Web Games

Home › Devlog

Table tennis physics in a browser: spin, bounce and a fixed time step

2026-11-29 · Sagar Studio

Table tennis is a game of spin. A topspin loop dips and kicks forward off the table, a backspin chop floats and dies, and a sidespin serve curves in the air. To make that feel right in Table Tennis Darbar we did not animate the ball along hand-drawn paths. We wrote a small physics simulation and let the ball obey it. This post walks through how it works.

A world in metres

The simulation is plain code with no graphics in it. It measures in metres: x runs across the table, y goes up from the table top and z runs along the table, with the net at z = 0. The table is 1.525 metres wide and 2.74 metres long, the net is 15.25 centimetres high, and the ball has a radius of 2 centimetres.

Flight: gravity, drag and spin

Every step, the ball's velocity is changed by three effects. Gravity pulls it down. Air drag slows it, in proportion to the square of its speed, so a fast ball loses speed quickly and a slow one hardly at all. And the Magnus effect, the push a spinning ball feels from the air, adds a force at right angles to both the spin and the direction of travel. Topspin pushes the ball down, so it dips and lands shorter than a flat ball. Backspin pushes it up, so it floats over the net. Sidespin pushes it sideways, so it curves, and the opposite spin curves it the other way. Spin fades slowly in the air and is capped at a maximum.

Bouncing on the table and the net

When the ball reaches the table, the simulation first works out exactly when during the step it crossed the surface, so a fast ball cannot skip through. The bounce has two parts. Vertically it keeps most of its speed, with a restitution of 0.9. Horizontally, the question is how fast the ball's surface is sliding along the table. That depends on both the ball's speed and its spin, so a spinning ball has a different slip from a non-spinning one. Friction pushes against the slip, up to a limit, and the push does two things: it changes the ball's forward speed and it changes its spin. That is why topspin kicks forward off the table and backspin slows. A ball that lands within about a centimetre of the table's edge gets a small random deflection, because edge balls in real life are unpredictable.

The net is checked at the moment the ball crosses the plane z = 0. If it passes over, nothing happens. If the ball is clearly below the top, it hits the net and falls back, slow. If it only grazes the top, within a few millimetres, it counts as a cord: it loses speed, pops up and wobbles off in a slightly random direction, and may fall either side. A cord on a serve that then lands on the receiver's side is a let, and the serve is replayed.

The paddle, and how the computer aims

A stroke is a collision between the ball and the paddle face, and a paddle's rubber is described by two numbers: how bouncy it is and how grippy. The face has a direction it points and a velocity. The collision gives the ball a normal push away from the face, scaled by the bounce number, and a friction push along the face, limited by the grip number. That friction acts on the ball's surface, so it spins the ball. Brush up and forward across the ball and you get topspin; brush down and forward and you get backspin. Grippier rubber makes more spin; bouncier rubber makes a faster ball.

The computer players decide where they want the ball to land, how hard to hit and how much spin to put on it. The game then has to find a paddle angle that does it. It solves this by trying: it tests a set of candidate angles, for each one running the contact and then flying a copy of the ball forward to see where it lands, and picks the angle that lands closest to the target, refining between the best neighbours. If the power is too high for the ball to land on the table, it reduces the power in steps, to roughly 70 percent, as a good player would. Mistakes come from contact quality, which depends on how far from the paddle's centre, how high above the table and how well timed the contact was. A poor contact nudges the paddle's angle and speed, and weaker players are noisier.

A fixed step makes it repeatable

The simulation advances in steps of exactly 1/240 of a second. The game keeps a running total of elapsed time, adds the time since the last frame, and runs as many whole steps as that covers, so the same match plays out the same on a fast desktop and an older phone. The game also runs at 62 percent of real speed so that a thumb can keep up.

Anything random in the simulation, such as edge balls and cord touches, comes from a seeded random number generator that is part of the simulation. With the same seed and the same decisions, the same match happens every time. That is what lets a test play thousands of matches and lets us reproduce one that went wrong.

What the checks prove, and what we simplified

Our checks test the physics as physics. Over 300 random balls, flying without a paddle touching them, the ball's energy never goes up from one step to the next, apart from net, edge and floor contacts. At very high speeds, none of 400 fast balls passes through the table or the net. Topspin lands shorter than a flat ball, backspin clears the net by more than 3 centimetres higher than a flat ball, and left and right sidespin curve in opposite directions. Serves the computer plans are legal more than 95 percent of the time, and a ball that only just clears the net does sometimes touch the cord. The scoring rules are tested too: 11 points, serve changing every two points, deuce at 10-10 with the serve alternating, and a win needs a two-point lead.

This is a game, not a laboratory. Each player hits from a fixed plane a little behind the end of the table, apart from short balls, which they step in to meet. The paddle is a flat face with two numbers for its rubber. Air is the same everywhere, and every shot is a single contact with that flat face.

About Table Tennis Darbar