I'm trying to implement some really basic physics into my game and I'm coming up with some interesting problems I've never thought of before.
My objects have 16 bit fixed point coordinates; that's a byte of pixel positioning and a byte of subpixel positioning. Simple enough. My objects also have 16 bit fixed point velocities, but those values are signed. This is very simple when it comes to applying the most significant byte of the velocity to the most significant byte of the coordinate:
Coders familiar with 2's compliment notation will see that negative velocities are already being handled.
I've also expanded on this to handle the subpixel velocity:
Simple enough, I'm just adding the low byte of the horizontal velocity onto the subpixel X coordinates, and then adding the carry onto the pixel X coordinates. But this doesn't handle negative cases, so we must expand further:
This works. If the character's velocity is $01.80 (1.5), he will advance 3 pixels to the right every two frames. If the character's velocity is $FF.80(-1.5), he will advance 3 pixels to the left every two frames.
But I don't think there's a way to express a number between 0 and -1 under this system. I can't conceive of any other way to do this, either. Is this an entirely flawed approach? Anyone have any sage advice?
My objects have 16 bit fixed point coordinates; that's a byte of pixel positioning and a byte of subpixel positioning. Simple enough. My objects also have 16 bit fixed point velocities, but those values are signed. This is very simple when it comes to applying the most significant byte of the velocity to the most significant byte of the coordinate:
Code:
lda OBJHIH,x ;load this OBJect's HI byte for Horizontal velocity
clc
adc OBJHIX,x ;add it to this OBJect's HI byte for the X coordinates
sta OBJHIX,x ;store result
clc
adc OBJHIX,x ;add it to this OBJect's HI byte for the X coordinates
sta OBJHIX,x ;store result
Coders familiar with 2's compliment notation will see that negative velocities are already being handled.
I've also expanded on this to handle the subpixel velocity:
Code:
lda OBJHIH,x ;load this OBJect's HI byte for Horizontal velocity
clc
adc OBJHIX,x ;add it to this OBJect's HI byte for the X coordinates
sta OBJHIX,x ;store result
lda OBJLOH,x ;lo byte of horizontal velocity
clc
adc OBJLOX,x
bcc .carryHandled
inc OBJHIX,x
.carryHandled
clc
adc OBJHIX,x ;add it to this OBJect's HI byte for the X coordinates
sta OBJHIX,x ;store result
lda OBJLOH,x ;lo byte of horizontal velocity
clc
adc OBJLOX,x
bcc .carryHandled
inc OBJHIX,x
.carryHandled
Simple enough, I'm just adding the low byte of the horizontal velocity onto the subpixel X coordinates, and then adding the carry onto the pixel X coordinates. But this doesn't handle negative cases, so we must expand further:
Code:
lda OBJHIH,x ;load this OBJect's HI byte for Horizontal velocity
clc
adc OBJHIX,x ;add it to this OBJect's HI byte for the X coordinates
sta OBJHIX,x ;store result
lda OBJHIH,x ;negative flag set if our horizontal velocity is negative
php ;store the negative flag
lda OBJLOX,x
plp ;pull negative flag
bmi .HNeg ;branch on negative
clc
adc OBJLOH,x
bcc .PosCarryHandled
inc OBJHIX,x ;increase the pixel x coordinate if we overflowed subpixels
.PosCarryHandled
jmp .HLoDone
.HNeg
sec
sbc OBJLOH,x
bcc .NegCarryHandled
dec OBJHIX,x ;DEcrease the pixel x coordinate if we UNDERflowed subpixels
.NegCarryHandled
.HLoDone
sta OBJLOX,x
clc
adc OBJHIX,x ;add it to this OBJect's HI byte for the X coordinates
sta OBJHIX,x ;store result
lda OBJHIH,x ;negative flag set if our horizontal velocity is negative
php ;store the negative flag
lda OBJLOX,x
plp ;pull negative flag
bmi .HNeg ;branch on negative
clc
adc OBJLOH,x
bcc .PosCarryHandled
inc OBJHIX,x ;increase the pixel x coordinate if we overflowed subpixels
.PosCarryHandled
jmp .HLoDone
.HNeg
sec
sbc OBJLOH,x
bcc .NegCarryHandled
dec OBJHIX,x ;DEcrease the pixel x coordinate if we UNDERflowed subpixels
.NegCarryHandled
.HLoDone
sta OBJLOX,x
This works. If the character's velocity is $01.80 (1.5), he will advance 3 pixels to the right every two frames. If the character's velocity is $FF.80(-1.5), he will advance 3 pixels to the left every two frames.
But I don't think there's a way to express a number between 0 and -1 under this system. I can't conceive of any other way to do this, either. Is this an entirely flawed approach? Anyone have any sage advice?