Jump to content
IGNORED

Help applying friction


Recommended Posts

Been messing around with 16 bit math and sub-pixel sprite movements and I ran into a problem when trying to apply friction to a given sprites speed.

 

I've attached the entire code below but the relevant section is this:



;-------------------------------
; Friction (broken )
;-------------------------------



ApplyFriction



.xfriction  lda xspeed+1

 bmi .doNegFrict

 lda xspeed

 bmi .doNegFrict

 

.checkPosFrict  lda xspeed+1 ;check if 0

 beq .checkLow

 jmp .doPosFrict

.checkLow	lda xspeed

 beq .yfriction

.doPosFrict;positive speed, subtract friction from speed

 sec

 lda xspeed

 sbc #FrictionLow

 sta xspeed

 lda xspeed+1

 sbc #0

 sta xspeed+1

 

 jmp .yfriction

 

;negative speed, add friction to speed

.doNegFrict	clc

 lda xspeed

 adc #FrictionLow

 sta xspeed

 lda xspeed+1

 adc #0

 sta xspeed+1

 

.yfriction
;Not done yet

 

It would seem it works as I expect when the xspeed is negative (sprite moving left) but when positive xspeed+1 > 0 friction no longer applies.

 

2 thoughts

 

1) this is probably a simple bug, but I cant seem to see it.

2) there is probably a much easier way of applying friction to a given speed than the method I am using above :)

 

Any suggestions?

friction.zip

Link to comment
Share on other sites

:idea: BTW: Instead of two separate routines for adding and subtracting the friction values, you can use a more general routine and add negative values. E.g.


 lda #FrictionLow; #-FrictionLow

 ldy #0          ; #-1

 clc

 adc xspeed 

 sta xspeed 

 tya

 adc xspeed+1 

 sta xspeed+1

And since you probably need more than one 16-bit addition in your code, you can define a subroutine:


 lda #FrictionLow; #-FrictionLow

 ldy #0          ; #-1

 ldx #<xspeed    ; or e.g. <xpos, <yspeed ...

 jsr Add16

 ...

Add16 SUBROUTINE

 clc

 adc $00,x

 sta $00,x

 tya

 adc $01,x

 sta $01,x

 rts

Link to comment
Share on other sites

:idea: BTW: Instead of two separate routines for adding and subtracting the friction values, you can use a more general routine and add negative values. E.g.

 lda #FrictionLow; #-FrictionLow

 ldy #0          ; #-1

 clc

 adc xspeed 

 sta xspeed 

 tya

 adc xspeed+1 

 sta xspeed+1

And since you probably need more than one 16-bit addition in your code, you can define a subroutine:


 lda #FrictionLow; #-FrictionLow

 ldy #0          ; #-1

 ldx #<xspeed    ; or e.g. <xpos, <yspeed ...

 jsr Add16

 ...

Add16 SUBROUTINE

 clc

 adc $00,x

 sta $00,x

 tya

 adc $01,x

 sta $01,x

 rts

 

Thanks thomas! I saw code similar to this in your cave 1k source, but didn't fully understand it at the time, I think because you were applying friction based on relative speed of the object(?). This makes much more sense now :)

Link to comment
Share on other sites

I think because you were applying friction based on relative speed of the object(?)...

Yup, the faster the vertical speed, the more opposite friction (1/8th of the current yspeed). This makes flying the helicopter much easier.

 

The physics are still not very exact, since air-friction isn't linear but quadratic to speed. But it works quite well for the game. :)

Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

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...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...