A box inside the picture, the way Mario's is
The stomp lesson’s square and walker, with the thing that decides a touch made visible. Press Select to see each one’s box, then walk into the walker: the pictures overlap first, and nothing happens until the boxes do. The program is 397 instructions in 853 bytes, built and checked the same way.
Play it here Change it on the desk The cartridge, to play anywhere that plays NES cartridges: hitbox.nes
On the screen
What touches is smaller than what you see
Super Mario Bros. keeps a box for Mario and one for every enemy, four edges each, written into memory every frame from where the thing is. We read them off the running game: Mario’s box is three pixels in from either side of his picture and four down from its top, to its bottom; a Goomba’s is a band across the middle of its picture, six down from the top and four up from the bottom. When Mario walked into a Goomba, the two pictures were already overlapping a frame before the boxes were, and the hit came only with the boxes. This cartridge keeps its boxes the same way, with those numbers.
| this cartridge | Super Mario Bros. | |
|---|---|---|
| the player's box, in from its picture: left, top, right, bottom | 3, 4, 3, 0 | 3, 4, 3, 0 |
| the walker's box, in from its picture | 3, 6, 3, 4 | 4, 6, 2, 4 |
| the first touch: the pictures overlapped by, across and down, the frame before it was known | 4, 16 | 8, 16 |
| and the boxes by | not yet, 2 apart | 2, 6 |
| frames the pictures had overlapped while the boxes did not | 2 | 3 |
| the touch is known | as the boxes meet | the frame after the boxes meet |
As written
The part of the program this lesson is about. The whole program is a plain text file: hitbox.s
;; The boxes, from the positions, every frame: the square's is 3 in from
;; each side and 4 down from the top of its picture, to its bottom; the
;; walker's is 3 in from each side, 6 down from the top and 4 up from
;; the bottom. The right and bottom edges are one past the last pixel,
;; so two boxes touch when each left edge is before the other's right.
boxes:
LDA $11
CLC
ADC #$03
STA $70
ADC #$0A
STA $72
LDA $15
CLC
ADC #$04
STA $71
ADC #$0C
STA $73
LDA $60
CLC
ADC #$03
STA $74
ADC #$0A
STA $76
LDA $64
CLC
ADC #$06
STA $75
ADC #$06
STA $77
RTS
;; Touching: the two boxes overlap when each one's left edge is before
;; the other's right, and each one's top is above the other's bottom.
;; Then the square goes back to where it started, the walker to the
;; right edge, and a touch is counted. When only the pictures overlap
;; (closer than 16 apart both ways) the frame is counted too, and
;; nothing else happens.
touch:
LDA $70
CMP $76
BCS apart
LDA $74
CMP $72
BCS apart
LDA $71
CMP $77
BCS apart
LDA $75
CMP $73
BCS apart
JMP hit
apart:
SEC
LDA $11
SBC $60
BPL across
EOR #$FF
CLC
ADC #$01
across:
CMP #$10
BCS clear2
SEC
LDA $15
SBC $64
BPL down
EOR #$FF
CLC
ADC #$01
down:
CMP #$10
BCS clear2
INC $67
clear2:
RTS
hit:
LDA #$28
STA $11
LDA #$B0
STA $15
LDA #$F0
STA $60
LDA #$00
STA $61
STA $10
STA $12
STA $13
STA $14
STA $16
STA $17
STA $03
INC $65
RTSBefore this one: A stomp and a hit, told apart the way Mario does