## [stella] Delightful horizontal position code...

 Subject: [stella] Delightful horizontal position code... From: "Andrew Davie" Date: Mon, 12 Feb 2001 17:44:38 +1100
```I did a bit of work on my sprite horizontal position code, and am quite
delighted with this contribution... it figures out and positions two sprites
horizontally, based on their pixel position (X) held in PlayerX[2].  I
analysed the differences between dividing by 16 (4 shifts) and dividing by
15 (repeated subtractions) and wrote a CONSTANT-TIME algorithm to do the
adjustment.  That is, rather than multiple subtractions, this code does some
shifting and additions (constant time) to figure the player positions.  It
works, and is quickish - but best of all... constant execution time!

This is the sort of trick code I like doing :)

Enjoy!

----------------------------------------------------
PositionSprites

; Set the horizontal position of the two sprites, based upon their
coordinates

sta HMCLR                       ; clear any previous movement

ldx #1                          ; sprite index
PosSP

lda PlayerX,x
lsr
lsr
lsr
lsr
tay                             ; /16

sta PS_temp

lda PlayerX,x
and #15

clc
cmp #15

tya
tay
sty PS_temp

iny
iny
iny

lda PlayerX,x
and #15
clc
adc PS_temp                     ; calculate the remainder for the
/15

; Use remainder for fine adjustment
eor #15
sbc #7                          ; -8 to +7
asl
asl
asl
asl

sta HMP0,x                      ; fine movement
sta WSYNC

Jiggle  dey
bpl Jiggle

sta RESP0,x

dex
bpl PosSP

sta WSYNC
sta HMOVE

rts

----------------------------------------------------

--
_  _  _| _ _        _| _    * _                               _  ,
(_|| )(_|( (/_\/\/  (_|(_|\/(_(/_                           ,~' L_|\
,-'        \
see my Museum of Soviet Calculators at                  (            \
http://www.taswegian.com/MOSCOW/soviet.html              \    __     /
L,~'  "\__/
@--> v

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

```