Ok, I'm stuck. I can not for anything get my collision detection to work. I have read so many posts on the subject, so I have a clue as to what must be done, but I just can't get it to work.
What I have so far is a background and a moving sprite. This is how I made the background.
I have the background nametable like so:
And then it is copied to PPU RAM $2000 like so:
I also have some collision data harcoded to start with:
Zeros are air and ones are solids.
Then I have CollisionData copied to RAM like this:
The variable coltab is declared like this:
Now for the gravity, I want to look up in the CollisionData table if the tile underneth the sprite is solid or not. I have gotten this far:
So far I think is correct, but what comes after that. First I had some idea to calculate what adress the tile below the sprite is at, like this:
$2000 + (32*SpriteY) + 32 + SpriteX
But it seams like I then would need to calculate som 16bit numbers or something.
So the question is basically what do I do next.
What I have so far is a background and a moving sprite. This is how I made the background.
I have the background nametable like so:
Code:
background:
.db $0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0 ;;row 1
.db $0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0 ;;
.db $0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0 ;;row 2
.db $0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0 ;;
.
.
.
.db $1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1 ;;row 29
.db $1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1 ;; THE FLOOR
.db $0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0 ;;row 30
.db $0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0 ;
.db $0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0 ;;row 1
.db $0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0 ;;
.db $0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0 ;;row 2
.db $0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0 ;;
.
.
.
.db $1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1 ;;row 29
.db $1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1,$1 ;; THE FLOOR
.db $0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0 ;;row 30
.db $0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0,$0 ;
And then it is copied to PPU RAM $2000 like so:
Code:
LoadBackground:
LDA $2002
LDA #$20
STA $2006
LDA #$00
STA $2006
LDA #$00
STA pointerLo
LDA #HIGH(background)
STA pointerHi
LDX #$00
LDY #$00
BGOutsideLoop:
BGInsideLoop:
LDA [pointerLo], Y
STA $2007
INY
CPY #$00
BNE BGInsideLoop
INC pointerHi
INX
CPX #$04
BNE BGOutsideLoop
LDA $2002
LDA #$20
STA $2006
LDA #$00
STA $2006
LDA #$00
STA pointerLo
LDA #HIGH(background)
STA pointerHi
LDX #$00
LDY #$00
BGOutsideLoop:
BGInsideLoop:
LDA [pointerLo], Y
STA $2007
INY
CPY #$00
BNE BGInsideLoop
INC pointerHi
INX
CPX #$04
BNE BGOutsideLoop
I also have some collision data harcoded to start with:
Code:
CollisionData:
.db %00000000, %00000000, %00000000, %00000000 ;row1(air)
.db %00000000, %00000000, %00000000, %00000000 ;row 2(air)
.
.
.
.db %11111111, %11111111, %11111111, %11111111 ;row29(floor)
.db %00000000, %00000000, %00000000, %00000000 ; row30(air)
.db %00000000, %00000000, %00000000, %00000000 ;row1(air)
.db %00000000, %00000000, %00000000, %00000000 ;row 2(air)
.
.
.
.db %11111111, %11111111, %11111111, %11111111 ;row29(floor)
.db %00000000, %00000000, %00000000, %00000000 ; row30(air)
Zeros are air and ones are solids.
Then I have CollisionData copied to RAM like this:
Code:
LoadColData:
LDA coltab
STA pointerLo
LDX #$00
LoadColDataLoop:
LDA CollisionData, x
STA pointerLo, x
INX
CPX #$78
BNE LoadColDataLoop
LDA coltab
STA pointerLo
LDX #$00
LoadColDataLoop:
LDA CollisionData, x
STA pointerLo, x
INX
CPX #$78
BNE LoadColDataLoop
The variable coltab is declared like this:
Code:
coltab .rs $78 ; 120 bytes, 1 bit per background tile (0 air, 1 solid)
Now for the gravity, I want to look up in the CollisionData table if the tile underneth the sprite is solid or not. I have gotten this far:
Code:
LDX MarioX ; Sprite X-pos.
LDY MarioY ; Sprite Y-pos.
;Calculating Y-tile number
TYA
LSR A
LSR A
LSR A ; divide by 8
INC A
INC A ; add 2 rows, to get under the sprite
;Calculating X-tile number
TXA
LSR A
LSR A
LSR A ; divide by 8
LDY MarioY ; Sprite Y-pos.
;Calculating Y-tile number
TYA
LSR A
LSR A
LSR A ; divide by 8
INC A
INC A ; add 2 rows, to get under the sprite
;Calculating X-tile number
TXA
LSR A
LSR A
LSR A ; divide by 8
So far I think is correct, but what comes after that. First I had some idea to calculate what adress the tile below the sprite is at, like this:
$2000 + (32*SpriteY) + 32 + SpriteX
But it seams like I then would need to calculate som 16bit numbers or something.
So the question is basically what do I do next.