AABB sprite-vs-sprite and tile-based wall collision in a warehouse. Walk Kit with the D-pad between the crates. A drone he walks under turns its lights green.
| Button | Action |
|---|---|
| D-Pad | Walk Kit (2 pixels/frame) |
Then open collision_demo.sfc in luna (luna-gui collision_demo.sfc) or any emulator.
collideRect() takes two Rect* pointers and returns non-zero if the rectangles overlap. The Rect struct holds x, y, width, and height fields:
The example checks Kit against four drones each frame, storing results in a bitmask (collision_flags). Bit N set means drone N is overlapped. Two boxes that only touch edge to edge do not overlap.
collideTileEx() takes a pixel position, the collision map, its width in cells and the cell size in pixels (8, 16 or 32; collideTile() is the 8-pixel case):
It divides the position by the cell size and returns the cell's value, non-zero for solid. The map here is 16x14 cells of 16 pixels, the whole screen, so screen coordinates are map coordinates. The example checks all four corners of Kit's 16x16 box.
When a diagonal movement would collide, each axis is tested independently. If only the X component collides, the Y movement still applies (and vice versa). This lets Kit slide along walls:
0 is the floor, 1 a crate, 2 a wall plate: both solid, drawn differently. At boot, build_tilemap() turns each cell into the 2x2 tiles of its picture in res/cells.png (and a floor cell into a drain grate now and then, chosen from its position), one row of 32 map entries at a time, one DMA a row, in force blank. Move a crate in the array and it moves on screen and in the collisions at once.
Kit is a 32x32 sprite, but what collides is a 16x16 box at his feet: the sprite is drawn 8 pixels left of the box and 16 above it, so his head may pass in front of a crate he stands below, as in a game seen from above. The drones' boxes are their bodies, 8 pixels into their frames each way.
Each drone is drawn on OBJ palette 1 (pink lights) or, while its bit is set, palette 2: the same colours with the lights green. Same tiles, another palette, nothing to upload. The drones take OAM slots 0-3 and Kit slot 4: the lower slot is drawn in front, and the drones fly, so a drone Kit walks under stays visible.
From vram.spec (vram_map.h is generated from it):
| Region | Content |
|---|---|
| $0000 | OBJ tiles: Kit's 15 frames and the drone's 4, 32x32 (320 tiles) |
| $1400 | BG1 tilemap, 32x32 |
| $2000 | BG1 tiles: the four 16x16 cells (16 tiles) |
res/cells.png (the floor, a crate, a wall plate, the floor with a drain, converted without reduction so that cell k is tiles 2k, 2k + 1, 8 + 2k, 9 + 2k) and res/cast.png (Kit's walk from above, then the drone's hover) are made by devtools/art_assets/build.py (warehouse_cells, collision_sprites); Kit and the drone are Néo-Lumen art (PixelLab, CC0; assets/neo-lumen/README.md). The build converts both from the settings file beside each (opensnes-tileset, opensnes-sprite).
console, input, sprite, dma, collision, background (LIB_MODULES in the Makefile).
| File | Purpose |
|---|---|
| main.c | Collision logic, the tilemap built from the map, sprite placement, game loop |
| vram.spec | The VRAM regions; vram_map.h is generated from it |
| Makefile | Build config (LIB_MODULES := console input sprite dma collision background) |
Game: Breakout - Complete game with collision, sprites, and levels