Euler, Semi-Implicit Euler and Verlet
An integrator turns accelerations into velocities and velocities into positions, one step at a time. The exact answer would need calculus; a game takes small steps of length dt and accepts a little error per step. Which small step you take decides whether a planet keeps its orbit, a spring settles or explodes, and a jump reaches the same height on every run.
Three integrators
| Name | Update | Notes |
|---|---|---|
| Explicit (forward) Euler | x += v*dt; v += a*dt |
uses the old velocity to move; gains energy |
| Semi-implicit (symplectic) Euler | v += a*dt; x += v*dt |
uses the new velocity; what Box2D, PhysX, Jolt and most game engines use |
| Verlet (position Verlet) | x_next = 2x - x_prev + a*dt² |
no velocity stored; velocity is implied by x - x_prev; loved for cloth and ropes |
The only difference between the two Euler versions is the order of two lines, and yet it is the difference between a stable game and one that slowly explodes.
Constant acceleration: a thrown ball
With constant gravity the exact answer is known, y = v₀t + ½gt², so we can measure each integrator's error directly:
#include <iostream>
#include <iomanip>
double z0(double v) { return v > -5e-5 && v < 5e-5 ? 0.0 : v; } // hide -0.0000
int main() {
std::cout << std::fixed << std::setprecision(4);
const double g = -9.81, v0 = 10, dt = 1.0 / 30.0;
const int steps = 30; // one second
double xe = 0, ve = v0; // explicit Euler
double xs = 0, vs = v0; // semi-implicit Euler
double xv = 0, xvPrev = -v0 * dt + 0.5 * g * dt * dt; // Verlet needs the position one step earlier
for (int i = 0; i < steps; i++) {
xe += ve * dt; ve += g * dt;
vs += g * dt; xs += vs * dt;
double next = 2 * xv - xvPrev + g * dt * dt;
xvPrev = xv; xv = next;
}
double t = steps * dt;
double exact = v0 * t + 0.5 * g * t * t;
std::cout << "exact y = " << exact << "\n";
std::cout << "explicit Euler y = " << xe << " error " << xe - exact << "\n";
std::cout << "semi-implicit y = " << xs << " error " << xs - exact << "\n";
std::cout << "Verlet y = " << xv << " error " << z0(xv - exact) << "\n";
}exact y = 5.0950
explicit Euler y = 5.2585 error 0.1635
semi-implicit y = 4.9315 error -0.1635
Verlet y = 5.0950 error 0.0000
Both Euler variants are off by ½·g·dt·t in opposite directions, so the error shrinks linearly with dt. Verlet is exact (to rounding) for constant acceleration, because its update is the same parabola the exact formula describes.
Energy drift: a spring
Constant gravity is forgiving. A spring is not: the force depends on the position, so errors feed back into the next step. Take a 1 kg mass on a spring with stiffness k = 1 released from x = 1. Its total energy ½v² + ½kx² should stay 0.5 forever.
#include <iostream>
#include <iomanip>
double energy(double x, double v) { return 0.5 * v * v + 0.5 * x * x; } // m = k = 1
int main() {
std::cout << std::fixed << std::setprecision(3);
const double dt = 0.1;
double xe = 1, ve = 0; // explicit Euler
double xs = 1, vs = 0; // semi-implicit Euler
double xv = 1, xvPrev = 1 + 0.5 * -1 * dt * dt; // Verlet, started at rest (a = -x)
std::cout << " time explicit semi-impl Verlet\n";
for (int step = 0; step <= 1000; step++) {
if (step % 100 == 0) {
double vv = (xv - xvPrev) / dt; // Verlet's implied velocity (backward difference)
std::cout << std::setw(7) << step * dt << std::setw(11) << energy(xe, ve)
<< std::setw(11) << energy(xs, vs) << std::setw(8) << energy(xv, vv) << "\n";
}
double ae = -xe; xe += ve * dt; ve += ae * dt;
vs += -xs * dt; xs += vs * dt;
double next = 2 * xv - xvPrev - xv * dt * dt;
xvPrev = xv; xv = next;
}
} time explicit semi-impl Verlet
0.000 0.500 0.500 0.501
10.000 1.352 0.478 0.478
20.000 3.658 0.484 0.481
30.000 9.894 0.511 0.507
40.000 26.762 0.526 0.525
50.000 72.386 0.512 0.513
60.000 195.792 0.485 0.485
70.000 529.580 0.477 0.476
80.000 1432.416 0.499 0.495
90.000 3874.417 0.523 0.520
100.000 10479.578 0.521 0.521
Read the table column by column:
- Explicit Euler gains energy every step. Here it multiplies the energy by
1 + dt²per step, which is tiny per step and enormous after 1000 steps. In a game this is the spring, joint or orbit that slowly winds up until it explodes. - Semi-implicit Euler wobbles a little around the true value but never drifts: it is symplectic, which roughly means it preserves the shape of the motion even though each step has an error.
- Verlet behaves the same way. The printed energy wobbles because the velocity is estimated from two positions, not because the motion is drifting.
Note: "Bounded error" is not "no error". Semi-implicit Euler still gets the phase slightly wrong (the oscillation runs a bit fast), which is invisible in a game but would matter in a scientific simulation. For games, stability is what counts.
Velocity Verlet and why engines don't use plain Verlet
Position Verlet stores no velocity, so anything that wants to set a velocity (a jump impulse, a collision response, friction) has to fake it by moving x_prev. That's awkward for rigid-body solvers, which work with velocities. Two practical consequences:
- Rigid-body engines (PhysX, Box2D, Jolt, Rapier) use semi-implicit Euler with velocity-level constraint solving.
- Position-based systems (cloth, ropes, soft bodies, hair) love Verlet, because a constraint can simply move positions and the velocity follows automatically. You will use it in Distance Constraints and Ropes and Chains.
Velocity Verlet keeps an explicit velocity and still gets Verlet's accuracy, at the price of evaluating the acceleration twice per step:
// velocity Verlet (a(x) = acceleration at position x)
x += v * dt + 0.5 * a(x) * dt * dt;
double aNew = a(x);
v += 0.5 * (aOld + aNew) * dt;
aOld = aNew;See it: three planets, three integrators
Each planet starts on the same circular orbit around the sun. Explicit Euler spirals outward (gaining energy), semi-implicit Euler and Verlet keep orbiting. Drag the slider to change the time step and press Reset: larger steps make Euler explode faster, while the other two get wobblier but stay bound.
<style>
html, body { margin: 0; height: 100%; background: #0e1018; overflow: hidden; color: #fff; font: 13px system-ui, sans-serif; }
canvas { display: block; margin: 0 auto; width: min(100vw, 177.78vh); height: auto; }
.ui { position: absolute; left: 8px; bottom: 8px; display: flex; gap: 8px; align-items: center; }
button { font: inherit; }
</style>
<canvas width="480" height="270"></canvas>
<div class="ui">dt <input id="dt" type="range" min="0.005" max="0.08" step="0.005" value="0.03"><span id="dtv"></span><button id="reset">Reset</button></div>
<script>
const cv = document.querySelector('canvas'), ctx = cv.getContext('2d');
const dtInput = document.getElementById('dt'), dtLabel = document.getElementById('dtv');
const GM = 1, R = 1, SCALE = 55;
const planets = [
{ name: 'explicit Euler', color: '#ef5350', cx: 85 },
{ name: 'semi-implicit Euler', color: '#66bb6a', cx: 240 },
{ name: 'Verlet', color: '#42a5f5', cx: 395 },
];
function accel(x, y) { const r2 = x * x + y * y, r = Math.sqrt(r2); return [-GM * x / (r2 * r), -GM * y / (r2 * r)]; }
function reset() {
const dt = +dtInput.value;
for (const p of planets) {
p.x = R; p.y = 0; p.vx = 0; p.vy = Math.sqrt(GM / R); // circular orbit speed
p.px = p.x - p.vx * dt; p.py = p.y - p.vy * dt; // Verlet's previous position
p.trail = [];
}
}
function energy(p, dt) {
let vx = p.vx, vy = p.vy;
if (p.name === 'Verlet') { vx = (p.x - p.px) / dt; vy = (p.y - p.py) / dt; }
return 0.5 * (vx * vx + vy * vy) - GM / Math.hypot(p.x, p.y);
}
function step(dt) {
for (const p of planets) {
if (Math.hypot(p.x, p.y) > 20) continue; // escaped: stop simulating
const [ax, ay] = accel(p.x, p.y);
if (p.name === 'explicit Euler') { p.x += p.vx * dt; p.y += p.vy * dt; p.vx += ax * dt; p.vy += ay * dt; }
else if (p.name === 'semi-implicit Euler') { p.vx += ax * dt; p.vy += ay * dt; p.x += p.vx * dt; p.y += p.vy * dt; }
else { const nx = 2 * p.x - p.px + ax * dt * dt, ny = 2 * p.y - p.py + ay * dt * dt; p.px = p.x; p.py = p.y; p.x = nx; p.y = ny; }
p.trail.push([p.x, p.y]);
if (p.trail.length > 400) p.trail.shift();
}
}
function draw(dt) {
ctx.fillStyle = '#0e1018'; ctx.fillRect(0, 0, 480, 270);
for (const p of planets) {
const cy = 120;
ctx.fillStyle = '#ffca28'; ctx.beginPath(); ctx.arc(p.cx, cy, 6, 0, Math.PI * 2); ctx.fill();
ctx.strokeStyle = 'rgba(255,255,255,0.15)'; ctx.beginPath(); ctx.arc(p.cx, cy, R * SCALE, 0, Math.PI * 2); ctx.stroke();
ctx.strokeStyle = p.color; ctx.beginPath();
p.trail.forEach(([x, y], i) => { const sx = p.cx + x * SCALE, sy = cy - y * SCALE; i ? ctx.lineTo(sx, sy) : ctx.moveTo(sx, sy); });
ctx.stroke();
ctx.fillStyle = p.color; ctx.beginPath(); ctx.arc(p.cx + p.x * SCALE, cy - p.y * SCALE, 4, 0, Math.PI * 2); ctx.fill();
ctx.textAlign = 'center'; ctx.fillText(p.name, p.cx, 215);
ctx.fillStyle = '#ccc'; ctx.fillText(`energy ${energy(p, dt).toFixed(3)}`, p.cx, 232);
}
ctx.fillStyle = '#999'; ctx.textAlign = 'right'; ctx.fillText('true energy -0.500', 470, 262);
}
dtInput.addEventListener('input', () => { dtLabel.textContent = (+dtInput.value).toFixed(3); reset(); });
document.getElementById('reset').addEventListener('click', reset);
dtLabel.textContent = (+dtInput.value).toFixed(3);
reset();
requestAnimationFrame(function frame() {
const dt = +dtInput.value;
for (let i = 0; i < 2; i++) step(dt);
draw(dt);
requestAnimationFrame(frame);
});
</script>Which one should I use?
| Situation | Integrator |
|---|---|
| Rigid bodies, characters, projectiles | semi-implicit Euler (it is what your engine already does) |
| Cloth, ropes, hair, soft bodies, particles with constraints | position Verlet or position-based dynamics |
| Orbits, precise trajectories, tools that predict a path | velocity Verlet, or a higher-order method such as RK4 |
| Never | explicit Euler for anything that oscillates |
Whatever you choose, keep dt fixed. The next lesson, Fixed Timestep and Interpolation, shows why and how.
Test yourself
What is the only code difference between explicit and semi-implicit Euler?
- Semi-implicit Euler uses a smaller dt
- Semi-implicit Euler updates the velocity first and moves with the new velocity
- Semi-implicit Euler stores the previous position
- Semi-implicit Euler evaluates the force twice
A spring simulated with explicit Euler slowly swings wider and wider. Why?
- Floating-point rounding
- Explicit Euler adds a little energy every step, and it compounds
- The spring constant is negative
- The time step is fixed
Why do cloth and rope simulations like position Verlet?
- It needs no forces
- Constraints can move positions directly and the velocity follows automatically from x - x_prev
- It is the most accurate integrator
- It runs on the GPU