Re: [stella] need 16-bit math for JoustPong?

Subject: Re: [stella] need 16-bit math for JoustPong?
From: "Andrew Davie" <atari2600@xxxxxxxxxxxxx>
Date: Thu, 29 Jan 2004 11:42:05 +1100
Let's do this with just the Y coordinate.  Assume you have one byte Y now,
and you add or subtract 1 to it.
If you add or subtract 1 per frame, then it moves one scanline per frame.
Let's convert that to the exact 16-bit equivalent...

    clc
    lda YCoord            ; low byte of 16-bit Y coordinate (fractional
byte)
    adc #0                    ; yeah, this isn't really doing anything...
but here to show the process
    sta YCoord
    lda YCoord+1
    adc #1                    ; add 1 pixel to integer byte of 16-bit x
coordinate
    sta YCoord+1

OK, that's just added "1 pixel" to a 16-bit coordinate which is conceptually
represented as 8.8 format.  That is, 8 bits of integer and 8 bits of
fractional.  The position of the decimal point is entirely up to us, the
programmer.  When we actually display the object, we use only the integer
portion for its display position.  SO in this case, we always use the
variable "YCoord+1"

Now that example showed moving 1 pixel down.  How would we move 1/10th of a
pixel down?
Well, remember 1 pixel is 1.0  which is represented in our two-byte system
as 1 for the high byte and 0 for the low byte.  So what's 1/10th of this?
Let's use decimal, then convert back.  1 * 256 = 256.  1/10th of this is
25.6, say 26.  So if we use 0 for the high byte and 26 for the low byte
(decimal) then we have roughly 1/10th of a pixel.

    clc
    lda YCoord            ; low byte of 16-bit Y coordinate (fractional
byte)
    adc #26                    ;OK, now this is doing something.  Add 1/10th
of a pixel
    sta YCoord
    lda YCoord+1
    adc #0                    ; caters for carry/overflow from the low
fractional byte
    sta YCoord+1

And that's how fractional movement is done.


Now let's make it general purpose.  Rather than adding actual hardwired
values, let's use another two-byte varable in the same format to represent
the movement speed...

    clc
    lda YCoord            ; low byte of 16-bit Y coordinate (fractional
byte)
    adc YSpeed            ; low byte of 16-bit Y movement speed
    sta YCoord
    lda YCoord+1
    adc YSpeed+1        ; high byte of 16-bit Y movement speed
    sta YCoord+1

So if the YSpeed was, say, 1/10th of a pixel...

    lda #26
    sta YSpeed
    lda #0
    sta YSpeed+1

then our object would work nicely.  To speed it up, we modify the value in
YSpeed.  To slow it down, likewise.  In fact, since we're doing normal
binary two's complement addition here, if the YSpeed value became less than
0, instead of increasing the Y coordinate, we'd end up actually decreasing
it.  And this is the trick to the full 16-bit movement code.  Just remember
you're using signed numbers here, so it's just as easy to have YSpeed hold
minus 1/10th of a pixel as it is to have it hold plus 1/10th of a pixel.

So the final code is...

    clc
    lda YCoord            ; low byte of 16-bit Y coordinate (fractional
byte)
    adc YSpeed            ; low byte of 16-bit Y movement speed
    sta YCoord
    lda YCoord+1
    adc YSpeed+1        ; high byte of 16-bit Y movement speed
    sta YCoord+1

That's it!  Just make sure you have the correct movement speed in YSpeed
(two bytes) positive or negative... and bob's your uncle :)

Cheers
A






----- Original Message ----- 
From: <KirkIsrael@xxxxxxxxxxxxx>
To: <stella@xxxxxxxxxxx>
Sent: Thursday, January 29, 2004 11:07 AM
Subject: [stella] need 16-bit math for JoustPong?


>
> So if people were saying JoustPong could benefit mightily from
> fractional position and/or speed...I guess I'd need 16 bit math
> for all that? For a bit I thought about using a single byte, but
> it probably donesn't have enough precision...is there a guide to
> doing 16 bit addition/subtraction anywhere?  it seems trickier than I
> first guessed it would be...
> -- 
> KirkIsrael@xxxxxxxxxxxxx    http://kisrael.com
>  Indeed, the Russians' predisposition for quiet reflection followed by
>  sudden preventive action explains why they led the field for many
>  years in both chess and ax murders.  --Marshall Brickman, Playboy 4/73
>
>
> --------------------------------------------------------------------------
--------------------
> Archives (includes files) at http://www.biglist.com/lists/stella/archives/
> Unsub & more at http://www.biglist.com/lists/stella/
>
>

----------------------------------------------------------------------------------------------
Archives (includes files) at http://www.biglist.com/lists/stella/archives/
Unsub & more at http://www.biglist.com/lists/stella/


Current Thread