Moving sprite is jitter.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
ddabrahim
Party member
Posts: 182
Joined: Mon May 17, 2021 8:05 pm
Contact:

Moving sprite is jitter.

Post by ddabrahim »

Hi everyone!

I would like to ask for some advice with moving sprites.

Currently I move sprites right to left in my game by simply changing their position like so

sprite.x = sprtie.x - speed * delta

There is no camera and physics but I draw the sprites on to a canvas in love.update and then draw the canvas using love.graphics.draw(canvas) in love.draw.

However, I do experience that sprites sometimes jitter.
This is what it looks like, it is a video, if the playback is not smooth, try downloading the video and play it locally in a video player
Watch the strawberry, the jitter I am talking about happens around the middle, there is no jitter before and after just once around the middle, again if playback is not smooth for you, try downloading the video:

https://gofile.io/d/dMCTGj

This jitter happens like every 2-3 seconds. I did search the forum and tried everything people was recommending:

1 - VSync on/off
2 - AverageDelta instead of Delta based on the time elapsed between two frames
3 - Fixed delta 1/Monitor refresh rate + VSync on
4 - Round position sprite.x = sprite.x - math.floor(speed * delta)

Nothing is working. Average delta is a bit smoother but the speed is not consistent, sometime faster, sometime slower.

There is one thing I haven't tried that someone mentioned to solve this problem and that is "Sub pixel rendering". No idea what is that mean. How can you do that?

I would appreciate any help or advice.

Thank you.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Moving sprite is jitter.

Post by ReFreezed »

Measure the actual time updates are taking to see if any update takes too long. The third solution you listed should've fixed any uneven jitter that wasn't caused by too high workload. Also, please share your code.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
ddabrahim
Party member
Posts: 182
Joined: Mon May 17, 2021 8:05 pm
Contact:

Re: Moving sprite is jitter.

Post by ddabrahim »

Measure the actual time updates are taking to see if any update takes too long.
I'm not sure how to do that. I tried to do at the beginning of love.update
updateCounter = love.timer.getTime()

And then I did this at the end

updateCounter = updateCounter - love.timer.getTime()

Not sure if I did it right but according to this, it takes 1-6 ms for love.update to finish. The moment the first sprite show up, it is drop to 1 ms and then it is around 2 ms but then it is all over the place in range 1-6.

No idea if I did it right.
Also, please share your code.
I managed to reproduce it in a minimal example; Just select a sprite and lock on with your eyes and follow. Sometime it is jitter just a little bit, barely noticeable but sometimes it is a big jump. It is hard for me to tell if related to update or not, but it seems like the time it takes for update to complete is drop below 1 ms the moment it jitters. But hard to tell and even then I have no idea how to fix that. Maybe there is a better way to measure it.

I appreciate any help.

Here is the example:
jitter-test.zip
(61.34 KiB) Downloaded 46 times
Thank you.
User avatar
keharriso
Citizen
Posts: 92
Joined: Fri Nov 16, 2012 9:34 pm

Re: Moving sprite is jitter.

Post by keharriso »

For what it's worth, I don't see any jittering. Very smooth.
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
User avatar
ddabrahim
Party member
Posts: 182
Joined: Mon May 17, 2021 8:05 pm
Contact:

Re: Moving sprite is jitter.

Post by ddabrahim »

Thank you for trying. Maybe it is just my screen or hardware. I'll try it on other hardware too later.
But even then if its hardware related, it would be nice to find a way to make sure it runs smooth on all hardware.

It can take few seconds before you notice any jitter and even then it can be very small that hard to notice. Sometime it is a big jump.
That's why I made the window width 1400 so you have more time to follow the sprite.

One characteristic is that when 1 sprite jitter, all sprites jitter the same. So it is something that effect all sprites or the entire draw, update cycle.

Thanks.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Moving sprite is jitter.

Post by ReFreezed »

ddabrahim wrote: Sat Sep 17, 2022 5:16 pm updateCounter = love.timer.getTime()

And then I did this at the end

updateCounter = updateCounter - love.timer.getTime()

Not sure if I did it right but according to this, it takes 1-6 ms for love.update to finish. The moment the first sprite show up, it is drop to 1 ms and then it is around 2 ms but then it is all over the place in range 1-6.
Yeah, something like that is what I meant. How long it takes seem very high though. It takes close to 0.001 ms (1 µs) with rare spikes up to 0.12 ms for me. Formatting the time better in love.draw() makes it clearer:

Code: Select all

love.graphics.print(string.format("%.3f ms", updateCounter*1000))
Anyway, the example does jitter for me very rarely. Putting this at the top of love.update() solves the issue:

Code: Select all

local _,_, flags = love.window.getMode()
dt = 1 / flags.refreshrate
Note that the code assumes there's no lag, and that LÖVE can determine the refresh rate.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Moving sprite is jitter.

Post by pgimeno »

Can you try to add this to your repro?

Code: Select all

local buf = {}
local maxlen = 100

function love.quit()
  print(table.concat(buf, "\n"))
end
And this to the beginning of love.udpate:

Code: Select all

  if #buf >= maxlen then
    table.remove(buf, 1)
  end
  buf[#buf + 1] = dt
Then when you see a jitter, quit immediately and check the output. See if there's any outstanding dt.

Also, this is unrelated to your issue, but the candies.lua file has a bug, namely that you set the metatable to {__index = self} where self is not defined. I think you meant {__index = candy} instead. Which, by the way, is better placed out of the function, so that you only have one metatable for all objects instead of a distinct metatable for each. And similarly, you don't need to have a distinct draw function for every object; it can be placed out of the constructor and assigned inside, so that all objects share the same function.
User avatar
ddabrahim
Party member
Posts: 182
Joined: Mon May 17, 2021 8:05 pm
Contact:

Re: Moving sprite is jitter.

Post by ddabrahim »

ReFreezed wrote:It takes close to 0.001 ms (1 µs) with rare spikes up to 0.12
Yes with the formatting you suggested it is show me 0.001 too but hard to tell.
ReFreezed wrote:Putting this at the top of love.update() solves the issue:
Unfortunately it did not solve the problem for me :( I begin to think it is hardware related.
pgimeno wrote:Can you try to add this to your repro?
It is printed steady 0.16-0.17 ms occasionally drop as low as 0.001 the moment the jitter happens.
I guess it is confirm that the moment the jitter happens, it takes longer for the update cycle to complete 0.016 instead of 0.001. But why is that and is there anything I can do about it?
Wondering if my Antivirus could interfere or something else. I know for the fact my AV do slow down the launch of applications sometime and this is why for example if I put things into love.load, it is being executed too late sometime. So I had to move everything out of love.load to the top of my code.
pgimeno wrote:you set the metatable to {__index = self} where self is not defined.
Thanks a lot for mentioning, didn't notice. I was not thinking just copy from my notes how to do OOP :D Fixed it.
I am still having a battle with OOP in Lua, but I begin to understand now what meta tables and meta methods are :)
pgimeno wrote:better placed out of the function, so that you only have one metatable for all objects instead of a distinct metatable for each. And similarly, you don't need to have a distinct draw function for every object; it can be placed out of the constructor and assigned inside, so that all objects share the same function.
Thank you for the suggestion. I am actually trying to avoid inheritance and DRY practice intentionally this time because this is where usually spaghetti code begins which always doing me headache to debug after. I have started my project from scratch because of this. I know it is not practical but my brain is simply not capable to see through inherited code implemented at multiple levels across multiple files. But once I get more comfortable with OOP in Lua I'll consider it.

Thanks a lot guys.

So in case it is remain a mystery why is it happening to me, does anyone know about "Sub pixel rendering"? Someone on the forum has mentioned it is solved jitter but I have no idea how to even begin. I mean love.draw render everything, how can I even manipulate that?
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Moving sprite is jitter.

Post by ReFreezed »

dt sometimes being that low is really strange. Have you updated your graphics drivers to the latest version?

Subpixel rendering is just a thing you can do to make movement smoother when you have low resolution (pixel art) graphics. It's not relevant here.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
ddabrahim
Party member
Posts: 182
Joined: Mon May 17, 2021 8:05 pm
Contact:

Re: Moving sprite is jitter.

Post by ddabrahim »

I am on a Mac, unfortunately I'm getting all updates through the Update Center but I guess with the new M1 chips and the transition period official end this year, It is no longer the priority of Apple to provide any driver updates for AMD, Intel and Nvidia chips.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 47 guests