← A299 Test for a collision ahead of the ball Map A341 → Active ball's tile-map probe origin (two bytes, little-endian)
A2D8: Set a ball's velocity from its heading code
Derives the active ball's per-axis step thresholds and signs from its heading code, through the velocity table.
set_ball_velocity A2D8 LD A,(active_ball) Select the active ball state record; this arm takes ball A.
A2DB OR A
A2DC JR NZ,set_ball_velocity_0
A2DE LD IY,ball_a_x_accum
A2E2 JR set_ball_velocity_1
set_ball_velocity_0 A2E4 LD IY,ball_b_x_accum This arm takes ball B.
set_ball_velocity_1 A2E8 LD E,A Index the heading array by the ball's slot.
A2E9 LD D,$00
A2EB LD HL,ball_a_heading
A2EE ADD HL,DE
A2EF LD A,(HL) Read this ball's heading code.
A2F0 CP $48 A heading code below the range is out of bounds.
A2F2 JR C,set_ball_velocity_2
A2F4 CP $90 One above it is out of bounds too.
A2F6 JR C,set_ball_velocity_3
set_ball_velocity_2 A2F8 LD A,$57 Either way reset it to the default heading, 72° up-right.
A2FA LD (HL),A
A2FB JR set_ball_velocity_5
set_ball_velocity_3 A2FD CP $48 Test the heading code against the four pure-axis headings – due right, straight up, due left and straight down.
A2FF JR Z,set_ball_velocity_4
A301 CP $5A
A303 JR Z,set_ball_velocity_4
A305 CP $6C
A307 JR Z,set_ball_velocity_4
A309 CP $7E
A30B JR NZ,set_ball_velocity_5
set_ball_velocity_4 A30D ADD A,$06 Any of them is nudged six codes counterclockwise, which avoids a flat endless bounce. Due right becomes 27°, straight up 125°, due left 207° and straight down 305°.
A30F LD (HL),A
set_ball_velocity_5 A310 SUB $48 Index the velocity table by the heading code's offset into the range, three bytes each.
A312 LD C,A
A313 ADD A,A
A314 ADD A,C
A315 LD HL,velocity_table
A318 LD E,A
A319 LD D,$00
A31B ADD HL,DE
A31C LD A,(HL)
A31D LD (IY+$02),A Store the X step threshold.
A320 INC HL
A321 LD A,(HL) Store the Y step threshold.
A322 LD (IY+$03),A
A325 INC HL
A326 LD A,(HL)
A327 AND $10 X direction sign from the sign byte
A329 RRCA
A32A RRCA
A32B RRCA
A32C RRCA
A32D LD (IY+$04),A
A330 LD A,(HL)
A331 AND $01 Y direction sign from the same byte
A333 LD (IY+$05),A
A336 XOR A The new heading takes the ball off the bat and restarts both sub-pixel accumulators.
A337 LD (IY+$0B),A
A33A LD (IY+$00),A
A33D LD (IY+$01),A
A340 RET
← A299 Test for a collision ahead of the ball Map A341 → Active ball's tile-map probe origin (two bytes, little-endian)
Fork me on GitHub