OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
main.c File Reference

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.

Detailed Description

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.

SNES Concepts
  • AABB collision via collideRect() for sprite-vs-sprite overlap testing
  • Tile collision via collideTileEx() (16x16 cells) for sprite-vs-map wall rejection
  • A tilemap built from game data in force blank: one DMA a row
  • A hitbox smaller than its sprite, and the sprite drawn from the box
  • A palette per state: the same tiles, OBJ palette 1 or 2
What to Observe
  • Walk Kit with the D-pad: he faces the way he walks (left is his right frames mirrored) and takes steps while he moves
  • He cannot pass through the crates or the walls; he slides along them
  • When his box overlaps a drone's, the drone's lights turn green
  • Two drones can be touched at once; each is tracked on its own
Modules Used
console, input, sprite, dma, collision, background
See also
collision.h, sprite.h, dma.h, input.h

Macro Definition Documentation

◆ BOX_SIZE

#define BOX_SIZE   16

Kit's and the drones' hitboxes, 16x16 pixels

◆ CELL

#define CELL   16

A cell's size in pixels, as collideTileEx() is told

◆ CRATE

#define CRATE   1

◆ FLOOR

#define FLOOR   0

◆ FRAME_DOWN

#define FRAME_DOWN   0

Kit walking down: frames 0-4

◆ FRAME_DRONE

#define FRAME_DRONE   15

the drone's hover: 15-18

◆ FRAME_RIGHT

#define FRAME_RIGHT   10

walking right: 10-14; left is the same, mirrored

◆ FRAME_UP

#define FRAME_UP   5

walking up: 5-9

◆ MAP_HEIGHT

#define MAP_HEIGHT   14

Collision map height in 16x16 cells

◆ MAP_WIDTH

#define MAP_WIDTH   16

Collision map width in 16x16 cells

◆ NUM_ENEMIES

#define NUM_ENEMIES   4

Number of drones in the warehouse

◆ PAL_DRONE

#define PAL_DRONE   1

pink lights

◆ PAL_DRONE_SEEN

#define PAL_DRONE_SEEN   2

the same drone, its lights green: Kit is touching it

◆ PAL_KIT

#define PAL_KIT   0

◆ PLAYER_SPEED

#define PLAYER_SPEED   2

Kit's speed in pixels per frame

◆ WALL

#define WALL   2

Function Documentation

◆ build_tilemap()

void build_tilemap ( void )
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

◆ check_collisions()

void check_collisions ( void )
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

◆ check_wall_collision()

u8 check_wall_collision ( s16 new_x,
s16 new_y )
static

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.

Parameters
new_xProposed box X in screen coordinates (the map covers the screen)
new_yProposed box Y
Returns
1 if the position overlaps a solid cell, 0 if clear

< 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

◆ frame_tile()

u16 frame_tile ( u16 f)
static

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.

◆ main()

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.

Returns
Never returns (infinite loop).

< 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

◆ update_sprites()

void update_sprites ( u8 moving)
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

Variable Documentation

◆ collision_flags

u8 collision_flags
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).

◆ collision_map

const u8 collision_map[16 *14]
static
Initial value:
= {
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
2,0,0,1,1,0,0,0,0,0,0,1,1,0,0,2,
2,0,0,1,1,0,0,0,0,0,0,1,1,0,0,2,
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
2,0,0,0,0,0,1,1,1,1,0,0,0,0,0,2,
2,0,0,0,0,0,1,1,1,1,0,0,0,0,0,2,
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
2,0,0,1,1,0,0,0,0,0,0,1,1,0,0,2,
2,0,0,1,1,0,0,0,0,0,0,1,1,0,0,2,
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
}

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.

Note
const, so it lives in ROM and costs zero WRAM. collideTileEx() takes a const u8 *, and const-qualified loads compile to bank-honouring far reads — the map may sit in any bank.

◆ enemy_box

Rect enemy_box[4]
static

Bounding boxes for each drone, rebuilt every frame for collideRect().

◆ enemy_x

s16 enemy_x[4]
static

Drones' hitboxes, top-left corner X.

◆ enemy_y

s16 enemy_y[4]
static

Drones' hitboxes, top-left corner Y.

◆ facing

u8 facing
static

The way Kit faces: FRAME_DOWN, FRAME_UP or FRAME_RIGHT (with facing_left).

◆ facing_left

u8 facing_left
static

Kit faces left: his right frames, mirrored.

◆ player_box

Rect player_box
static

Kit's bounding box for AABB collision tests via collideRect().

◆ player_x

s16 player_x
static

Kit's hitbox, top-left corner, in screen coordinates.

◆ player_y

s16 player_y
static

Kit's hitbox, top-left corner, Y.

◆ tick

u16 tick
static

Frames since boot, for the walk and the hover.