AABB

Line the boxes up horizontally while keeping one above the other. Which overlap test still fails?
Interactive demo#Collision
A rectangle that never rotates. The simplest and fastest collision shape.
Two such boxes overlap only if their shadows overlap on both the horizontal and the vertical axis. The bars along the edges show each test; the boxes collide when both pairs turn magenta.
Code
namespace aabb_lesson;
import stage;
import std;
import math;
export "aabb" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
artwork("draft");
let bx = floor(px - 14 + 0.5);
let by = floor(py - 11 + 0.5);
let overlap_x = 52 < bx + 28 && bx < 92;
let overlap_y = 32 < by + 22 && by < 64;
draw_prop("crate", 52, 32, 40, 32);
draw_prop("crate", bx, by, 28, 22);
opacity(0.3);
rect(52, 32, 40, 32, SKY);
rect(bx, by, 28, 22, SUN);
opacity(1);
if overlap_x && overlap_y {
let x = max(52.0, bx);
let y = max(32.0, by);
rect(x, y, min(92.0, bx + 28) - x, min(64.0, by + 22) - y, MAG);
}
box(52, 32, 40, 32, SKY);
box(bx, by, 28, 22, SUN);
rect(52, 93, 40, 2, if overlap_x { MAG } else { SKY });
rect(bx, 96, 28, 2, if overlap_x { MAG } else { SUN });
rect(3, 32, 2, 32, if overlap_y { MAG } else { SKY });
rect(6, by, 2, 22, if overlap_y { MAG } else { SUN });
}

Comments
No comments yet.