Game Dev Advanced

22  Collision

Axis-aligned boxes are the workhorse of game collision. Detect when two overlap, then keep a point inside an arena with clamp.

Step 2 / 2

Implement clamp_into: pull the point back inside the arena on both axes.

Hints
  • f32 has a built-in: x.clamp(lo, hi).
  • The arena's far corner is arena.x + arena.w / arena.y + arena.h.
22_collision.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
Reveal solution
#[derive(Clone, Copy)]
struct Aabb { x: f32, y: f32, w: f32, h: f32 }

fn clamp_into(x: f32, y: f32, arena: Aabb) -> (f32, f32) {
    (
        x.clamp(arena.x, arena.x + arena.w),
        y.clamp(arena.y, arena.y + arena.h),
    )
}