Moving sprite by value < 1

This is probably a dumb question but because the code looks solid I have no idea what it could be.

I have 2 sprites. After their y becomes 0 they refuse to move any further (works as long a their y-value is < 0). Changing the VELOCITY to 1 or greater fixes the problem. Why? Why does @bg1.y += 0.5 not work but @bg1.y += 1 does?

Note that bitmap.height and Graphics.height both equal 480.

Sample Code:

Spoiler
class Scrolling_BG_Vertical VELOCITY = 0.5 # BUG: any velocity < 1 will bug and will not move the sprite down after it reaches "bg1.y == 0" or "bg2.y == 0" #VELOCITY = 1 # <<< works #VELOCITY = 0.9 # <<< bugs def initialize(image) @bg1 = Sprite.new @bg1.bitmap = Cache.test(image) @bg1.x = 0 @bg1.y = 0 @bg1.z = 1 @bg2 = Sprite.new @bg2.bitmap = Cache.test(image) @bg2.x = 0 @bg2.y = [email hidden] @bg2.z = 1 end def update @bg1.y += VELOCITY @bg1.y = [email hidden] if @bg1.y == Graphics.height @bg2.y += VELOCITY @bg2.y = [email hidden] if @bg2.y == Graphics.height p 'just making sure that update is being called every frame' endend
Is the float being rounded down somewhere in the Sprite class?

Also:

Code:
p @bg1.y # => [email protected] += 0.5p @bg1.y # => 0.... wut... still 0?
maybe it rounds it down to 0... after all I don't think you can have decimals in tiles and pixels (especially pixels since I think they are the smallest, indivisible unit of measurement in a screen so they are always whole)...
Try converting the value to a float or use ox and oy perhaps?
These are not tiles, just sprites. But if you can not move a sprite in decimals then all sprites in ace would be limited to a minimum velocity of 1. I can't imagine that it would be like that. That would be a serious limitation.

def update @bg1.y = @bg1.y.to_f + VELOCITY @bg1.y = [email hidden] if @bg1.y == @bg1.bitmap.height @bg2.y = @bg2.y.to_f + VELOCITY @bg2.y = [email hidden] if @bg2.y == @bg2.bitmap.height endGives me the same problem. And sadly I do not know how to view the Sprite source code to see if it rounds it down somewhere.

An alternative solution would be to have a single sprite that fills the entire screen and then to blt the image on it twice (I suppose that blt-ing works with floats.. I hope...). But that would probably cost more resources + it doesn't explain why I can't move a sprite by a float/decimal. I would then run into this problem with some other piece of code later on.

The helpfile only gives me this information on the sprite.x:

Code:
xThe sprite's x-coordinate.
I seriously think it's all because the coordinates are pixel coordinates, and pixels don't have decimals... At the very least, I haven't seen any pixel calculation that allowed decimals, not just in RGSS but in other languages too...

You can always opt to simulate this movement via integers though, since velocity is 0.5, it means it should move by 1 pixel after two movements, so what you can do is like save the supposed displacement into another variable, and only move the sprite once the variable is a whole number...
If that is true, could anyone else also confirm that? (and perhaps add it to the helpfile as well to make it more complete)

I never saw a game development environment that limits sprites to whole pixels. Usually when a sprite must be drawn at y = 0.5 it will be drawn at y = 1 but the y value will still be 0.5 so you can still move sprites at slower speeds. Now I have to add another variable that counts to 2 and every time the modules of 2 == 0 the sprite will be moved (to simulate a 0.5 velocity)...

BUT, this also explains why my pathfinding AI bugs...

def self.get_move_dir(start, destination) if (destination - start == Point.empty) # If-statement is needed because normalizing a zero value results in a NaN value return Point.empty else return Point.normalize(destination - start) end end...# move in a straight line to destination coordinatedef move(destination) @location += velocity * get_move_dir(destination, @location) # <<<<<<<<<<<<< bugs. Because it attempts to move the sprite by floats.end...Because these techniques are VERY common... I'd find it very troublesome if the Ace sprites only allow movement by integers. Or I'd have to ovewrite the sprite class with my own 'proper implementation'.
I think those that you have seen simply automate this process of having the extra variable count... I doubt they round up coz that would mean that 0.5 would be 1 pixel movement, which means that after two movements, you already moved by 2 pixels...

sadly, you need to do it (the extra variable count) manually in RGSS...

After all, you cannot really draw something between pixels because the space between pixels doesn't really exist... which is why if you ask me, it's actually more logical not to accept floats on the first place for pixels...

as for other engines, most of those that I used have their own unit of measurement and doesn't explicitly use pixels (the systems auto-convert the measurement to pixels with some kind of conversion, most probably the lowest value that they accept for the measurement is the equivalent of 1 pixel) which is why most of them allows floats for movement...
Sorry I edited my post after your reply. But how do you move a sprite then from location (0,0) to location (10,34) in a straight line if you can not use floats and w/o overhauling the default sprite class?

Why did the RPG Maker devs do this...
It would probably move diagonally, and maybe even in a zigzag manner

like it will only change x after every 3 or 4 y movements for un-normalized or fixed velocity, but the zigzag won't be visible and will look straight mainly because 1 pixel is so small such that a 1-4 pixel difference isn't really visible in high-res screens, especially if the update frequency is high... 

now for normalized velocity, then it will move by 3 or 4 y and 1 x every movement (since the x difference is 10 while the y difference is 34), most of the time... for large pixels and/or slow movement, it will look zigzag, but then again this is 1 unit of location = 1 pixel, and for most screens nowadays and update frequency of like 60 FPS, it won't be visible...

just take the game window for example, it is 500+x400+ pixels, so 1 pixel would be like 500 times smaller than the width of the window... the game window is quite small as it is, so just imagine how small the 1 pixel is...

btw, since I edited my last post too:

as for other engines, most of those that I used have their own unit of measurement and doesn't explicitly use pixels (the systems auto-convert the measurement to pixels with some kind of conversion, most probably the lowest value that they accept for the measurement is the equivalent of 1 pixel) which is why most of them allows floats for movement...
Yeah but now all my algorithms need to keep track of the 'real' x&y coordinates manually and then setting the sprite x&y to the closest integer value. I tried overriding the Sprite class but it's too hard without knowing it's internal code. I keep breaking the entire class. Like, is it @x, or x? And when does it get rounded down and how do I alias setter methods and etc. Too complicated.

But wait a minute, why does the sprite class work with negative y-values? Why does the sprite move with increments of 0.5 pixels while it's y-value is < 0? That shouldn't work either if it rounds down right?
I'm not sure how it works with negatives... it could simply be because it's negative, which shouldn't exist assuming the 0,0 coordinate is in the corner, so I think the engine do some kind of conversion when the coordinate is negative...

and yeah, the hidden classes are quite complicated
It all sounds really weird/buggy to me. Negative coordinates are used to draw stuff off-screen. It's not common but it it used. Therefor, if negative values behave different compared to positive values then I highly suspect a bug in the RPG Maker code. But all developers are Japanese right? So getting an explanation is probably gonna be really hard 😛

@test = Sprite.new @test.bitmap = Bitmap.new(100,100) @test.x = 0 @test.y = 0 @test.z = 0 @test.y += 0.5 @test2 = Sprite.new @test2.bitmap = Bitmap.new(100,100) @test2.x = 0 @test2.y = -1000 @test2.z = 0 @test2.y += 0.5 p @test.y # => 0 p @test2.y # => -999 # EDITOr... maybe it is because negative values are rounded the other way around. So up instead of down? Somehow...
0 + 0.5 = 0.5 (round down) => 0

-1000 + 0.5 = -999.5 (round "down")  => -999 (btw, your post says 999 (positive), is that true?)

*though of course -999.5 to -999 is actually a round "up" since it's negative... but maybe they round the absolute value first, then reapply the negative sign after...

this would also explain why it doesn't work with any other positive value since:

20 + 0.5 = 20.5 (round down) => 20
They are pixel coordinates. They must be integers. Negatives are inconsistent, but that doesn't matter because you're not supposed to be working with floats anyways for those values.


I don't know why you would need fractions of a pixel coordinate, nor do I think it makes sense: a pixel is supposed to be the smallest addressable unit on the screen.


Velocity is a measure of distance over time. If your distance is limited to integers, why not just increase the time? That is, instead of moving 0.5 pixels per frame, you move 1 pixel per 2 frames. It's a vector quantity so there shouldn't be any harm in scaling it.
Yes sorry I meant -999.

I just tried updating it every 2 frames but then it jitters. So just increasing the time is not good enough unless I misunderstood something.

So it indeed seems that the other engines that I have used so far use some pretty smart system underneath it to 'smooth' the movement of sprites that move by float values. RPG Maker does not have such a system.

Yes there is a problem with integer only coordinates (for me at least):

If I have to move diagonally from coordinate (0,0) to let's say coordinate (100,32) then the movement over the y axis must be at least 1. And then the movement over the x axis will be something like 3.something (a float). Either the x or the y movement MUST be a float in order to arrive exactly at (100,32) in a straight line. This line can not be 100% straight because pixels are whole numbers. Yes I understand. But by just rounding it down it will bug all the way.

If my movement vector is like (0.43242,0.14423) for example then my velocity must be at least 8. Otherwise the y-movement will be less than 1. A movement of 8 is REALLY fast. How do I solve this (without jittering)? Or how do other people here code their sprites to move from coordinate a to coordinate b giving that you know the velocity and the coordinates of a and b without skipping a frame?
Updating it every two frames shouldn't have noticeable difference with updating per frame from my experience with other objects... updating per frame actually results to more jittering and lag in my case...

If I have to move diagonally from coordinate (0,0) to let's say coordinate (100,32) then the movement over the y axis must be at least 1. And then the movement over the x axis will be something like 3.something (a float). Either the x or the y movement MUST be a float in order to arrive exactly at (100,32) in a straight line. This line can not be 100% straight because pixels are whole numbers. Yes I understand. But by just rounding it down it will bug all the way.
you move it by 3, then move it by 4 during the last update... basically you use the rounded down value for the initial movements, then adjust during the last one... 

and as long as the coordinates deal with the pixels directly, you cannot really do something to make it work on floats directly unless you're a genius that can split a pixel into smaller parts...

btw, what is this for? AFAIK, you can always do some kind of approximation if this is for things like collision detection, path finding etc... like having a 1 or 2 pixel buffer for those checks... Especially if this sprites are actually quite big, then the approximation becomes more acceptable since you would probably be using adjustments to take into account the size of the sprite itself and such...
The sprites are relatively small.

It's not really that simple anymore now that I know that rpg maker does not support that. I wrote the code below, They now actually move without jittering or anything. In my sample I have them move from (0,0) to (640,480) but they arrive like 200 pixels too high.

@movedir = (X:0.7952175397655094, Y:0.6063242238681302)

What I now must do:

1. Get the desired movement in float and add the fractional overflow from any possible previous frames.

2. increase both the x and y movement to at least 1. But the increases must be proportional to each other. Like if the (start-destination) has a height that is 3x larger than the width then the increase in y must be 3x as large as the x... (making it very fast)

3. store the fractional part of the movement and then remove it from the movement.

4. apply the resulting integer movement

Code:

Spoiler
def initialize...@overflow_x = 0@overflow_y = 0end...def get_fraction(float)  parts = float.to_s.split(".")  return parts.count > 1 ? parts[1].to_s : '0'enddef move movement_x = @velocity * @move_dir.x + @overflow_x movement_y = @velocity * @move_dir.y + @overflow_y #ensure minimal movement of 1 increase = 1 - movement_x y_increase = 1 - movement_y if y_increase > 0 increase = [increase, y_increase].max end # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< bug is probable in the next 2 lines. The increase should be a % and not a fixed value? Pff then I must calculate how much the increase is as a % from x and y and then apply it to each other and etc. movement_x += increase movement_y += increase # move only by whole integers and save the fractions @overflow_x = ('0.' + get_fraction(movement_x)).to_f @overflow_y = ('0.' + get_fraction(movement_y)).to_f movement_x = movement_x.floor movement_y = movement_y.floor @sprite.x += movement_x @sprite.y += movement_y # old code (which should work imo in any modern engine...):#~ @sprite.x += @velocity * @move_dir.x#~ @sprite.y += @velocity * @move_dir.y end
I think I'm just gonna commission this one. Too much math for me.
Try to store sprite coordinate elsewhere

For my example

def updates Graphics.update Input.update $x += 0.1 $sprite.x = $xend$x = 0$sprite = Sprite.new$sprite.bitmap = Cache.character("Actor1")updates while trueCopy it above main and see what happen
Ugh that was all? Haha I'm such an idiot! Theodoric, you are right. Solution:

def move @x += @velocity * @move_dir.x @y += @velocity * @move_dir.y @sprite.x = @x @sprite.y = @y endNo more jittering, no more fast movement, no pages full of inefficient code, it just works. All I had to do was to store the x and y values in their own variables instead of directly into the sprite. And all of a sudden my pathing algorithms/AI works perfectly.
You can look at Game_Picture's update_move method to see how pictures (sprites) are already handled. It uses whole numbers, not decimals.


This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/moving-sprite-by-value-1.19771/