Collision Detection in Depth: GTIA's 16 Collision Registers and Beyond

GRAPHICS 3 screen with red, green and teal vertical bars; a purple square player sits on the green bar beside a thin white player, a small purple missile on the teal bar, and a text window lists collision register values

GTIA can tell you, pixel-exactly and for free, when a player or missile touches the playfield or another player. Most Atari programmers meet the collision registers in a first player/missile program. Using them well in a real game means knowing exactly what each bit means, when the bits are set, when to clear them, and when a software test is the better tool.

Player/Missile Graphics in Atari BASIC and Player/Missile Graphics in Assembly introduce P0PF and HITCLR. This guide covers all sixteen registers and the rules around them.

What you'll learn: the complete register map, how to decode the bits, a BASIC program that shows the registers live, latching and clearing in a VBI, behaviour in text, multicolour and GTIA modes, collision tricks, and software bounding boxes.

What you need: Atari BASIC for the explorer program, MADS for the routines, and player/missile graphics already working.

1. The register map

The collision registers are GTIA's read side of $D000–$D00F (53248–53263). Writing to the same addresses sets positions and sizes, which is why POKE 53248,PEEK(53248)+1 can't move a player.

Address Dec Read name Meaning of bits 0–3
$D000–$D003 53248–53251 M0PF–M3PF missile n touched playfield PF0, PF1, PF2, PF3
$D004–$D007 53252–53255 P0PF–P3PF player n touched PF0–PF3
$D008–$D00B 53256–53259 M0PL–M3PL missile n touched player 0, 1, 2, 3
$D00C–$D00F 53260–53263 P0PL–P3PL player n touched player 0, 1, 2, 3
$D01E 53278 HITCLR (write) any value clears all collision bits

In every register bit 0 (value 1) is the first object (PF0 or player 0), bit 1 (2) the second, bit 2 (4) the third and bit 3 (8) the fourth. The upper four bits are unused, so mask with AND #15 in assembly if you test the whole byte.

The count: 32 playfield bits, 16 missile-to-player bits and 12 player-to-player bits, 60 in total. A collision between players 0 and 2 sets two bits: bit 2 of P0PL and bit 0 of P2PL. A player's bit for itself is always 0. There are no missile-to-missile collisions and no collisions with the background colour.

2. Watching them live (BASIC)

This program draws PF0, PF1 and PF2 bars in GRAPHICS 3, slides player 0 and missile 0 across them with player 1 parked in the way, and prints five of the registers after each step:

10 REM COLLISION REGISTER EXPLORER
20 A=PEEK(106)-8:POKE 106,A:GRAPHICS 3
30 PM=A*256:POKE 54279,A
40 FOR I=PM+384 TO PM+1023:POKE I,0:NEXT I
50 FOR C=1 TO 3:COLOR C:FOR X=C*10-2 TO C*10+1
60 PLOT X,0:DRAWTO X,19:NEXT X:NEXT C
70 FOR I=40 TO 47:POKE PM+512+I,255
80 POKE PM+640+I,24:NEXT I
90 FOR I=60 TO 63:POKE PM+384+I,3:NEXT I
100 POKE 704,88:POKE 705,14:POKE 623,1
110 POKE 559,46:POKE 53277,3
120 POKE 53249,128
130 FOR X=48 TO 124 STEP 4
140 POKE 53248,X:POKE 53252,X+40
150 T=PEEK(20):IF PEEK(20)=T THEN 150
160 T=PEEK(20):IF PEEK(20)=T THEN 160
170 ? CHR$(125);"X=";X;" P0PF=";PEEK(53252);
180 ? " M0PF=";PEEK(53248)
190 ? "P0PL=";PEEK(53260);" P1PL=";PEEK(53261);
200 ? " M0PL=";PEEK(53256)
210 POKE 53278,0:NEXT X
220 GOTO 220
  • Lines 20–40 reserve 2K under RAMTOP for double-line player/missile memory (missiles at +384, player 0 at +512, player 1 at +640) and clear it.
  • Lines 50–60 draw bars in COLOR 1–3, which are PF0–PF2 in GRAPHICS 3.
  • Lines 70–90 make player 0 a solid 8 × 8 block, player 1 a thin bar (24 = two pixels) on the same rows, and missile 0 a short stub on lower rows (value 3 = missile 0's two bits).
  • Lines 150–160 wait for RTCLOK (20) to change twice, so at least one complete frame has been drawn with the new positions before the registers are read.
  • Line 210 writes HITCLR only after reading, then moves on.

At the end player 0 sits on the green PF1 bar and overlaps player 1, and missile 0 sits on the PF2 bar: P0PF = 2, M0PF = 4, P0PL = 2 (player 1) and P1PL = 1 (player 0). M0PL stays 0 because the missile never meets a player. On the way across, notice that P0PF reads 0 between the bars: background never counts.

3. When collisions are set, and when to clear them

A collision bit is set at the moment the beam draws the overlapping pixel, and then latches: it stays set until HITCLR. So:

  • A register describes what has been drawn since the last HITCLR. Move a player and read immediately, and you see the old position's collisions, or nothing if you cleared in between. It can take up to a whole frame (about 16 ms) for the new position to be drawn.
  • The standard order, in the VBI: read the collision registers (they describe the complete frame just displayed), write HITCLR, then move the objects. Done that way, every frame's collisions are seen exactly once.
  • Only the visible area counts. Overlaps in horizontal or vertical blank (horizontal positions below 34 or above 221, scan lines outside 8–247) never register. A sprite parked off screen is safe.
  • Collisions accumulate. If you don't clear every frame, a register can hold several bits from different events, so a test like IF PEEK(53252)=2 fails when another bit is also set.

This routine takes a snapshot of all sixteen registers for the rest of the program to inspect, then clears them. Call it at the start of your VBI:

; --- snippet 1: copy all 16 collision registers, then clear them
snapshot ldx #15
snap    lda M0PF,x              ; $D000-$D00F read as collision registers
        sta hits,x
        dex
        bpl snap
        sta HITCLR              ; any value clears every latch
        rts
hits    :16 dta 0               ; hits+0 = M0PF ... hits+15 = P3PL

4. What counts as "playfield"

Collisions follow the playfield colour signals, not the colour values, and some modes have surprises:

  • Colour doesn't matter. Two objects in the same colour still collide, and a player hidden behind the playfield by priority still registers. You can deliberately draw "invisible" playfield in the background colour (using a register whose colour matches the background) or park invisible players to make walls, triggers and goal zones that the hardware checks for you.
  • Hi-res modes (GRAPHICS 0 and 8, ANTIC 2, 3 and F): set pixels count as PF2, whatever luminance you see; cleared pixels never collide. GTIA works in colour clocks, so a player touching either half of a pixel pair registers.
  • ANTIC 4 and 5: each bit pair reports its own register, and inverse characters report PF3 for their 11 pixels. Two otherwise identical tiles can therefore be told apart in hardware (see ANTIC Modes 4 and 5).
  • GTIA modes: GRAPHICS 9 and 11 produce no playfield collisions at all. In GRAPHICS 10, pixels using the PF0–PF3 colour codes register, but pixels using the player colour codes do not. The difference between a CTIA and a GTIA in mode 9 is the basis of the classic chip-detection test (see GTIA Graphics Modes 9, 10 and 11).
  • Priority settings, multicolour players and the fifth player don't change collisions. With the fifth player enabled, each missile still reports its own Mn registers and no extra PF3 collisions appear.

5. Collisions in a real game

Experienced Atari coders mostly use hardware collisions for "something hit something" events (bullets, enemies, pickups) and software tests for movement. The reason is simple: the hardware tells you about an overlap after it has been drawn, but a platform character must stop before it sinks into the wall. Common software methods:

  • Map lookups. Convert the sprite's coordinates to a character cell and look at the tile there. This is ideal with character-mode playfields; a tile type can carry flags such as solid, ladder or deadly.
  • Bounding boxes. Compare rectangles. It is cheap, works on objects that aren't on screen yet, and tells you which two objects collided even when players are multiplexed.
  • Height or distance tables. The modern multi-player game Sails of Doom checks ship against ship with a 16 × 16 table of precomputed distances. For sloped ground, a table of terrain heights per X coordinate is a simple answer.

Here is a minimal bounding-box test for two 8 × 8 objects. It returns carry set on a hit and was checked against four test cases:

; --- snippet 2: does object (objx,objy,8x8) overlap the hero box?
; returns carry set on a hit
boxhit  lda objx
        sec
        sbc herox               ; dx = objx - herox
        bcs @+
        eor #$FF                ; negative: make it positive
        adc #1
@       cmp #8                  ; |dx| < 8 (both boxes 8 wide)?
        bcs miss
        lda objy
        sec
        sbc heroy
        bcs @+
        eor #$FF
        adc #1
@       cmp #8                  ; |dy| < 8 ?
        bcs miss
        sec                     ; overlap on both axes: hit
        rts
miss    clc
        rts

For boxes of different sizes, compare against the sum of half-widths or use the four-edge test (A.left < B.right and B.left < A.right, and the same vertically).

6. Multiplexed and per-band collisions

If players are reused down the screen with DLIs (see Player Multiplexing), a set bit says only that some copy of the player collided. To find out which, read the registers at the end of each band inside its DLI, store them in a per-band table and write HITCLR, so the next band starts with clear latches. The VBI then reads the table. This keeps hardware detection usable even with many reused sprites.

Troubleshooting

  • Collision never detected: you cleared with HITCLR after moving but before a frame was drawn, or the object is in the border or blank area.
  • Collision detected one frame late or "sticks": you read too early, or never write HITCLR.
  • IF PEEK(...)=n fails although objects touch: another bit is also set; test bits instead, for example IF PEEK(53252)>0, or clear more often.
  • No playfield hits in GRAPHICS 9 or 11: these modes don't generate playfield collisions; use software tests.
  • GRAPHICS 0 text always reports PF2: correct; hi-res pixels are PF2 for collision purposes.

Further reading

  • De Re Atari (Atari, 1982), Chapter 4: hardware collision detection and the read–clear–move order.
  • Mapping the Atari by Ian Chadwick (COMPUTE! Books), entries 53248–53263 and 53278.
  • Altirra Hardware Reference Manual, section 6.6: collision detection in full detail, including the blanking regions and GTIA modes.
  • Atari Graphics and Arcade Game Design by Jeffrey Stanton and Dan Pinal (Arrays, 1984), Chapter 5: collision registers and explosions in a Space War game.
  • AtariAge forum thread "Collision detection - best practice" (2019): developers compare hardware and software approaches.

Credits