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

Aim, distance, and angle showcase for <snes/math.h>. More...

#include <snes.h>
#include <snes/text.h>
#include <snes/math.h>
#include <snes/fixed32.h>
#include <snes/gameloop.h>
#include "res/units.inc"
#include "res/roof.inc"

Macros

#define HALF   16
 Half a 32×32 sprite: from its centre to the top-left corner OAM wants.
#define OBJ_VRAM   0x4000
 The sprites' tiles, after the font and the maps.
#define ROOF_MAP   0x3C00
#define ROOF_TILES   0x1000
 Where BG2 (the roof) keeps its tiles and its map; the text has $0000 and $3800.
#define TILE_DRONE   256
 The drones' frames in res/units.png, after the turret's sixteen.
#define TILE_ORBITER   260
#define TURRET_X   128
 The turret's centre — fixed at the screen centre.
#define TURRET_Y   112

Functions

int main (void)
static void on_init (void)
static void on_update (void)
static void orbit_step (void)
 Advance the orbiter one frame: centre (128, 96), radius 60 px, one LUT step a frame (an orbit in about 4.3 s at 60 Hz).
static u16 turret_tile (u8 heading)
 The OAM tile of the turret's frame for heading (0–15).

Variables

fixed32 g_angle
 The orbiter: angle (16.16, its integer part a LUT step), velocity, radius.
u8 g_angle8
fixed32 g_omega
fixed32 g_radius
s16 g_sx
 The orbiter's centre on screen and its angle as a LUT index.
s16 g_sy
static s16 prev_target_x
 Previous target X — for change detection (skip text reformat when nothing moved).
static s16 prev_target_y
 Previous target Y — see prev_target_x.
static s16 target_x
 The drone's centre, X (movable, 16–240: the sprite stays on screen).
static s16 target_y
 The drone's centre, Y (movable, 16–208).
u8 turret_heading
 The turret's heading, 0–15 (sixteenths of a turn): its frame.

Detailed Description

Aim, distance, and angle showcase for <snes/math.h>.

Demonstrates the four math primitives a 2D action game leans on the most: sqrt16 (pixel distance), atan2_8 (8-bit angle to a target), and the fixCos / fixSin LUTs (project a velocity vector along that angle). Everything updates live as the user moves the target with the D-pad — the numbers on screen are real arithmetic, not pre-baked.

A security turret sits on a roof at the screen centre; the drone it aims at starts upper-right and follows the D-pad. Each frame the example recomputes dx/dy from the two positions, calls sqrt16(dx² + dy²) for the pixel distance, calls atan2_8(dy, dx) for the 8-bit angle, then calls fixCos and fixSin on that angle to read back the unit direction vector. All five values are printed live in the text panel. The turret turns to face the drone: it has sixteen frames, one per sixteenth of a turn, and the angle picks one ((angle + 8) >> 4, the nearest heading) — the "rotate a sprite to face X" pattern, with no rotation hardware.

A second drone orbits the roof on its own: its angle is a 16.16 accumulator (fixed32) advanced by a 16.16 angular velocity every frame, and its position is radius * fix32Cos / radius * fix32Sin through fix32Mul — the motion half of the fixed-point story (it was the fix32_orbit example).

SNES Concepts
  • <snes/math.h> square-root and atan2:
    • sqrt16(n) → integer floor of √n, range 0–255
    • atan2_8(dy, dx) → 8-bit angle in the same convention as the sin/cos LUT (0 = +X, 64 = +Y, 128 = −X, 192 = −Y)
  • fixSin/fixCos LUT chaining: feed the atan2_8 output back into the trig LUT to recover a unit direction vector that points from the turret to the drone — the canonical pattern for projectile aiming, pursuit AI, and any "rotate sprite to face X" code.
  • <snes/fixed32.h> motion: an angle accumulator and a velocity in 16.16, fix32Mul(radius, fix32Cos(angle)) for the position; the integer part of the accumulator (UNFIX32) indexes the 256-step LUT
  • Mode 0 + sprite overlay: BG1 hosts the text via the text module, BG2 the roof (2bpp, its own four colours at CGRAM 32), and the turret and the drones are 32×32 OBJ sprites over both.
  • A sprite's position is its top-left corner: the example keeps the centres (what the maths should aim between) and draws each 32×32 sprite 16 pixels up and left of its centre.
What to Observe
  • The turret stays at the screen centre and turns its barrel to the drone, in steps of a sixteenth of a turn.
  • The drone follows the D-pad. Diagonal input moves diagonally — both axes update on the same frame.
  • As you move the target, the live readout panel updates on every frame:
    • DX / DY are the signed distances from the turret to the drone.
    • DIST is sqrt16(dx² + dy²), in pixels (0–255).
    • ANGLE is the 8-bit angle from the turret to the drone, printed in hex; the equivalent in degrees follows in parentheses (≈ angle × 360 / 256).
    • COS 8.8 / SIN 8.8 are the 8.8 fixed-point components of the unit direction vector. At ANGLE = 0x40 (90°, target is straight south) you see COS = 0x0000, SIN = 0x0100.
    • COS 16.16 / SIN 16.16 are the SAME values lifted to the fix32 type. Look at the bottom hex digits: they're always 00 because fix32Cos/fix32Sin reuse the 8.8 LUT and shift up. A future 16-bit LUT would fill those bits with real precision — the comparison row makes the 8-bit-LUT precision floor visible at runtime.
8.8 and 16.16 side by side
The fixCos/fixSin (8.8) rows have fix32Cos/fix32Sin (16.16) rows beside them, so the two representations can be compared; the trailing zeros of the 16.16 ones show that they lift the same 8.8 table.
Modules Used
console, sprite, dma, background, text, input, gameloop, math, fixed32
See also
math.h, fixed32.h, sprite.h, text.h, gameloop.h

Macro Definition Documentation

◆ HALF

#define HALF   16

Half a 32×32 sprite: from its centre to the top-left corner OAM wants.

◆ OBJ_VRAM

#define OBJ_VRAM   0x4000

The sprites' tiles, after the font and the maps.

◆ ROOF_MAP

#define ROOF_MAP   0x3C00

◆ ROOF_TILES

#define ROOF_TILES   0x1000

Where BG2 (the roof) keeps its tiles and its map; the text has $0000 and $3800.

◆ TILE_DRONE

#define TILE_DRONE   256

The drones' frames in res/units.png, after the turret's sixteen.

◆ TILE_ORBITER

#define TILE_ORBITER   260

◆ TURRET_X

#define TURRET_X   128

The turret's centre — fixed at the screen centre.

Made into a #define rather than a runtime variable so the compiler folds the constant into the dx/dy expressions and we don't pay a load+sub for a quantity that never changes.

◆ TURRET_Y

#define TURRET_Y   112

Function Documentation

◆ main()

int main ( void )

◆ on_init()

void on_init ( void )
static

◆ on_update()

void on_update ( void )
static

◆ orbit_step()

void orbit_step ( void )
static

Advance the orbiter one frame: centre (128, 96), radius 60 px, one LUT step a frame (an orbit in about 4.3 s at 60 Hz).

◆ turret_tile()

u16 turret_tile ( u8 heading)
static

The OAM tile of the turret's frame for heading (0–15).

A 32×32 frame is 4×4 tiles and a row of OBJ tiles is 16 wide, so heading h (four a row of the sheet) starts at column (h % 4) * 4 of tile row (h / 4) * 4.

Variable Documentation

◆ g_angle

fixed32 g_angle

The orbiter: angle (16.16, its integer part a LUT step), velocity, radius.

◆ g_angle8

u8 g_angle8

◆ g_omega

fixed32 g_omega

◆ g_radius

fixed32 g_radius

◆ g_sx

s16 g_sx

The orbiter's centre on screen and its angle as a LUT index.

◆ g_sy

s16 g_sy

◆ prev_target_x

s16 prev_target_x
static

Previous target X — for change detection (skip text reformat when nothing moved).

◆ prev_target_y

s16 prev_target_y
static

Previous target Y — see prev_target_x.

◆ target_x

s16 target_x
static

The drone's centre, X (movable, 16–240: the sprite stays on screen).

◆ target_y

s16 target_y
static

The drone's centre, Y (movable, 16–208).

◆ turret_heading

u8 turret_heading

The turret's heading, 0–15 (sixteenths of a turn): its frame.