Matter.js Hill Climb Car Game — Free Physics Driving Snippet

Matter.js Hill Climb Car Game · Games · Plain HTML, CSS & JS · Live preview

CategoryGames

What's included

Features

Sprung suspension
Two constraints per wheel.
Wheel-driven motion
Eased angular velocity plus friction.
Air tilt control
Query.collides detects airborne.
Procedural hills
Summed sines that steepen with distance.
Segment terrain
No poly-decomp needed.
Follow camera
Eased render.bounds.
Fuel and fail states
Sensor cans, head hits, flips.
Touch pedals
Pointer-captured on-screen buttons.

About this UI Snippet

Building a Hill Climb Car in Matter.js — Suspension, Terrain and a Follow Camera

Screenshot of the Matter.js Hill Climb Car Game snippet rendered live

Hill Climb Racing's charm is pure physics: a floppy little car, bumpy hills and a driver whose head must never touch the ground. This snippet rebuilds that loop with Matter.js and explains each part.

The car: three bodies and some springs

The chassis is a chamfered rectangle; each wheel is a circle joined to it by two constraints — a soft, short spring from above (the suspension) and a diagonal one that stops the wheel sliding forward or back. A small head body sits on a stiff neck. All parts share one negative collision group from Body.nextGroup(true), so wheels never collide with their own chassis.

Driving by spinning wheels

The engine never pushes the car. Each frame, while a pedal is held, the wheels' angular velocity is eased toward a maximum with Body.setAngularVelocity. High wheel friction against the ground turns that spin into motion — so you get wheelspin on steep slopes, wheelies from hard acceleration and natural braking.

Air control

When neither wheel touches the ground (Query.collides against the terrain), the pedals nudge the chassis' angular velocity instead, letting you lean forward or back mid-jump to land on your wheels.

Terrain without poly-decomp

Hills come from a few summed sine waves whose amplitude grows with distance. Rather than one huge concave body (which would need poly-decomp), the ground is 520 thin static rectangles, each rotated to the slope between two sample points and offset so its top edge sits exactly on the curve. The visible ground is drawn separately as a filled path with a grass stroke.

A follow camera with render.bounds

With hasBounds: true, Matter's renderer only draws what's inside render.bounds. Every frame the bounds ease toward the car, giving a smooth camera; custom drawing uses the same offset via setTransform. The sky and parallax mountains are painted last with destination-over so they land behind everything.

Rules

Fuel drains while a pedal is held and yellow sensor cans refill it; the head touching the ground, being upside-down for 1.5 seconds, or stopping on an empty tank ends the run.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Paste this snippet into an AI assistant like Claude and ask it to explain how the suspension constraints and wheel spin produce driving. Ask it to add coins and upgrades for engine and fuel, a boost button, sound for the engine revving, different vehicles, or endless terrain generated in chunks as you drive. It can also help tune the car's handling for steeper maps.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a Hill Climb Racing-style game with Matter.js 0.20 (from a CDN) in plain HTML, CSS and JavaScript.

Requirements:
- Terrain: a height function of summed sine waves that steepens with distance; build it from thin static rectangles rotated to each segment's slope, and draw it as a filled brown path with a green grass edge.
- Car: a rounded chassis, two circle wheels (high friction) each joined by a soft suspension spring and a diagonal locating constraint, and a head body on a stiff neck, all in one negative collision group.
- Drive by easing the wheels' angular velocity toward a max; when both wheels are airborne (Query.collides), pedals tilt the chassis instead.
- A smooth follow camera using hasBounds and render.bounds, with custom drawing offset to match and a sky/parallax background painted with destination-over.
- Fuel that drains while driving and refills from sensor fuel cans; game over when the head touches the ground, the car is upside down 1.5s, or it stops with no fuel.
- HUD with distance in metres, best distance and a fuel bar; distance markers every 50 m; keyboard arrows/A/D and on-screen touch pedals.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

Requires
<div class="hc">
  <div class="hc-hud">
    <div><span>Distance</span><b id="hcDist">0 m</b></div>
    <div><span>Best</span><b id="hcBest">0 m</b></div>
    <div><span>Fuel</span><i class="hc-fuel"><em id="hcFuel"></em></i></div>
    <button type="button" id="hcReset">Restart</button>
  </div>
  <div class="hc-stage" id="hcStage">
    <div class="hc-over" id="hcOver" hidden>
      <strong id="hcOverTitle">Flipped!</strong>
      <span id="hcOverText"></span>
      <button type="button" id="hcAgain">Drive again</button>
    </div>
    <div class="hc-pedals">
      <button type="button" class="hc-pedal" id="hcBrake" aria-label="Brake / reverse">◀ Brake</button>
      <button type="button" class="hc-pedal gas" id="hcGas" aria-label="Accelerate">Gas ▶</button>
    </div>
  </div>
  <p class="hc-hint">Hold <kbd>→</kbd> / <kbd>D</kbd> for gas, <kbd>←</kbd> / <kbd>A</kbd> to brake. Lean on hills — don't flip! Collect fuel cans.</p>
</div>

Step by step

How to Use

  1. 1
    Press gasHold → or D, or the green on-screen pedal.
  2. 2
    Brake and reverseHold ← or A to slow down or roll back.
  3. 3
    Lean in the airPedals tilt the car when both wheels are off the ground.
  4. 4
    Grab fuelDrive through yellow cans to refill the tank.
  5. 5
    Beat your bestDistance markers every 50 m; hills get steeper.

Real-world uses

Common Use Cases

Game jam starters
A complete loop to reskin.
Learning vehicle physics
Suspension, traction and torque.
Camera techniques
Scrolling worlds with Matter's renderer.
Procedural terrain
Endless hills from simple functions.
Mobile web games
Touch-first controls included.
Related: Matter.js Slingshot
Related: Matter.js Stacking Tower

Got questions?

Frequently Asked Questions

Make a chassis body and two circle wheels, connect each wheel to the chassis with Constraints (a soft spring for suspension plus one to hold it in place) and put all parts in one negative collision group. Drive by setting the wheels' angular velocity.

Create the renderer with hasBounds: true and update render.bounds.min and max every frame (for example in beforeRender) to centre on the body. Easing toward the target makes it smooth. Render.lookAt is another option.

Sample a height function and create a thin static rectangle between each pair of points, rotated to the slope. Draw the visible ground yourself as a filled path.

Use Matter.Query.collides(wheel, terrainBodies); if neither wheel returns a collision, the car is airborne.

Setting full wheel speed instantly creates a huge torque. Ease the angular velocity toward the target, lower the maximum, or make the chassis heavier relative to the wheels.