Jump to content

Help applying friction


5 replies to this topic

#1  

    Chopper Commander

  • 179 posts
  • Joined: 17-January 02
  • Location:St. Louis

Posted Wed Mar 31, 2004 4:38 PM

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?

Attached Files



#2  

    Stargunner

  • 1,481 posts
  • Joined: 30-December 02
  • Rootbeer!
  • Location:Western NY state

Posted Wed Mar 31, 2004 5:16 PM


xfriction    lda xspeed+1 

      bmi .doNegFrict 

      lda xspeed 

      bmi .doNegFrict 


if xspeed and xspeed+1 form a 16-bit integer, then your should only test the high byte for the sign of the 16-bit integer not both bytes.

Cheers!

#3  

    Chopper Commander

  • 179 posts
  • Joined: 17-January 02
  • Location:St. Louis

Posted Wed Mar 31, 2004 7:09 PM

Cool, I knew it would be something simple

Thanks a bunch!

#4  

    Thrust, Jammed, SWOOPS!

  • 16,625 posts
  • Joined: 25-April 01
  • Always left from right here!
  • Location:Düsseldorf, Germany

Posted Thu Apr 1, 2004 2:10 AM

: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


#5  

    Chopper Commander

  • 179 posts
  • Joined: 17-January 02
  • Location:St. Louis

Posted Thu Apr 1, 2004 9:10 AM

Thomas Jentzsch said:

: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 :)

#6  

    Thrust, Jammed, SWOOPS!

  • 16,625 posts
  • Joined: 25-April 01
  • Always left from right here!
  • Location:Düsseldorf, Germany

Posted Thu Apr 1, 2004 9:49 AM

Galaga_Freak said:

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. :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users