SNES Collision Detection.
More...
Go to the source code of this file.
|
| struct | Rect |
| | Axis-aligned bounding box (rectangle). More...
|
|
| u8 | collidePoint (s16 x, s16 y, const Rect *r) |
| | Check point vs rectangle collision.
|
| u8 | collideRect (const Rect *a, const Rect *b) |
| | Check rectangle vs rectangle collision (AABB).
|
| u8 | collideRectEx (const Rect *a, const Rect *b, s16 *overlapX, s16 *overlapY) |
| | Check if two rectangles overlap and return overlap amount.
|
| u8 | collideRectTile (const Rect *r, const u8 *tilemap, u16 mapWidth) |
| | Check collision between rectangle and tilemap.
|
| u8 | collideTile (s16 px, s16 py, const u8 *tilemap, u16 mapWidth) |
| | Check collision with tile at pixel coordinates.
|
| u8 | collideTileEx (s16 px, s16 py, const u8 *tilemap, u16 mapWidth, u8 tileSize) |
| | Check collision with tile using custom tile size.
|
| u8 | rectContains (const Rect *inner, const Rect *outer) |
| | Check if rectangle is completely inside another.
|
| void | rectGetCenter (const Rect *r, s16 *cx, s16 *cy) |
| | Get rectangle center point.
|
| void | rectInit (Rect *r, s16 x, s16 y, u16 w, u16 h) |
| | Initialize a rectangle.
|
| void | rectSetPos (Rect *r, s16 x, s16 y) |
| | Set rectangle position.
|
SNES Collision Detection.
Simple collision detection routines for game objects.
Overview
This module provides:
- Rectangle vs rectangle (AABB) collision
- Point vs rectangle collision
- Tile-based collision for platformers
- Bounding box helpers
Usage Example
player_hit();
}
on_ground = 1;
}
static s16 enemy_y[4]
Y positions of the four static enemy sprites.
Definition main.c:65
static s16 player_y
Player Y position in screen coordinates.
Definition main.c:58
static s16 enemy_x[4]
X positions of the four static enemy sprites.
Definition main.c:63
static s16 player_x
Player X position in screen coordinates.
Definition main.c:56
u8 collideRect(const Rect *a, const Rect *b)
Check rectangle vs rectangle collision (AABB).
u8 collideTile(s16 px, s16 py, const u8 *tilemap, u16 mapWidth)
Check collision with tile at pixel coordinates.
static u16 tilemap[896]
Generated 32x28 tilemap (krom's fixed pattern — see buildTilemap).
Definition main.c:55
Axis-aligned bounding box (rectangle).
Definition collision.h:54
- Author
- OpenSNES Team
- Copyright
- MIT License
◆ collidePoint()
Check point vs rectangle collision.
Tests if a point is inside a rectangle.
- Parameters
-
| x | Point X coordinate |
| y | Point Y coordinate |
| r | Rectangle to test against |
- Returns
- 1 if point is inside rectangle, 0 otherwise
target_hit();
}
u8 collidePoint(s16 x, s16 y, const Rect *r)
Check point vs rectangle collision.
◆ collideRect()
Check rectangle vs rectangle collision (AABB).
Tests if two axis-aligned bounding boxes overlap.
- Parameters
-
| a | First rectangle |
| b | Second rectangle |
- Returns
- 1 if rectangles overlap, 0 otherwise
Rect enemy = { ex, ey, 16, 16 };
}
static u8 px[64]
Definition main.c:52
static u8 py
Definition main.c:74
- Note
- Read-only rectangles are const Rect * since 2026-09-20 — in this module that is correctness, not style. A static const Rect lives in an asset bank, and only a const-qualified read is a far read: through the old Rect * parameter (with the cast it forced on the caller) the fields were read from bank $00. Callers passing a plain Rect * are unaffected.
◆ collideRectEx()
| u8 collideRectEx |
( |
const Rect * | a, |
|
|
const Rect * | b, |
|
|
s16 * | overlapX, |
|
|
s16 * | overlapY ) |
Check if two rectangles overlap and return overlap amount.
Tests collision and returns the overlap distance for each axis. Useful for collision response (pushing objects apart).
- Parameters
-
| a | First rectangle |
| b | Second rectangle |
| overlapX | Receives the X displacement that moves a OUT of b (negative = move a left, positive = move a right) |
| overlapY | Receives the Y displacement (negative = up, positive = down) |
- Returns
- 1 if rectangles overlap, 0 otherwise
The values are displacements to ADD to a. Both axes are always filled; applying both moves the object diagonally, so resolve the smaller one. (This example subtracted them until 2026-09-20, which pushes the player deeper into the wall: for a = {10,10,16,16}, b = {20,20,16,16} the function returns dx = -6, and a must move LEFT.)
if ((dx < 0 ? -dx : dx) < (dy < 0 ? -dy : dy))
player_x += dx;
}
u8 collideRectEx(const Rect *a, const Rect *b, s16 *overlapX, s16 *overlapY)
Check if two rectangles overlap and return overlap amount.
signed short s16
16-bit signed integer (-32768 to 32767)
Definition types.h:50
◆ collideRectTile()
| u8 collideRectTile |
( |
const Rect * | r, |
|
|
const u8 * | tilemap, |
|
|
u16 | mapWidth ) |
Check collision between rectangle and tilemap.
Tests if any corner of the rectangle touches a solid tile. Useful for checking if a moving object would collide with the world.
- Parameters
-
| r | Rectangle to test |
| tilemap | Pointer to collision tilemap |
| mapWidth | Width of tilemap in tiles |
- Returns
- 1 if any corner touches solid tile, 0 otherwise. A corner off the map to the left, the top or the right counts as solid (collideTile()).
- Note
- The function does not know the map's height, so a rectangle whose bottom row lies past the last map row reads whatever follows the tilemap in ROM — usually a non-zero byte, so it reads as a wall, but nothing guarantees it. Keep r->y + r->height - 1 inside the map, or add a solid last row to the collision map.
}
static const u8 collision_map[16 *14]
Tile-based collision map (16x14 = 224 bytes, 128x112 pixel area).
Definition main.c:96
u8 collideRectTile(const Rect *r, const u8 *tilemap, u16 mapWidth)
Check collision between rectangle and tilemap.
◆ collideTile()
| u8 collideTile |
( |
s16 | px, |
|
|
s16 | py, |
|
|
const u8 * | tilemap, |
|
|
u16 | mapWidth ) |
Check collision with tile at pixel coordinates.
Looks up the tile at a given pixel position and returns whether it's solid. Tiles are assumed to be 8x8 pixels.
- Parameters
-
| px | Pixel X coordinate |
| py | Pixel Y coordinate |
| tilemap | Pointer to collision tilemap (1 byte per tile, 0=empty, nonzero=solid) |
| mapWidth | Width of tilemap in tiles |
- Returns
- Tile value at position; 0 if empty. Off the map to the left, the top or the right (px < 0, py < 0, px >= mapWidth * 8) the function returns 1, "off the map is a wall" (since the v1 fix that let sprites slip past the left wall; this line said 0 until 2026-10-04). The bottom edge is not bounded: it does not know the map's height, so keep py inside the map.
- Note
- tilemap is const ON PURPOSE: const-qualified loads compile to bank-honouring far reads (#121), so the collision map may live in ANY bank — typically a multi-KB .incbin in a SUPERFREE section. With a non-const pointer the dereference is bank-$00-hardcoded and a map outside bank $00 reads garbage SILENTLY, which forced callers to waste RAM on their map.
player_vy = 0;
on_ground = 1;
}
◆ collideTileEx()
| u8 collideTileEx |
( |
s16 | px, |
|
|
s16 | py, |
|
|
const u8 * | tilemap, |
|
|
u16 | mapWidth, |
|
|
u8 | tileSize ) |
Check collision with tile using custom tile size.
Like collideTile() but allows custom tile sizes (8, 16, etc).
- Parameters
-
| px | Pixel X coordinate |
| py | Pixel Y coordinate |
| tilemap | Pointer to collision tilemap |
| mapWidth | Width of tilemap in tiles |
| tileSize | Size of tiles in pixels (must be power of 2: 8, 16, 32; anything else is treated as 8) |
- Returns
- Tile value at position; 0 if empty. Off the map to the left, the top or the right the function returns 1, like collideTile() (this line said 0 until 2026-10-05; the code has said 1 since the v1 fix). The bottom edge is not bounded.
◆ rectContains()
| u8 rectContains |
( |
const Rect * | inner, |
|
|
const Rect * | outer ) |
Check if rectangle is completely inside another.
- Parameters
-
| inner | Inner rectangle (potentially contained) |
| outer | Outer rectangle (container) |
- Returns
- 1 if inner is completely inside outer, 0 otherwise
◆ rectGetCenter()
| void rectGetCenter |
( |
const Rect * | r, |
|
|
s16 * | cx, |
|
|
s16 * | cy ) |
Get rectangle center point.
- Parameters
-
| r | Rectangle |
| cx | Pointer to store center X |
| cy | Pointer to store center Y |
◆ rectInit()
Initialize a rectangle.
- Parameters
-
| r | Rectangle to initialize |
| x | Left edge X |
| y | Top edge Y |
| w | Width |
| h | Height |
◆ rectSetPos()
Set rectangle position.
- Parameters
-
| r | Rectangle to modify |
| x | New X position |
| y | New Y position |