Tutorial 1: Collision Response and Forces

Unit 6 · Physics and gameplay systems

Objectives

Collision detection answers “are these shapes intersecting?” Response answers “what changes now?” A simple axis-aligned response can move an object back to the surface, remove velocity into the surface, and preserve tangential velocity.

velocityY += gravity * deltaSeconds;
positionY += velocityY * deltaSeconds;
if (positionY > floorY) {
    positionY = floorY;
    velocityY = 0;
    grounded = true;
}

Use one consistent coordinate convention. Decide whether positive y points down or up, and document it. A small simulation step is easier to reason about than a renderer that silently changes positions.

Exercises

  1. Implement floor collision and grounded detection.
  2. Add horizontal friction that approaches zero without reversing direction.
  3. List edge cases for a player starting inside a wall.

Self-check

  1. What is the difference between detection and response?
  2. Why set position back to the surface?
  3. What does damping represent?

Self-Check Quiz

1. What should happen to downward velocity after landing on a floor?

AnswerRemove the downward component, commonly by setting vertical velocity to zero, and mark the object grounded.

2. Why document the positive y direction?

AnswerGravity, jump, and collision signs depend on the coordinate convention; documenting it prevents inverted or inconsistent rules.

Homework

  1. Build a small physics sandbox with gravity, floor collision, and jumping.
  2. Record three input traces and expected positions at each update.
  3. Explain how your system handles a large delta time.
Sample answerUse velocity += acceleration * delta, then position += velocity * delta. If the player crosses the floor, clamp position to the floor, clear downward velocity, and set grounded. Cap delta time or use fixed substeps after a pause.
👤