Copy SPR X / Y position to similar in NameTable (PPU addr.)

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
sdm
Posts: 412
Joined: Tue Apr 11, 2006 4:08 am
Location: Poland

Copy SPR X / Y position to similar in NameTable (PPU addr.)

Post by sdm »

I would copy the X / Y coordinates to match the positions 8x8 on the BGR (NT PPU) screen. It is a similar method of placing an object on the screen as it is in Bomber Man. I managed to get a LO byte from a PPU address, but I have a problem with HI byte (20xx-23xx). It seemed to me more simple, but of course ...: /

Code: Select all

	CLC
	LDA HERO_1_X			;X
 
	LSR A
	LSR A
	LSR A

	STA BombX_Temp			;jest ok, co 8pix daje wartosc o 1

	CLC
	LDA HERO_1_Y
	AND #%11111000
	STA BombY_Temp


	LDA BombY_Temp
	ADC BombX_Temp
	STA BOMB_PPU_TILE1_AddrLO
	CLC
	ADC #1
	STA BOMB_PPU_TILE2_AddrLO

	LDA BOMB_PPU_TILE1_AddrLO
	CLC
	ADC #$20
	STA BOMB_PPU_TILE3_AddrLO
	CLC
	ADC #1
	STA BOMB_PPU_TILE4_AddrLO


	LDA #$20			;This HiByte is set manually to $ 20xx
	STA BOMB_PPU_TILE1_AddrHI
	STA BOMB_PPU_TILE2_AddrHI
	STA BOMB_PPU_TILE3_AddrHI
	STA BOMB_PPU_TILE4_AddrHI
Last edited by sdm on Fri Jul 28, 2017 1:51 am, edited 1 time in total.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Copy SPR X / Y position to similar in NameTable (PPU ad

Post by tokumaru »

You mean you want to convert pixel coordinates into NT addresses? Just look at the bits:

X: XXXXX***
Y: YYYYY***
NT address: 0010NNYY YYYXXXXX

All you have to do is a little bit shifting, ANDing and ORing to get the bits where they need to be.

Code: Select all

  lda #%00001000 ;base value for $2000 (change lower 2 bits for other NTs)
  sta addrhi
  lda ycoord
  and #%11111000
  asl
  rol addrhi
  asl
  rol addrhi
  sta addrlo
  lda xcoord
  lsr
  lsr
  lsr
  ora addrlo
  sta addrlo
sdm
Posts: 412
Joined: Tue Apr 11, 2006 4:08 am
Location: Poland

Re: Copy SPR X / Y position to similar in NameTable (PPU ad

Post by sdm »

Thanks, now works as I wanted. :)

Code: Select all

	lda #%00001000			;base value for $2000 (change lower 2 bits for other NTs)
	sta BOMB_PPU_TILE1_AddrHI
	lda HERO_1_Y
	clc
	adc #7				;"centering"
	and #%11110000			;y bomb in steps of 16pix
	asl A
	rol BOMB_PPU_TILE1_AddrHI
	asl A
	rol BOMB_PPU_TILE1_AddrHI
	sta BOMB_PPU_TILE1_AddrLO
	lda HERO_1_X
	clc
	adc #7				;"centering"
	and #%11110000			;x bomb in steps of 16pix
	lsr A
	lsr A
	lsr A
	ora BOMB_PPU_TILE1_AddrLO
	sta BOMB_PPU_TILE1_AddrLO
	clc
	adc #1
	sta BOMB_PPU_TILE2_AddrLO
	clc
	adc #$1F
	sta BOMB_PPU_TILE3_AddrLO
	clc
	adc #1
	sta BOMB_PPU_TILE4_AddrLO

I would still need a method of calculating the PPU address of the tiles to the X / Y coordinate of the sprite. :( I need this to determine the X / Y co-ordinates of a collision during a bomb explosion. Unfortunately, I was not able to simply copy the X / Y of the SPRITE position at the moment the bomb is placed, which is not set to the SPRITE position accuracy (the bomb is set to 16 pixels "step")

I'm trying to create a logical pattern, but of course I'm not going to take it in the code:

X 00,10,20,30,40,50,60,70,80,90,A0,B0,C0,D0,E0,F0
Y 00,10,20,30,40,50,60,70,80,90,A0,B0,C0,D0,E0 (sbc #1)

2000=X00/Y00
2002=X10/Y00
2004=X20/Y00
2006=X30/Y00
(...)

2000=X00/Y00
2040=X00/Y10 (Y sbc #1)
2080=X00/Y20 (Y sbc #1)
20C0=X00/Y30 (Y sbc #1)
2100=X00/Y40 (Y sbc #1)
(...)

2042=X10/Y10 (Y sbc #1)
(...)
Attachments
ppu2sprxy.png
bomb.nes
(256.02 KiB) Downloaded 190 times
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Copy SPR X / Y position to similar in NameTable (PPU ad

Post by tokumaru »

I'm not sure I understand what you need... Do you want to convert the coordinates back to pixel precision in order to test for collisions against sprites?

Normally, a game engine would not be doing this kind of conversion back and forth, but instead maintaining all objects in "level space", not "sprite space" or "name table space", since sprites and name tables are just details about how your game world is presented to the player, and these details could change significant if for some reason you decide to port the game to another platform. The game's engine should be able to function independently from these details.

The idea is to keep everything in level/world space at all times so that objects can interact with each other and the level map without problems, and do any coordinate conversions one way only, for drawing the objects on screen.
sdm
Posts: 412
Joined: Tue Apr 11, 2006 4:08 am
Location: Poland

Re: Copy SPR X / Y position to similar in NameTable (PPU ad

Post by sdm »

tokumaru wrote:I'm not sure I understand what you need... Do you want to convert the coordinates back to pixel precision.
Yes exactly. PPU address, which is contained in the variables (when the bomb is placed on the screen):

BOMB_PPU_TILE1_AddrHI
BOMB_PPU_TILE1_AddrLO

That is the first (left / top) of the four BGR bomb tiles. Convert this address to match a similar sprite on the screen (exactly as pictured).
For example 21CA addres responds X50, Y70 of sprite position (6F - Considering that the Y position on the screen is not equal).

Generally speaking, a 16x16pix collision field (coordinates corresponding to the sprite position (x, y)) will be inserted on the screen where the
exactly bomb will be placed (16x16pix area)

Maybe it's hard for me to explain it, I hope you understand.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Copy SPR X / Y position to similar in NameTable (PPU ad

Post by tepples »

Instead of storing BOMB_PPU_TILE1_AddrHI and BOMB_PPU_TILE1_AddrLO, they're trying to recommend that you instead store the bomb's X and Y positions BOMB_X and BOMB_Y, and then convert those to BOMB_PPU_TILE1_AddrHI and BOMB_PPU_TILE1_AddrLO when updating the bomb's display.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Copy SPR X / Y position to similar in NameTable (PPU ad

Post by tokumaru »

You could just reverse the formula and account for the dimensions of the explosion, but like I said, I seriously advise against this. Explosions, after all, exist in the game world and interact with game entities, so there's no reason for them not to be game entities themselves.

Having one of your game objects represented exclusively as a set of NT addresses is a very hacky thing to do, because that information doesn't tell you directly where in the world the object is (hence why you need the conversion), only where in the screen it's drawn.

Keeping track of your objects always in level space is not only the professional thing to do, it's also easier to code and less error prone, since everything is unified and you don't have to constantly think of which space each thing lives in and do all sorts of space conversions to have them interact with each other.

Each object will have only one type of conversion, to translate its coordintes from level space to screen space so they can be displayed to the player. These conversions have to take into consideration whether the game objects are rendered using sprites or background, whether there's any scrolling, and whether there's anything shifting the gameplay window, such as status bars. No matter the type of conversion, they only go one way.
sdm
Posts: 412
Joined: Tue Apr 11, 2006 4:08 am
Location: Poland

Re: Copy SPR X / Y position to similar in NameTable (PPU ad

Post by sdm »

Thanks.
Ok I did it, it was even surprisingly simple:

Button A - place bomb
Button B - copy PPU addres to X/Y co-ord (For the test I gave the sprite)

Code: Select all

	lda #%00001000			;base value for $2000 (change lower 2 bits for other NTs)
	sta BOMB_PPU_TILE1_AddrHI
	lda HERO_1_Y
	clc
	adc #7				;"centering"
	and #%11110000			;y bomb in steps of 16pix
	sta BombY_Temp
	asl A
	rol BOMB_PPU_TILE1_AddrHI
	asl A
	rol BOMB_PPU_TILE1_AddrHI
	sta BOMB_PPU_TILE1_AddrLO
	lda HERO_1_X
	clc
	adc #7				;"centering"
	and #%11110000			;x bomb in steps of 16pix
	sta BombX_Temp
	lsr A
	lsr A
	lsr A
	ora BOMB_PPU_TILE1_AddrLO
	sta BOMB_PPU_TILE1_AddrLO
	clc
	adc #1
	sta BOMB_PPU_TILE2_AddrLO
	clc
	adc #$1F
	sta BOMB_PPU_TILE3_AddrLO
	clc
	adc #1
	sta BOMB_PPU_TILE4_AddrLO

	LDA BombY_Temp			;1pix odejmij od Y
	SEC
	SBC #1
	STA BombY_Temp
Attachments
demo.nes
(256.02 KiB) Downloaded 209 times
Last edited by sdm on Mon Nov 06, 2017 10:51 am, edited 1 time in total.
Post Reply