Page 1 of 1

Mouse lags on low (medium) fps

Posted: Fri Dec 01, 2017 5:07 pm
by Sasha264
Hello everyone! Have a nice day (:

I have some kind of problem here. Noticed that my program cursor in love game lags behind system cursor.
I found 2 topics about this
https://love2d.org/forums/viewtopic.php?f=4&t=2780
https://love2d.org/forums/viewtopic.php?t=10048
But they don't help me. I know abount vsync and what is it for. I know that this problem disappears at high fps (around 200-400). I tried to move love.mouse.getPosition() operator around my code. In the love.update() in the love.draw() at the begin at the end. No matter.

The problem is: on low fps (about 30 and lower) lag is very noticeable.
I made some experiments with vsync and with heavy drawings, both reduces fps and showing that problem.
Here is video file in attachment that I record with OBS (recording is not slowing game frame rate).
White cursor is system. Black is love-program, displaying by this code:

Code: Select all

function love.load()
	img = love.graphics.newImage("mouse-black.png")
	...
end

function love.update(dt)
	...
end

function love.draw()
	... heavy drawings here
	local x, y = love.mouse.getPosition()
	love.graphics.draw(img, x, y, 0, 1, 1)
end

In the first part of video fps is high (300) and lag isn't noticeable.
In the second part I enabled vsync and fps became common 60. Lag is noticeable but not so important for many games.
In the third part I enabled invisible heavy drawings that leds to fps about 30. And here is the problem! I scrolled the video-record frame by frame and count that between real cursor start moving and love cursor follow it passes about 4 game frames (or 8 video frames at 60 fps capture rate on record). So the problem is not only in low game performance because 4 frames is not 1 frame.

Looks like some guy "smooths" mouse movement. Who is he? :roll:

Re: Mouse lags on low (medium) fps

Posted: Fri Dec 01, 2017 5:57 pm
by RagingDave
Have a look at love.mouse.setCursor and the other cursor related functions. My guess is that they use some OS mouse cursor magic and will be smoother (just a guess but give it a try).

Re: Mouse lags on low (medium) fps

Posted: Fri Dec 01, 2017 6:29 pm
by Sasha264
RagingDave, thanks for the reply!
Some refinement: I don't want to customise cursor for my game now. I want to have actual cursor coordinates for other purposes like game bg scrolling, moving objects etc. For just customising cursor appearance love.mouse.setCursor works perfect, yes! But it is not my target.

Re: Mouse lags on low (medium) fps

Posted: Fri Dec 01, 2017 6:55 pm
by RagingDave
Ah so you meant some kind of input lag. However I just realized that you use love.mouse.getPosition() in the love.draw() function but you should use it in love.update().

Now I am a bit rusty in love2d but usually the update function should be called at a minimum interval even if draw is slow.. (i think).

Re: Mouse lags on low (medium) fps

Posted: Fri Dec 01, 2017 7:07 pm
by Sasha264
Yes, I tried getting coordinates in love.update(). With the same result :o:

Re: Mouse lags on low (medium) fps

Posted: Fri Dec 01, 2017 7:50 pm
by RagingDave
Ok I have some last thoughts, then I'm out of knowledge here :)

1) I wouldn't bet on the whole recording and comparing cursors.. maybe OBS has it's own lag..
2) I read some things about love again and it seems my last comment about update wasn't correct. I still advise you to put your input into update but for logic reasons. However when your draw slows down (low fps, drawing much) your update will too because they both run sequentially after each other.. this means you cannot avoid the "lag" you experience. You should focus on optimizing the drawing (don't draw stuff that is not visible etc.) and target a good frame rate .. usually 60fps should really be good enough.

Maybe someone else has better advice or more insights here.

Re: Mouse lags on low (medium) fps

Posted: Fri Dec 01, 2017 8:16 pm
by zorg
Disable vsync and have code similar to:

Code: Select all

local time
function love.update(dt)
time = time + dt
    if time >= inverseOfFramerate then
        yourUpdate(inverseOfFramerate)-- call fixed timestep update
        time = time - inverseOfFramerate
    end
end
This may solve it only because while all logic will be calculated only 30/60/whatever times per second, the system events will be processed as fast as they can.

You can also define love.run yourself, and edit the love.timer.sleep call at the end to finetune how much time löve hands over processing to the system; the default of 0.001 is fine, though my own PC showed that for me, 0.002 was more optimal.

Re: Mouse lags on low (medium) fps

Posted: Sat Dec 16, 2017 12:41 pm
by Sasha264
@RagingDave,
OBS perfectly shows effect that I see with my own eyes without any OBS launced, so it is not core problem.
About logic reasons to put update logic into update function... yes, I promise than I will do it in every not-test project :cool:

@zorg,
Thank you for interesting suggestion. I spent weeks with testing it in different situations. I will share some results a bit later =)