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.
Implement clamp_into: pull the point back inside the arena on both axes.
Hints
f32has a built-in:x.clamp(lo, hi).- The arena's far corner is
arena.x + arena.w/arena.y + arena.h.
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),
)
}