Re: Collision solid objects, level data, tile-based movement
Posted: Sat Aug 08, 2015 7:30 am
Row one of data is 0-31. Row two is 32-63. Etc.
In the code you posted, vpos is divided by 8. Adding that to the pointer can only add a range of 0-31. So no matter what it is, the row will never change.
The equation is vpos/8 * 32 + xpos/8.
Since you're dividing by 8, and then multiplying by 32, you need two bytes to represent the full range.
Dividing by 8, then multiplying by 8 basically only accomplishes zeroing out the lowest 3 bits. So you can multiply by 4 without the divide and just AND out the lowest 3 bits.
A final note on the multiply for Y. If the range of a row is 32 bytes, that needs 5 bits.
00YXXXXX. Those 5 X bits are which column, chosen by the X position. To go to the next row, you'd add one to the bit marked Y. So the range of Y is put in the 5 bits higher than X. 3 in the low byte, 2 in the high byte.
In the code you posted, vpos is divided by 8. Adding that to the pointer can only add a range of 0-31. So no matter what it is, the row will never change.
The equation is vpos/8 * 32 + xpos/8.
Since you're dividing by 8, and then multiplying by 32, you need two bytes to represent the full range.
Dividing by 8, then multiplying by 8 basically only accomplishes zeroing out the lowest 3 bits. So you can multiply by 4 without the divide and just AND out the lowest 3 bits.
Code: Select all
;There's a faster way, of course
lda vpos
asl;This moves the highest bit of vpos into the carry, and shifts all the other bits
sta pageloc
lda #$00
rol a;the highest bit of vpos is now in A
sta pageloc2
lda pageloc
asl a;This moves the next highest bit of vpos into the carry
and #%11100000;getting rid of the lowest bits.
sta pageloc
rol pageloc2;Now both of the highest two bits of vpos are in the pointer.
;Now we add where the nametable is. You said this was $8000. Let's just assume screendata is a label there.
lda #<screendata
clc
adc pageloc
sta pageloc
lda #>screendata
adc pageloc2
sta pageloc2
;Now we have an address pointing to the first byte of the row we're currently on. So the X position divided by 8 can help us pick which column.
lda xpos
lsr
lsr
lsr
tay
lda(pageloc),y
00YXXXXX. Those 5 X bits are which column, chosen by the X position. To go to the next row, you'd add one to the bit marked Y. So the range of Y is put in the 5 bits higher than X. 3 in the low byte, 2 in the high byte.
Code: Select all
Hi=000000YY
Lo=YYYXXXXX