AABB and tile-based collision detection with visual feedback. More...
#include <snes.h>#include <snes/collision.h>#include "vram_map.h"#include "res/cells.inc"#include "res/cast.inc"Macros | |
| #define | BOX_SIZE 16 |
| #define | CELL 16 |
| #define | MAP_HEIGHT 14 |
| #define | MAP_WIDTH 16 |
| #define | NUM_ENEMIES 4 |
| #define | PLAYER_SPEED 2 |
Sprite frames in res/cast.png (32x32, four a row) | |
| #define | FRAME_DOWN 0 |
| #define | FRAME_DRONE 15 |
| #define | FRAME_RIGHT 10 |
| #define | FRAME_UP 5 |
OBJ palettes | |
| #define | PAL_DRONE 1 |
| #define | PAL_DRONE_SEEN 2 |
| #define | PAL_KIT 0 |
Cell values: 0 passable, anything else solid | |
| #define | CRATE 1 |
| #define | FLOOR 0 |
| #define | WALL 2 |
Functions | |
| static void | build_tilemap (void) |
| Build BG1's tilemap from the collision map, in force blank. | |
| static void | check_collisions (void) |
| Test Kit-vs-drone AABB overlap and update collision_flags. | |
| static u8 | check_wall_collision (s16 new_x, s16 new_y) |
| Test whether Kit's box at a position would overlap a solid cell. | |
| static u16 | frame_tile (u16 f) |
The OAM tile of frame f of res/cast.png. | |
| int | main (void) |
| Entry point – build the warehouse, run the loop with collision tests. | |
| static void | update_sprites (u8 moving) |
| Place the five sprites from their hitboxes. | |
Variables | |
| static u8 | collision_flags |
| Bitmask tracking which drones Kit is currently overlapping. | |
| static const u8 | collision_map [16 *14] |
| Tile-based collision map (16x14 = 224 bytes, 256x224 pixels). | |
| static Rect | enemy_box [4] |
| Bounding boxes for each drone, rebuilt every frame for collideRect(). | |
| static s16 | enemy_x [4] |
| Drones' hitboxes, top-left corner X. | |
| static s16 | enemy_y [4] |
| Drones' hitboxes, top-left corner Y. | |
| static u8 | facing |
| The way Kit faces: FRAME_DOWN, FRAME_UP or FRAME_RIGHT (with facing_left). | |
| static u8 | facing_left |
| Kit faces left: his right frames, mirrored. | |
| static Rect | player_box |
| Kit's bounding box for AABB collision tests via collideRect(). | |
| static s16 | player_x |
| Kit's hitbox, top-left corner, in screen coordinates. | |
| static s16 | player_y |
| Kit's hitbox, top-left corner, Y. | |
| static u16 | tick |
| Frames since boot, for the walk and the hover. | |
AABB and tile-based collision detection with visual feedback.
Demonstrates the OpenSNES collision module with two complementary techniques: axis-aligned bounding box (AABB) tests between sprites, and tile-based collision against a map. Kit walks through a warehouse of the Rue basse, between wall plates and stacks of crates, while four security drones hover at fixed places. Collisions are detected every frame: a drone Kit touches turns its lights green. Wall collisions use corner-point checks against a 16x14 collision map of 16x16 cells, with per-axis rejection so that Kit slides along walls.
The collision map is also the picture: the example builds BG1's tilemap from it at boot (a wall plate, a crate or the floor for each cell), so what Kit bumps into is exactly what is drawn.
A hitbox is not a sprite. Kit is a 32x32 sprite; what collides is a 16x16 box at his feet, and the sprite is drawn around it. The drones' boxes are their bodies, smaller than their frames too.
| #define BOX_SIZE 16 |
Kit's and the drones' hitboxes, 16x16 pixels
| #define CELL 16 |
A cell's size in pixels, as collideTileEx() is told
| #define CRATE 1 |
| #define FLOOR 0 |
| #define FRAME_DOWN 0 |
Kit walking down: frames 0-4
| #define FRAME_DRONE 15 |
the drone's hover: 15-18
| #define FRAME_RIGHT 10 |
walking right: 10-14; left is the same, mirrored
| #define FRAME_UP 5 |
walking up: 5-9
| #define MAP_HEIGHT 14 |
Collision map height in 16x16 cells
| #define MAP_WIDTH 16 |
Collision map width in 16x16 cells
| #define NUM_ENEMIES 4 |
Number of drones in the warehouse
| #define PAL_DRONE 1 |
pink lights
| #define PAL_DRONE_SEEN 2 |
the same drone, its lights green: Kit is touching it
| #define PAL_KIT 0 |
| #define PLAYER_SPEED 2 |
Kit's speed in pixels per frame
| #define WALL 2 |
|
static |
Build BG1's tilemap from the collision map, in force blank.
Each cell is 2x2 tiles of res/cells.png: cell k of the picture (0 floor, 1 crate, 2 wall, 3 floor with a drain) is tiles 2k, 2k + 1 above 8 + 2k, 9 + 2k. A floor cell gets the drain now and then, chosen from its position so the floor is not one repeated slab. One row of 32 map entries is built in RAM and sent with one DMA, 28 times.
< Collision map height in 16x16 cells
< Collision map width in 16x16 cells
< Collision map width in 16x16 cells
|
static |
Test Kit-vs-drone AABB overlap and update collision_flags.
Rebuilds Kit's and every drone's box from their current positions, then calls collideRect() for each Kit-drone pair. The result is stored in collision_flags as a bitmask (bit N = drone N is overlapped).
< Kit's and the drones' hitboxes, 16x16 pixels
< Kit's and the drones' hitboxes, 16x16 pixels
< Number of drones in the warehouse
< Kit's and the drones' hitboxes, 16x16 pixels
< Kit's and the drones' hitboxes, 16x16 pixels
Test whether Kit's box at a position would overlap a solid cell.
Probes the four corners of the 16x16 box against collision_map[] via collideTileEx() with 16-pixel cells. If any corner lands on a solid cell, the function returns 1 to reject the movement.
The caller uses per-axis rejection: if the combined (new_x, new_y) collides, it tries each axis independently so that Kit slides along walls instead of stopping dead.
| new_x | Proposed box X in screen coordinates (the map covers the screen) |
| new_y | Proposed box Y |
< Collision map width in 16x16 cells
< A cell's size in pixels, as collideTileEx() is told
< Kit's and the drones' hitboxes, 16x16 pixels
< Collision map width in 16x16 cells
< A cell's size in pixels, as collideTileEx() is told
< Kit's and the drones' hitboxes, 16x16 pixels
< Collision map width in 16x16 cells
< A cell's size in pixels, as collideTileEx() is told
< Kit's and the drones' hitboxes, 16x16 pixels
< Kit's and the drones' hitboxes, 16x16 pixels
< Collision map width in 16x16 cells
< A cell's size in pixels, as collideTileEx() is told
The OAM tile of frame f of res/cast.png.
A 32x32 frame is 4x4 tiles and a row of OBJ tiles is 16 wide, so frame f (four a row of the sheet) starts at column (f % 4) * 4 of tile row (f / 4) * 4. Frames 16 and up are past tile 255: the second OBJ name table, which oamSet() reaches through the tile's ninth bit.
| int main | ( | void | ) |
Entry point – build the warehouse, run the loop with collision tests.
Sets up Mode 1 with BG1 for the warehouse and sprites for Kit and the drones. The main loop reads the D-pad every frame, proposes a new position, validates it against the wall collision map (with per-axis sliding), checks AABB overlaps with the drones, and places the sprites. The NMI handler sends the OAM buffer during VBlank.
< Current joypad button state (bitmask)
< Proposed box position before the wall check
< A cell's size in pixels, as collideTileEx() is told
< A cell's size in pixels, as collideTileEx() is told
< Kit walking down: frames 0-4
< A cell's size in pixels, as collideTileEx() is told
< A cell's size in pixels, as collideTileEx() is told
< A cell's size in pixels, as collideTileEx() is told
< A cell's size in pixels, as collideTileEx() is told
< A cell's size in pixels, as collideTileEx() is told
< A cell's size in pixels, as collideTileEx() is told
< A cell's size in pixels, as collideTileEx() is told
< A cell's size in pixels, as collideTileEx() is told
< Number of drones in the warehouse
< Kit's speed in pixels per frame
< walking right: 10-14; left is the same, mirrored
< Kit's speed in pixels per frame
< walking right: 10-14; left is the same, mirrored
< Kit's speed in pixels per frame
< walking up: 5-9
< Kit's speed in pixels per frame
< Kit walking down: frames 0-4
|
static |
Place the five sprites from their hitboxes.
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 — its green lights are the feedback. Kit's 32x32 frame is drawn so that his 16x16 box sits at its bottom centre (8 pixels in, 16 down): his feet. The drones' boxes sit 8 pixels into their frames each way, round their bodies. Kit walks while he moves (five frames, six vblanks each) and stands on the first frame of his direction otherwise; the drones hover through four frames together.
< Number of drones in the warehouse
< the drone's hover: 15-18
< Number of drones in the warehouse
< the same drone, its lights green: Kit is touching it
< pink lights
|
static |
Bitmask tracking which drones Kit is currently overlapping.
Bit N is set when Kit's box overlaps drone N's. It picks each drone's palette (PAL_DRONE or PAL_DRONE_SEEN).
|
static |
Tile-based collision map (16x14 = 224 bytes, 256x224 pixels).
Row-major, one byte a cell: FLOOR is passable, CRATE and WALL are solid (collideTileEx() returns the value; the example only asks "non-zero"). The two solid kinds differ only in how they are drawn.
|
static |
Bounding boxes for each drone, rebuilt every frame for collideRect().
|
static |
Drones' hitboxes, top-left corner X.
|
static |
Drones' hitboxes, top-left corner Y.
|
static |
The way Kit faces: FRAME_DOWN, FRAME_UP or FRAME_RIGHT (with facing_left).
|
static |
Kit faces left: his right frames, mirrored.
|
static |
Kit's bounding box for AABB collision tests via collideRect().
|
static |
Kit's hitbox, top-left corner, in screen coordinates.
|
static |
Kit's hitbox, top-left corner, Y.
|
static |
Frames since boot, for the walk and the hover.