QML smooth movement
-
Hi,
I have a QML object which moves on the screen at constant speed. This is done by adding constant numbers to current x and y coordinates of the QML object 45 times per second.The problem is that the movement is not smooth.
I also tried to use QElapsedTimer to find time that passed from the last position update and using this time I adapted position change. Still no smooth movement.
Obviously, it is possible to have smooth movements in Qt as Qt animations work smoothly on my PC. So, how to make smooth movement (without using Qt animations)?
-
@ambershark Here si a video http://gaminn.sweb.cz/v.mp4
And the code is simple. On QTimer expiration (45 times per second) we update position of the ball - we add constant number to x and y coordinates.
The application consumes 3 - 5 % of CPU, so the CPU is not overloaded.
-
On QTimer expiration (45 times per second) we update position of the ball - we add constant number to x and y coordinates.
Can't work this way as the frame rate is never 100% constant. You need to synchronize with the renderer, and compute the ball's position for that point in time when it actually gets drawn.
-
@Wieland How can I synchronize with the renderer in Qt? The renderer is QML engine. In my application I use QML items (Item, Rectangle, Image, ..).
-
QQuickWindow
emits a number of signals for syncing, choose the one that fits your needs best: -
I've thought about it again. Given the low velocity of the ball, the jumps between the observed positions seem far too extreme to be caused by what I first thought. Now I think the problem must be something else. The Timer you're using, it is a QML Timer or a QTimer in C++?
-
@Wieland Thanks for getting back to me. I tried synchronization using the signals you mentioned and moving the ball depending on the time that passed between the two signal invokes but it doesn't help.
I use QTimer in C++. All computation is done in C++ (the movement is computed by Box2D engine). There are approx. 30 objects on the scene (as you can see, some bricks, ball, paddle). Box2D computation routine takes 20 to 200 us every frame (measured by QElapsedTimer). I don't know if it is acceptable or not...
-
@MartinD said in QML smooth movement:
Box2D computation routine takes 20 to 200 us every frame (measured by QElapsedTimer).
Are you saying that it takes 20 to 200 milliseconds to calculate? If that is the case there is no way you are going to be able to update 45 times per second. I'm assuming "ms" as you put "us" and not sure what you meant there? Could be nanoseconds maybe? I dunno, hence the assumption. Or maybe you meant microseconds which looks like a u in it's symbol representation, in which case ignore my comment. ;)
I don't have any good ideas on what might be going on to offer right now either, but I'll keep thinking about it.
-
@ambershark Hi. I'm used (and I think many other people) to use us for microseconds... The routine takes 20 to 200 microseconds.
-
I made a simple test project - http://gaminn.sweb.cz/MoveTest.zip. The red ball is moved by Qt animation, the black ball is moved by updating its position on afterAnimating signal.
It seems that both balls experience glitches.
I did QML profiling and I can see periodic gaps in rendering (purple "signal"):
Is there anything I can do with this issue. I use Qt 5.7.1 on Win and on Android.
-
@MartinD I wish I was better in QML and could help here, but I just started learning QML a month or so ago. So I just don't have a good answer on something you can try.
I've done Qt for 16 years, but I'm really new to QML. :(
You said your computer isn't running at high CPU so it really shouldn't have those gaps in processing. Is there a correlation between those gaps and something on your PC using a burst of resources? Like anti virus or something else that hogs up the CPU?