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 overlaps: true when the two boxes intersect. Touching edges don't count.
Hints
- Two boxes overlap when they overlap on BOTH axes.
- Per axis:
a.x < b.x + b.w && b.x < a.x + a.w-- strict<makes a touching edge 'no overlap'.
Reveal solution
#[derive(Clone, Copy)]
struct Aabb { x: f32, y: f32, w: f32, h: f32 }
fn overlaps(a: Aabb, b: Aabb) -> bool {
a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h
}