problem with inconsistent diagonal movement [SOLVED]

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
problem with inconsistent diagonal movement [SOLVED]
by on (#147677)
Attachment:
spritefield.nes [48.02 KiB]
Downloaded 86 times
I am working on a little sprite field project for learning ASM and I am able to move the sprites diagonally downwards left or right at a 45 degree angle but upwards it is more like 22.5 degrees. I think I am moving my sprites 2 pixels horizontally for every one vertically but ONLY when moving up left or up right. I am a bit confused by this.

I think these are the pertinent bits of code

Code:
;-----------------------------------------
; Sprite direction check subroutines start
spritemovecheck
 
  LDA spritedir

test0
  CMP #0
  BNE test1
  JMP done
test1
  CMP #1
  BNE test2
 JSR up
 JSR left
  JMP done
test2
  CMP #2
  BNE test3
  JSR up
  JSR right
  JMP done
test3
  CMP #3
  BNE test4
  JSR left
  JSR down
  JMP done
test4
  CMP #4
  JSR right
  JSR down

  JMP done

done
  RTS


and


Code:
;------------------------------
; Sprite move subroutines start
up
  LDX #0
  LDY #0
-
  CLC
  LDA spriteram,x
  SBC speed,y
  STA spriteram,x
  STA $0200,x
  INY
  INX
  INX
  INX
  INX
  CPX #0
  BNE -


  LDX #0
  LDY #0
-
  CLC
  LDA spriteram,x
  ADC speed,y
  STA spriteram,x
  STA $0200,x
  INY
  INX
  INX
  INX
  INX
  CPX #0
  BNE -
  RTS

  RTS

down
  LDX #0
  LDY #0
-
  CLC
  LDA spriteram,x
  ADC speed,y
  STA spriteram,x
  STA $0200,x
  INY
  INX
  INX
  INX
  INX
  CPX #0
  BNE -
  RTS

right:
  LDX #0
  LDY #0
-
  CLC
  LDA spriteram+3,x
  ADC speed,y
  STA spriteram+3,x
  STA $0203,x
  INY
  INX
  INX
  INX
  INX
  CPX #0
  BNE -
  RTS

left
  LDX #0
  LDY #0
-
  CLC
  LDA spriteram+3,x
  SBC speed,y
  STA spriteram+3,x
  STA $0203,x
  INY
  INX
  INX
  INX
  INX
  CPX #0
  BNE -
  RTS

; Sprite direction subroutines end
;---------------------------------


and I CAN get more or less 45 degree movement going up left / right if I do for example

Quote:
test1
CMP #1
BNE test2
JSR up
JSR up
JSR up
JSR up
JSR left
JMP done


rather than

Quote:
test1
CMP #1
BNE test2
JSR up
JSR left
JMP done


But that doesnt seem like a great solution. If anyone would be kind enough to take a quick look at it I would be most greatful. Thanks a lot!

I included the pastebin of the full code just in case and a compiled rom

http://pastebin.com/8N3QUJwp
Re: problem with inconsistent diagonal movement
by on (#147678)
The carry is backwards for subtraction.
SEC before SBC.
CLC before ADC.
Re: problem with inconsistent diagonal movement
by on (#147679)
Whoops! I should have caught that. Thank you very much for pointing it out.

in case it is useful for anyone in the future, a pastebin of the working code

http://pastebin.com/nPEvsh43

And the working rom.
Re: problem with inconsistent diagonal movement [SOLVED]
by on (#147681)
lazerbeat wrote:
Whoops! I should have caught that. Thank you very much for pointing it out.

I think we've all run into that problem before. :wink: (I know I have...)

Oh yeah, one way I taught myself what clc and sec go to is thinking about how sec kind of sounds like sbc, so they go together. Clc obviously goes with adc then.