Jump to content
Sign in to follow this  
Heroes & Shadows

[HELP]Movement Not Right

Recommended Posts

I am trying to get my "character" to bounce between the left and right parts of the screen. Here is the code I'm using:

First, the global equates:

LEFTBOUNDRY        equ                 #20
RIGHTBOUNDRY       equ                 #144

and then the actual code:

; pet movement code
   lda PetX                                                                    ; load pet x position into accumulator
   clc                                                                         ; clear carry flag
   adc PetVX                                                                   ; add the pet's velocity constant
   sta PetX                                                                    ; store pet's new x position
   cmp #LEFTBOUNDRY                                                            ; does it intersect the left boundry?
   bcs SkipReversePetDirection                                                 ; if not branch
   cmp #RIGHTBOUNDRY                                                           ; does it intersect the right boundry?
   bcc SkipReversePetDirection                                                 ; if not branch
   lda PetVX                                                                   ; load the pet's x velocity
   eor #$ff                                                                    ; negate it
   adc PetVX                                                                   ; add pet's current velocity
   sta PetVX                                                                   ; store the pet's new velocity
SkipReversePetDirection

Also here is a link to the full source code: https://www.dropbox.com/s/6g6fmpmksvx7dmo/Source_11-14-18.zip?dl=0

 

Thanks for the support so far,

 

Heroes & Shadows

Share this post


Link to post
Share on other sites

Second adc PetVX should be adc #0 since carry is set and you need to add 1?

 

Also, shouldn't the reverse direction only be skipped when PetX is greater than left AND less than right?

  • Like 1

Share this post


Link to post
Share on other sites

Second adc PetVX should be adc #0 since carry is set and you need to add 1?

 

Also, shouldn't the reverse direction only be skipped when PetX is greater than left AND less than right?

Fixed the first issue, same problem. About the second:

Cite: http://6502.org/tutorials/compare_instructions.htm

 

So for left, I use bcs to not reverse the velocity if pet's x position is greater then 20

For right, I use bcc to not reverse the velocity if pet's x position is less then 144

 

x > 20 OR x < 144

 

Regards,

 

Heroes & Shadows

Edited by Heroes & Shadows

Share this post


Link to post
Share on other sites

So for left, I use bcs to not reverse the velocity if pet's x position is greater then 20

For right, I use bcc to not reverse the velocity if pet's x position is less then 144

Still feels wrong to me. If your position is 155 you are greater than 20 and branch past the reversedirection code as well as the second check for the right-boundary condition.

 

In a higher level language I think you've created this:

if(x < 20)
{
  if(x > 144)
  {
    ReverseDirection();
  }
}

But what you really want is this:

if((x < 20) || (x > 144))
  ReverseDirection();

Or with the logic inverted:

if(!(20 < x && x < 144))
  ReverseDirection();
  • Like 1

Share this post


Link to post
Share on other sites

 

Still feels wrong to me. If your position is 155 you are greater than 20 and branch past the reversedirection code as well as the second check for the right-boundary condition.

 

In a higher level language I think you've created this:

if(x < 20)
{
  if(x > 144)
  {
    ReverseDirection();
  }
}

But what you really want is this:

if((x < 20) || (x > 144))
  ReverseDirection();

Or with the logic inverted:

if(!(20 < x && x < 144))
  ReverseDirection();

Ok, this is what I came up with:

; pet movement code
   lda PetX                                                                    ; load pet x position into accumulator
   clc                                                                         ; clear carry flag
   adc PetVX                                                                   ; add the pet's velocity constant
   sta PetX                                                                    ; store pet's new x position
   cmp #LEFTBOUNDRY                                                            ; does it intersect the left boundry?
   bcs SkipReversePetXVelocityRight                                            ; if not branch
   lda PetVX                                                                   ; load the pet's x velocity
   eor #$ff                                                                    ; negate it
   adc #0                                                                      ; add 0
   sta PetVX                                                                   ; store the pet's new velocity
SkipReversePetXVelocityRight
   cmp #RIGHTBOUNDRY                                                           ; does it intersect the right boundry?
   bcc SkipReversePetXVelocityLeft                                             ; if not branch
   lda PetVX                                                                   ; load the pet's x velocity
   eor #$ff                                                                    ; negate it
   adc #0                                                                      ; add 0
   sta PetVX                                                                   ; store the pet's new velocity
SkipReversePetXVelocityLeft

Seems very convoluted and something that seems like it can be very easily optimized but, hey it works thanks.

 

Regards,

 

Heroes & Shadows

Edited by Heroes & Shadows

Share this post


Link to post
Share on other sites

Off the top of my head without testing or optimizing I think something like this would work:

   ; pet movement code
   lda PetX                     
   clc                          
   adc PetVX                    
   sta PetX                     
   cmp #LEFTBOUNDRY             
   bcc ReversePetDirection     ; PetX < 20
   cmp #RIGHTBOUNDRY            
   bcs ReversePetDirection     ; PetX > 144
   jmp SkipReversePetDirection ; 20 < PetX && PetX < 144
ReversePetDirection:
   lda PetVX                    
   eor #$ff                     
   clc                         ; must clear carry because it's set or cleared depending on which boundary violation got us here
   adc #1                    
   sta PetVX        
   ; TODO This would be a good place to toggle the reflection flag if Pet has a directional sprite              				
SkipReversePetDirection

Regarding your solution, I think the first adc #0 should actually be adc #1 since the carry flag would need to be cleared to take that code path. If you find that the pet is speeding up each time it crosses the screen twice, that's why. Personally I'd probably just clc before every adc operation until you have a need to start optimizing. Even then you'll probably find better places to start optimizing (low hanging fruit).

 

Hope that helps.

-Zack

 

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...