Page 1 of 1
sprite point2point loop move - NO Temp RAM using
Posted: Sat Nov 07, 2015 4:20 pm
by sdm
I have a problem with taking a loop of a moving object. Loop from A (90) to B (C0) and back around (A-B,B-A ~). I can do this, but I have to use temporary RAM bytes, and I would do it completely without using the memory Temp bytes.
Code: Select all
sprloop:
LDA SPR1X
CLC
ADC #$01
CMP #$C0
BEQ sprloop_end
STA SPR1X
rts
sprloop_end:
LDA SPR1X
CLC
SBC #$01
CMP #$90
BEQ sprloop_end2
STA SPR1X
sprloop_end2:
RTS
Re: sprite point2point loop move - NO Temp RAM using
Posted: Sat Nov 07, 2015 5:54 pm
by dougeff
Why not use a non-temporary RAM byte?
(Although, aren't all RAM bytes "temporary", technically)
Re: sprite point2point loop move - NO Temp RAM using
Posted: Sat Nov 07, 2015 6:43 pm
by tokumaru
You have to use a variable to remember in which direction the object is moving. What you're doing now will always move the object right first, and that will undo any movement that might have happened last time. The only "hack" I can think of to avoid using any RAM for states is if you move right in 1-pixel increments and move left in 2-pixel decrements, so you'll only cancel half of the movement and the object will be able to move left. It will look pretty jittery though. Proper object A.I. needs a state variable.
Re: sprite point2point loop move - NO Temp RAM using
Posted: Sun Nov 08, 2015 2:18 am
by sdm
I did this by using unused bits (bit2) sprite attribute table. Interested in other methods to loop. But I'm curious about other ways of getting the same result.
method1:
Code: Select all
sprloop:
LDA SPR1S
CMP #%00000110 ;one of four unused SPR ATTRIBUTE bytes
BEQ sprloop_end
LDA SPR1X
CMP #$C0
BEQ sprloop_end
CLC
ADC #$01
STA SPR1X
RTS
sprloop_end:
LDA #%00000110
STA SPR1S
LDA SPR1X
CMP #$90
BEQ sprloop_end2
SEC
SBC #$01
STA SPR1X
RTS
sprloop_end2:
LDA #%00000010
STA SPR1S
RTS
method2:
Code: Select all
spr1loop:
LDA SPR1S
CMP #%00000110 ;one of four unused SPR ATTRIBUTE bytes
BEQ spr1loop_end
LDA SPR1X
CMP #$C0
BEQ spr1loop_end
CLC
ADC #$01
STA SPR1X
RTS
spr1loop_end:
LDA SPR1S
ora #%00000110
STA SPR1S
LDA SPR1X
CMP #$90
BEQ spr1loop_end2
SEC
SBC #$01
STA SPR1X
RTS
spr1loop_end2:
LDA SPR1S
and #%00000010
STA SPR1S
RTS
Re: sprite point2point loop move - NO Temp RAM using
Posted: Sun Nov 08, 2015 5:08 am
by tokumaru
That's the basic idea, but I have a few suggestions:
- Add labels that actually describe what it does, such as "MoveLeft" and "MoveRight".
- Don't immediately move in the opposite direction as soon as the destination is reached. That not only wastes CPU time by moving the object twice in the same frame but the object ends up never being displayed in the last position. Instead, just change the state when the target is reached, so that *next time* it will move in the opposite direction.
- Please consider using some RAM to keep track of object states. There isn't much you can do with them if you don't.
- This if quite a big change, so you may not want to do it right away, but please don't hardcode objects to OAM positions. That prevents you from doing proper sprite cycling and makes it harder to dynamically load and unload objects. The more versatile thing to do is have objects "live" in RAM, and every frame you generate sprites to represent them (using a function that draws meta-sprites).
Re: sprite point2point loop move - NO Temp RAM using
Posted: Sat Nov 28, 2015 4:09 am
by Prime
Hi sdm here is a function for metatiles it comes from road fighter
Code: Select all
///////////////////////////////////////////////////////////////////////////////
//Update Object Vertical Position,ObjectTile,Attributes,Horizontal Position
///////////////////////////////////////////////////////////////////////////////
//_TmpObjectMetaTileCounter = $9b
//_TmpObjectYposition = $9c
//_TmpObjectXposition = $9d
//_TmpXIndex = $ec
///////////////////////////////////////////////////////////////////////////////
Do_HandleUpdateObjectsCont:
and #$1F
asl
tay
lda ObjectDataStructureTabL,y
sta ObjectDataStructurePtr
lda ObjectDataStructureTabH,y
sta ObjectDataStructurePtr+1
ldy #0
lda (ObjectDataStructurePtr),y
sta _TmpObjectMetaTileCounter
lda #1
jsr Do_HandleAdjustObjectDataStructurePtrs
-:
ldx _TmpXIndex
ldy #0
lda (ObjectDataStructurePtr),y
clc
adc _TmpObjectYposition
sta $0200,x
inx
iny
lda (ObjectDataStructurePtr),y
sta $0200,x
inx
iny
lda (ObjectDataStructurePtr),y
sta $0200,x
inx
iny
lda (ObjectDataStructurePtr),Y
clc
adc _TmpObjectXposition
sta $0200,x
lda #4
jsr Do_HandleAdjustObjectDataStructurePtrs
inx
stx _TmpXIndex
dec _TmpObjectMetaTileCounter
bne -
rts
///////////////////////////////////////////////////////////////////////////////
Do_HandleAdjustObjectDataStructurePtrs:
clc
adc ObjectDataStructurePtr
sta ObjectDataStructurePtr
lda #$00
adc ObjectDataStructurePtr+1
sta ObjectDataStructurePtr+1
rts
///////////////////////////////////////////////////////////////////////////////
ObjectDataStructureTabL:
.byte <YellowCarStructure
ObjectDataStructureTabH:
.byte >YellowCarStructure
YellowCarStructure:
.byte 4 //Number of 8x8 sprites that creates the metatile used as counter
//Tile 0
.byte $00 //Vertical
.byte $6A //Tile
.byte $03 //Attrib
.byte $00 //Horizontal
//Tile 1
.byte $08 //Vertical
.byte $7A //Tile
.byte $03 //Attrib
.byte $00 //Horizontal
//Tile 2
.byte $00 //Vertical
.byte $6B //Tile
.byte $03 //Attrib
.byte $08 //Horizontal
//Tile 3
.byte $08 //Vertical
.byte $7B //Tile
.byte $03 //Attrib
.byte $08 //Horizontal