Hello.
How to make an sprite to move slowly?
The minimum speed I can move an sprite is the speed I get when I add 1 to the position of the sprite. This is still too quick. How is this done? Let pass some vblanks an then update? this technique has a name?
Thanks.
How to make an sprite move slowly?
Moderator: Moderators
Re: How to make an sprite move slowly?
Traditionally, you store the position as a fixed-point number, for example "8.8" which means 8 bits to the left of the decimal point and 8 bits to the right.
Mathematically, it looks identical to working with 16-bit numbers for addition and subtraction - you just use the upper byte instead of the lower byte when it comes to displaying it on screen.
So a speed of $00.80 would result in a speed of one-half-pixel per refresh, a speed of $00.40 in one-quarter, and so on.
Mathematically, it looks identical to working with 16-bit numbers for addition and subtraction - you just use the upper byte instead of the lower byte when it comes to displaying it on screen.
So a speed of $00.80 would result in a speed of one-half-pixel per refresh, a speed of $00.40 in one-quarter, and so on.
Re: How to make an sprite move slowly?
Two ways to do this:
Way 1: Fixed-Point Arithmetic
Way 2: Move every X frames
I'd recommend Fixed-point arithmetic because it's very flexible (use any fraction you want) and simple once you understand how it works.
'Fixed point arithmetic' means you have a Fractional part to your number. You treat this fractional part is like a number divided by 256. 128 (0x80) means 128/256 (1/2), 64 (0x80) means 64/256 (1/4), 86/256 is close to 1/3, etc.
You do 16-bit math when you want to add or subtract a fractional number, then discard the low 8 bits when you actually want to display the sprite's position.
Alternative: moving every X frames
Way one: A global frame counter, then using AND
This is really easy. You have a value somewhere that increases every frame.
If you AND that value with 1, then when the result is 0, you have a condition for every other frame.
If you AND that value with 3 then result is zero, you get every 4th frame, AND with 7 then result is zero to get every 8th frame, and so on.
Way two: Use a counter that ticks down, when it reaches 0, you can move it.
Way 1: Fixed-Point Arithmetic
Way 2: Move every X frames
I'd recommend Fixed-point arithmetic because it's very flexible (use any fraction you want) and simple once you understand how it works.
'Fixed point arithmetic' means you have a Fractional part to your number. You treat this fractional part is like a number divided by 256. 128 (0x80) means 128/256 (1/2), 64 (0x80) means 64/256 (1/4), 86/256 is close to 1/3, etc.
You do 16-bit math when you want to add or subtract a fractional number, then discard the low 8 bits when you actually want to display the sprite's position.
Alternative: moving every X frames
Way one: A global frame counter, then using AND
This is really easy. You have a value somewhere that increases every frame.
If you AND that value with 1, then when the result is 0, you have a condition for every other frame.
If you AND that value with 3 then result is zero, you get every 4th frame, AND with 7 then result is zero to get every 8th frame, and so on.
Way two: Use a counter that ticks down, when it reaches 0, you can move it.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
-
yaofan1212
- Posts: 4
- Joined: Sat Dec 04, 2021 9:48 am
Re: How to make an sprite move slowly?
If I understood correctly, do you mean to do some decimal math to pass by cpu cycles?Dwedit wrote: ↑Sat Dec 04, 2021 10:59 am Two ways to do this:
Way 1: Fixed-Point Arithmetic
Way 2: Move every X frames
I'd recommend Fixed-point arithmetic because it's very flexible (use any fraction you want) and simple once you understand how it works.
'Fixed point arithmetic' means you have a Fractional part to your number. You treat this fractional part is like a number divided by 256. 128 (0x80) means 128/256 (1/2), 64 (0x80) means 64/256 (1/4), 86/256 is close to 1/3, etc.
You do 16-bit math when you want to add or subtract a fractional number, then discard the low 8 bits when you actually want to display the sprite's position.
Alternative: moving every X frames
Way one: A global frame counter, then using AND
This is really easy. You have a value somewhere that increases every frame.
If you AND that value with 1, then when the result is 0, you have a condition for every other frame.
If you AND that value with 3 then result is zero, you get every 4th frame, AND with 7 then result is zero to get every 8th frame, and so on.
Way two: Use a counter that ticks down, when it reaches 0, you can move it.
Something like:
accumulator = 0.3
accumulator = accumulator +0.3
after the 4 vblank cycle we should have 1.2 which is trunked to 1?
In this way, after 4 blanks, the sprite moved 1 pixel?
Re: How to make an sprite move slowly?
It's like "decimal" in that there is a fractional number involved, but it's not like decimal in that it's not base 10. It's a fraction divided by 256. So 0.3 would approximate to 77/256, or 0.30078125. (Which is very close to 0.3)
It's a 16-bit addition operation. During this addition, the Integer part of the number is the high byte, and your fractional part is the low byte. You add 77 to the low byte, then add 0 with carry to the high byte.
After adding 77 four times, you get 0x01 in the high byte, and 0x34 (52) in the low byte. Your result is 1 + 52/256, or 1.203125.
It's a 16-bit addition operation. During this addition, the Integer part of the number is the high byte, and your fractional part is the low byte. You add 77 to the low byte, then add 0 with carry to the high byte.
After adding 77 four times, you get 0x01 in the high byte, and 0x34 (52) in the low byte. Your result is 1 + 52/256, or 1.203125.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
-
yaofan1212
- Posts: 4
- Joined: Sat Dec 04, 2021 9:48 am
Re: How to make an sprite move slowly?
Yes I understand thanks. But the idea is that in this way is possible to let pass some CPU cycles till get 1? So, after 3 vblanks one get 1 and then the sprites moves?Dwedit wrote: ↑Sat Dec 04, 2021 2:30 pm It's like "decimal" in that there is a fractional number involved, but it's not like decimal in that it's not base 10. It's a fraction divided by 256. So 0.3 would approximate to 77/256, or 0.30078125. (Which is very close to 0.3)
It's a 16-bit addition operation. During this addition, the Integer part of the number is the high byte, and your fractional part is the low byte. You add 77 to the low byte, then add 0 with carry to the high byte.
After adding 77 four times, you get 0x01 in the high byte, and 0x34 (52) in the low byte. Your result is 1 + 52/256, or 1.203125.
-
yaofan1212
- Posts: 4
- Joined: Sat Dec 04, 2021 9:48 am
Re: How to make an sprite move slowly?
Thanks for your help. I was able to slow the sprite bypassing cycles just with a loop using integers.
What I did was to make to set a memory byte as a global variable ($15). Then, for each NMI cycle I add 1 to this global variable. Then compare this value with a predefined one, like 5. If true, move the sprite 1 pixel.

What I did was to make to set a memory byte as a global variable ($15). Then, for each NMI cycle I add 1 to this global variable. Then compare this value with a predefined one, like 5. If true, move the sprite 1 pixel.
Code: Select all
LDA #00
LDX #00
LDA $15
CMP #5
BNE DONE
LDA $0200
CLC
SBC #$01
STA $0200
ldx #0
stx $15
jmp JJ
DONE : ADC #1
STA $15
JJ: 