Updating sprite positions from a look up table.

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
lazerbeat
Posts: 64
Joined: Tue Jul 09, 2013 7:13 am

Updating sprite positions from a look up table.

Post by lazerbeat »

I have a really simple sprite I am trying to move around in a kind of triangle wave shape. so it is constantly moving left but alternates between moving up and down.

This is the code I have

Code: Select all

movesprites:

  LDA $0203       ; load sprite X position
  SEC             ; make sure the carry flag is clear
  SBC #$01        ; A = A - 1
  STA $0203       ; save sprite X position

  LDA $0200       ; load sprite Y position
  CLC             ; make sure the carry flag is clear
  ADC vertpos     ; A = A plus vertposvalue
  STA $0200       ; save sprite Y position

rts

vertpos:
.db $01,$01,$01,$01,$01,$01,$01,$01,$FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF

so I am updating the X position every frame which is great. the code looks up the first number in the vertpos look up table but I can't work out how to make the code load subsequent numbers from the vertpos table so my sprite just moves diagonally down. I tried this

Code: Select all

  LDA $0200       ; load sprite Y position
  CLC             ; make sure the carry flag is clear
  ldx #01
  ADC vertpos, x  ; A = A plus vertposvalue
  STA $0200       ; save sprite Y position
  inx 
Which I thought would increment the x counter and move to the next number in the table each time the subroutine was called but no luck. Does anyone have any ideas?

Thanks guys!
Attachments
stars.nes
(48.02 KiB) Downloaded 68 times
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Updating sprite positions from a look up table.

Post by tokumaru »

You need to use a variable to keep track of what vertpos you're supposed to use. If you simply do ldx #01 like you're doing now, you'll always use the second value.

What you have to do is initialize a variable to 0 (i.e. the first index) before the game loop, and in the game loop you use that variable and increment it every frame:

Code: Select all

LDA $0200
CLC
LDX currentvertpos
ADC vertpos, x
STA $0200
INC currentvertpos
It might also be a good idea to look for the end of the table, so toy can change the index back to 0 when it's reached.

Always keep in mind that a game is updated once every frame. Each frame, everything about the game world has to be updated one step. You need variables to keep track of what's happening in the game world, so you can resume updating it the next frame. In this case, the position in the table of vertical positions has to be preserved and restored every frame, so you can successfully change it over time.
lazerbeat
Posts: 64
Joined: Tue Jul 09, 2013 7:13 am

Re: Updating sprite positions from a look up table.

Post by lazerbeat »

Thank you sir, just to clarify one frame is one complete cycle through my code right? (excluding setting up the variables / the header etc)
It might also be a good idea to look for the end of the table, so toy can change the index back to 0 when it's reached.
Ok so set something like this pseudo code?


CMP currentvirtpos to 16
<16 continue / >16 virtpos = 0

edit - silly code
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Updating sprite positions from a look up table.

Post by tokumaru »

lazerbeat wrote:Thank you sir, just to clarify one frame is one complete cycle through my code right? (excluding setting up the variables / the header etc)
I haven't seen your code, but programs are usually separated into initialization and a main loop (performing the initialization every frame would be VERY wrong). The initialization runs only once, the main loop runs every frame.

The variable being set to 0 would go in the initialization part, and it would be used and incremented in the part of the main loop that updates the sprites.

Have you made your program using a tutorial? Does this tutorial by any chance put the whole game logic inside the NMI? If so, the NMI is what runs every frame, analogous to the main loop.
lazerbeat
Posts: 64
Joined: Tue Jul 09, 2013 7:13 am

Re: Updating sprite positions from a look up table.

Post by lazerbeat »

Yes and yes! Almost have it working, just need to smooth out my look up table a bit!

Thanks a lot,
Post Reply