Loading...
Searching...
No Matches
collision.h File Reference

SNES Collision Detection. More...

#include <snes/types.h>

Go to the source code of this file.

Classes

struct  Rect
 Axis-aligned bounding box (rectangle) More...
 

Functions

u8 collidePoint (s16 x, s16 y, Rect *r)
 Check point vs rectangle collision.
 
u8 collideRect (Rect *a, Rect *b)
 Check rectangle vs rectangle collision (AABB)
 
u8 collideRectEx (Rect *a, Rect *b, s16 *overlapX, s16 *overlapY)
 Check if two rectangles overlap and return overlap amount.
 
u8 collideRectTile (Rect *r, u8 *tilemap, u16 mapWidth)
 Check collision between rectangle and tilemap.
 
u8 collideTile (s16 px, s16 py, u8 *tilemap, u16 mapWidth)
 Check collision with tile at pixel coordinates.
 
u8 collideTileEx (s16 px, s16 py, u8 *tilemap, u16 mapWidth, u8 tileSize)
 Check collision with tile using custom tile size.
 
u8 rectContains (Rect *inner, Rect *outer)
 Check if rectangle is completely inside another.
 
void rectGetCenter (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.
 

Detailed Description

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

// Define collision boxes for player and enemy
Rect playerBox = { player_x, player_y, 16, 16 };
Rect enemyBox = { enemy_x, enemy_y, 16, 16 };
// Check collision
if (collideRect(&playerBox, &enemyBox)) {
// Handle collision
player_hit();
}
// Tile-based collision (for platformers)
if (collideTile(player_x + 8, player_y + 16, tilemap, 32)) {
// Player is standing on solid ground
on_ground = 1;
}
static s16 enemy_y[4]
Y positions of the four static enemy sprites.
Definition main.c:64
static s16 player_y
Player Y position in screen coordinates.
Definition main.c:57
static s16 enemy_x[4]
X positions of the four static enemy sprites.
Definition main.c:62
static s16 player_x
Player X position in screen coordinates.
Definition main.c:55
u8 collideTile(s16 px, s16 py, u8 *tilemap, u16 mapWidth)
Check collision with tile at pixel coordinates.
u8 collideRect(Rect *a, Rect *b)
Check rectangle vs rectangle collision (AABB)
u8 tilemap[]
Axis-aligned bounding box (rectangle)
Definition collision.h:54
Author
OpenSNES Team

Function Documentation

◆ collidePoint()

u8 collidePoint ( s16  x,
s16  y,
Rect r 
)

Check point vs rectangle collision.

Tests if a point is inside a rectangle.

Parameters
xPoint X coordinate
yPoint Y coordinate
rRectangle to test against
Returns
1 if point is inside rectangle, 0 otherwise
// Check if bullet hits target
if (collidePoint(bullet_x, bullet_y, &target)) {
target_hit();
}
u8 collidePoint(s16 x, s16 y, Rect *r)
Check point vs rectangle collision.

◆ collideRect()

u8 collideRect ( Rect a,
Rect b 
)

Check rectangle vs rectangle collision (AABB)

Tests if two axis-aligned bounding boxes overlap.

Parameters
aFirst rectangle
bSecond rectangle
Returns
1 if rectangles overlap, 0 otherwise
Rect player = { px, py, 16, 16 };
Rect enemy = { ex, ey, 16, 16 };
if (collideRect(&player, &enemy)) {
// Collision detected
}
static u16 px
Definition main.c:166

◆ collideRectEx()

u8 collideRectEx ( Rect a,
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
aFirst rectangle
bSecond rectangle
overlapXPointer to store X overlap (negative = left, positive = right)
overlapYPointer to store Y overlap (negative = up, positive = down)
Returns
1 if rectangles overlap, 0 otherwise
s16 dx, dy;
if (collideRectEx(&player, &wall, &dx, &dy)) {
// Push player out of wall
}
u8 collideRectEx(Rect *a, 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:49

◆ collideRectTile()

u8 collideRectTile ( Rect r,
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
rRectangle to test
tilemapPointer to collision tilemap
mapWidthWidth of tilemap in tiles
Returns
1 if any corner touches solid tile, 0 otherwise
Rect playerBox = { player_x, player_y, 16, 16 };
if (collideRectTile(&playerBox, collision_map, 32)) {
// Player hit solid tiles
}
static u8 collision_map[16 *14]
Tile-based collision map (16x14 = 224 bytes, 128x112 pixel area)
Definition main.c:94
u8 collideRectTile(Rect *r, u8 *tilemap, u16 mapWidth)
Check collision between rectangle and tilemap.

◆ collideTile()

u8 collideTile ( s16  px,
s16  py,
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
pxPixel X coordinate
pyPixel Y coordinate
tilemapPointer to collision tilemap (1 byte per tile, 0=empty, nonzero=solid)
mapWidthWidth of tilemap in tiles
Returns
Tile value at position (0 if empty/out of bounds)
// Check if player would hit ground
player_vy = 0; // Stop falling
on_ground = 1;
}

◆ collideTileEx()

u8 collideTileEx ( s16  px,
s16  py,
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
pxPixel X coordinate
pyPixel Y coordinate
tilemapPointer to collision tilemap
mapWidthWidth of tilemap in tiles
tileSizeSize of tiles in pixels (must be power of 2: 8, 16, 32)
Returns
Tile value at position (0 if empty/out of bounds)

◆ rectContains()

u8 rectContains ( Rect inner,
Rect outer 
)

Check if rectangle is completely inside another.

Parameters
innerInner rectangle (potentially contained)
outerOuter rectangle (container)
Returns
1 if inner is completely inside outer, 0 otherwise

◆ rectGetCenter()

void rectGetCenter ( Rect r,
s16 cx,
s16 cy 
)

Get rectangle center point.

Parameters
rRectangle
cxPointer to store center X
cyPointer to store center Y

◆ rectInit()

void rectInit ( Rect r,
s16  x,
s16  y,
u16  w,
u16  h 
)

Initialize a rectangle.

Parameters
rRectangle to initialize
xLeft edge X
yTop edge Y
wWidth
hHeight

◆ rectSetPos()

void rectSetPos ( Rect r,
s16  x,
s16  y 
)

Set rectangle position.

Parameters
rRectangle to modify
xNew X position
yNew Y position