More n00b sprite collisions with background

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
mathe-matician
Posts: 8
Joined: Sat Aug 24, 2019 2:21 pm

More n00b sprite collisions with background

Post by mathe-matician » Wed Nov 13, 2019 7:08 pm

I need some help/clarification on this.

The most useful post I've found was here https://forums.nesdev.com/viewtopic.php?f=10&t=12995 and I've been trying some of the code examples in it which you'll see below.

Right now I have a 32x30 byte array for my background that I load through 5 loops via:

Code: Select all

LoadBackground:
	LDA $2002
	LDA #$20
	STA $2006             
	LDA #$00
	STA $2006             

	LDY #$00
        LDX #$00
LoadBackgroundLoop1:
	LDA background_lvl_1_1, y
	STA $2007
	INY
	INX
	CPX #$00
	BNE LoadBackgroundLoop1

;;done 4 more times 


I'm going off of tokumaru's code example in the above post and I have come to this:

Code: Select all

        LDA #$00
	STA L
	LDA PLAYER_POS_Y ;oam $0200
	AND #%11111000 ;why this?
	ASL
	ROL L
	ASL
	ROL L
	ADC #<background_lvl_1_1
	STA H
	LDA L
	ADC #>background_lvl_1_1
	STA L
	LDA PLAYER_POS_X ;oam $0203
	LSR
	LSR
	LSR
	TAY

	LDA (H), Y
	CMP #$04
	BCC No_Collision
	
Collision:
	LDA #$02
	STA $0202
	JMP CollisionDone
No_Collision:	
	LDA #$00
	STA $0202
CollisionDone:
H and L are in the zero page
H = $20
L = H+1

Right now I have my sprite player just turning a different color to show me if the collision is working, but I want to register a collision when the player hits a tree, which is #$04 in background_lvl_1_1 while the blank pathways are #$00.

I can see in the debugger that the final value after LDA (H), y is always #$04 no matter where the character is on screen and is always from a single location - so it isn't updating the location of every time I move the player sprite. I currently have this above collisions routine in my "forever" loop as well.

Is that because it is only reading from "background_lvl_1_1" and not going through the other sets of background data I have? Do I have to run this same routine with all my background data? (background_lvl_1_2, background_lvl_1_3, etc?) It seems so close, what am I missing? I'm not using meta sprites either, if that has anything to do with this...

(Sorry for my noobness, and thanks in advance)
Attachments
shot1.png

User avatar
dougeff
Posts: 2617
Joined: Fri May 08, 2015 7:17 pm
Location: DIGDUG
Contact:

Re: More n00b sprite collisions with background

Post by dougeff » Wed Nov 13, 2019 8:23 pm

What assembler are you using?

NESASM requires this syntax for indirect

LDA [address low byte], y

brackets, not parentheses

And low address needs to be a variable in the zero page.

Edit...
Also, HIGH() returns high byte of 16 bit value, LOW() returns the low byte. So, this line

ADC #<background_lvl_1_1

should be

ADC #LOW(background_lvl_1_1)

and this line

ADC #>background_lvl_1_1

should be

ADC #HIGH(background_lvl_1_1)
nesdoug.com -- blog/tutorial on programming for the NES

mathe-matician
Posts: 8
Joined: Sat Aug 24, 2019 2:21 pm

Re: More n00b sprite collisions with background

Post by mathe-matician » Wed Nov 13, 2019 9:56 pm

I'm using asm6, it doesn't use this same syntax, does it?

User avatar
dougeff
Posts: 2617
Joined: Fri May 08, 2015 7:17 pm
Location: DIGDUG
Contact:

Re: More n00b sprite collisions with background

Post by dougeff » Wed Nov 13, 2019 10:05 pm

Well then, never mind.

Oh, I think you have your L's and H's backwards.

This, for example

LDA (H), Y

should be

LDA (L), Y

and this

ROL L

should be

ROL H

etc.



L and H should be in the zeropage, with H being one more than L.
nesdoug.com -- blog/tutorial on programming for the NES

mathe-matician
Posts: 8
Joined: Sat Aug 24, 2019 2:21 pm

Re: More n00b sprite collisions with background

Post by mathe-matician » Fri Nov 15, 2019 10:35 am

Thanks for looking at it dougeff! I figured out the problem - I switched PLAYER_POS_Y and PLAYER_POS_X out with shadow_oam and shadow_oam+3. And now my collisions with the trees register. Awesome. I don't know why that would make a difference though since they have the same values of the player y and x: $0200, $0203.

Post Reply