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