Galaga_Freak Posted March 31, 2004 Share Posted March 31, 2004 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 Quote Link to comment Share on other sites More sharing options...
Robert M Posted March 31, 2004 Share Posted March 31, 2004 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! Quote Link to comment Share on other sites More sharing options...
Galaga_Freak Posted April 1, 2004 Author Share Posted April 1, 2004 Cool, I knew it would be something simple Thanks a bunch! Quote Link to comment Share on other sites More sharing options...
Thomas Jentzsch Posted April 1, 2004 Share Posted April 1, 2004 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 Quote Link to comment Share on other sites More sharing options...
Galaga_Freak Posted April 1, 2004 Author Share Posted April 1, 2004 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 Quote Link to comment Share on other sites More sharing options...
Thomas Jentzsch Posted April 1, 2004 Share Posted April 1, 2004 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.