Lesson 6 of 22, in world 2: Motion & Math
Velocity, Acceleration & Friction
Moving a character by setting its position directly feels robotic: it starts and stops instantly. Real movement — and good-feeling game movement — has momentum. The trick is a chain of three vectors: acceleration changes velocity, velocity changes position.
Position, velocity, acceleration
- Position (px) — where the object is.
- Velocity (px/s) — how fast and in which direction position changes.
- Acceleration (px/s²) — how fast velocity changes. Input, gravity, explosions and thrusters are all accelerations.
function update(dt) {
const input = normalize(readArrowKeys())
ship.acc = scale(input, ACCEL) // 1. forces → acceleration
ship.vel = add(ship.vel, scale(ship.acc, dt)) // 2. acceleration → velocity
ship.vel = scale(ship.vel, 1 - FRICTION * dt) // 3. friction slows us down
if (length(ship.vel) > MAX_SPEED) // 4. cap the top speed
ship.vel = scale(normalize(ship.vel), MAX_SPEED)
ship.pos = add(ship.pos, scale(ship.vel, dt)) // 5. velocity → position
}Fly with the arrow keys or WASD. The cyan arrow is velocity, the pink arrow is acceleration (your input). The graph at the bottom shows speed over time — see how acceleration and friction shape the curve. Try very low friction for an "ice level" feel.
This step-by-step update is called Euler integration: each frame we nudge velocity by acceleration × dt, then position by velocity × dt. It’s not perfectly accurate, but it is simple, fast and what most games use (especially combined with a fixed timestep from lesson 3).
Friction and damping
Without friction, the ship would glide forever (like in space — which is a valid design choice!). Multiplying velocity by a number slightly less than 1 every frame bleeds speed away. Low friction feels slippery like ice, high friction feels tight and responsive.
Key takeaways
- Acceleration → velocity → position, each multiplied by dt.
- Friction (damping) and max speed shape how movement feels.
- Movement numbers are design knobs: make them easy to tweak.
Check yourself
Q1. Velocity is (100, 0) px/s and acceleration is (0, 200) px/s². After 0.5 s (one big step), velocity is…
Q2. Your character feels like it is on ice. Which change helps most?
Q3. Which of these is an acceleration?
Exercises
- Set friction to 0. Describe how the ship feels. Which classic game does it remind you of?
- Find settings that feel "heavy" (like a truck) and settings that feel "twitchy" (like a fly). Write them down.
- Modify the code above so that pressing the opposite direction applies 2× acceleration ("turnaround boost").