problem with inconsistent diagonal movement [SOLVED]

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

problem with inconsistent diagonal movement [SOLVED]

Post by lazerbeat »

spritefield.nes
(48.02 KiB) Downloaded 101 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: Select all

;-----------------------------------------
; 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: Select all

;------------------------------
; 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
test1
CMP #1
BNE test2
JSR up
JSR up
JSR up
JSR up
JSR left
JMP done
rather than
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
Last edited by lazerbeat on Sun May 17, 2015 7:41 pm, edited 1 time in total.
User avatar
rainwarrior
Posts: 8062
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: problem with inconsistent diagonal movement

Post by rainwarrior »

The carry is backwards for subtraction.
SEC before SBC.
CLC before ADC.
lazerbeat
Posts: 64
Joined: Tue Jul 09, 2013 7:13 am

Re: problem with inconsistent diagonal movement

Post by lazerbeat »

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.
Attachments
spritefield.nes
(48.02 KiB) Downloaded 97 times
User avatar
Drew Sebastino
Formerly Espozo
Posts: 3496
Joined: Mon Sep 15, 2014 4:35 pm
Location: Richmond, Virginia

Re: problem with inconsistent diagonal movement [SOLVED]

Post by Drew Sebastino »

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.
Post Reply