← Games

Game Hub Articles

All articles
Home › Articles
🍎

How game physics works: gravity, bouncing and collisions in a few lines of code

By Noam · 25 November 2026 · 8 min read

When a character jumps and lands, a ball bounces off a wall, or a tower of blocks falls over, the game is doing physics. You might expect that to need hard maths, but most games use a few simple rules, repeated many times a second. Here's how they work, with small pieces of JavaScript you can try.

Position and velocity

Every moving object has a position (x, y) and a velocity (vx, vy): how far it moves each frame. Each frame, the game adds the velocity to the position:

ball.x += ball.vx;
ball.y += ball.vy;

That's all that's needed for something to move in a straight line. On a screen, y usually gets bigger as you go down, so a positive vy means falling.

Gravity

Gravity doesn't change where something is; it changes how fast it's moving. So each frame, we add a little to the downward velocity:

const GRAVITY = 0.5;
ball.vy += GRAVITY;   // speed up downwards a bit every frame
ball.y  += ball.vy;

A jump is just giving the character a big upward velocity once (for example vy = -10). Gravity then slows it down, stops it at the top and pulls it back. That's why game jumps make a curved arc, the same shape as a thrown ball in real life.

Bouncing

To bounce off the floor, check whether the ball went below it, put it back on the floor, and flip its vertical velocity:

if (ball.y + ball.r > floorY) {
  ball.y = floorY - ball.r;
  ball.vy = -ball.vy * 0.8;   // 0.8 = loses some energy each bounce
}

The 0.8 is called the restitution. With 1 the ball bounces forever; with 0 it just stops. Real balls are somewhere in between, which is why each bounce is a bit lower.

Friction and air resistance

To make things slow down naturally, multiply the velocity by a number just under 1 each frame:

ball.vx *= 0.99;

This one line is how ice levels feel slippery (0.995) and mud levels feel sticky (0.8).

Collisions

Games need to know when two things touch. The two most common checks are very short.

Two circles touch when the distance between their centres is less than their two radiuses added together:

const dx = a.x - b.x, dy = a.y - b.y;
const touching = Math.hypot(dx, dy) < a.r + b.r;

Two rectangles (often called boxes) overlap when neither one is completely to the left, right, above or below the other:

const touching = a.x < b.x + b.w && a.x + a.w > b.x &&
                 a.y < b.y + b.h && a.y + a.h > b.y;

Many games use simple boxes and circles even for complicated-looking characters, because these checks are fast and players rarely notice.

Frames and time

Browsers usually draw about 60 frames a second, but some screens run faster. To keep the game the same speed everywhere, careful games multiply movement by the time since the last frame (often called dt). Otherwise a game could run twice as fast on a 120 Hz screen.

Bigger physics engines

Games with piles of blocks that topple and spin, like Castle Smash, add rotation, mass and lots of collisions solved together. Programmers often use a ready-made physics engine for that. But every engine is built on the same ideas as the few lines above: move by velocity, change velocity with forces, and fix things up when objects overlap.

See it in action

Watch for these rules next time you play: the arc of a shot in Hoop Shot, the bounces in Brick Smash, or blocks tumbling in Castle Smash. If you want to try writing your own, our guide to making your first game is a good place to start.

▶ Play Castle SmashAll games

More articles

🪩 Pinball tips: how to trap the ball, aim your shots and score moreStop flailing at the flippers. Learn to catch the ball, aim with the flipper, pass it between flippers and use nudges, plus a bit of pinball history.🐍 Snake strategy: how to get a really long snakeHug the walls, sweep in zig-zags and never trap yourself. Simple strategies for the classic Snake game, plus where Snake came from.🎳 Bowling tips and how bowling scoring really worksWhy strikes and spares are worth extra, how a 300 game works, and where to aim to knock down all ten pins.🧱 Brick-breaking games: the story of Breakout and how to clear every levelHow a 1976 arcade game built by two future Apple founders created a whole genre, and how to control the ball's angle to clear bricks faster.🟦 Falling-block puzzle tips: keep your stack flat and clear more linesFlat stacks, a well for the long piece, using Hold and Next, and staying calm at high speed. How to get better at falling-block puzzles like Block Drop.