SFML community forums

General => General discussions => Topic started by: sontung on July 27, 2017, 03:05:31 pm

Title: have fast moving sprites without distortion?
Post by: sontung on July 27, 2017, 03:05:31 pm
Hello everyone.

I am new with SFML and I'm having problems moving fast sprites across the screen.

For some reason when I try to accelerate the sprite it begin to truncate and flicking. If I reduce the speed, everything goes smoothly.

Another strange effect that I'm getting is that during the course of the sprite it seems to do some "bumps" by changing the speed at regular intervals.

To move the sprite I'm using:
position x + = speed * elapsed time

I need to learn about best practices and how to have extremely fast moving sprites around the screen without distortion.

Does anyone have any tips or suggestions?

Thanks!
Title: Re: have fast moving sprites without distortion?
Post by: eXpl0it3r on July 27, 2017, 03:12:57 pm
Just to get an estimate, what's "extremely fast"?

To due away with most "artifacts" you have to understand that your screen has only a fixed amount of pixels, so whenever you use a floating point position, OpenGL will have to estimate how to map the texture pixel to the screen pixel. Which can lead to issues where not the expected pixel gets used due to rounding up or down one pixel.
So to control the proper positioning, you can do the rounding yourself and match the pixels perfectly.

As for smooth movement you need to know that the frame time is not a fixed value. It varies slightly for every frame. As such the elapsed time isn't constant and thus your movement won't be constant.
For a "proper" physics calculation you should Fix Your Timestep (https://gafferongames.com/post/fix_your_timestep/).
Title: Re: have fast moving sprites without distortion?
Post by: Ricky on July 27, 2017, 09:51:26 pm
Can I toot my own horn for a second?

https://ricanteja.github.io/tutorials/2017/07/07/sfgd_1.html

I had the same problem as you and it took me a while to figure out what was going on. You may be using some sort of forced frame limit but I'd recommend against it. Feel free to check out my article on it and give feed back. Hope it helps.
Title: Re: have fast moving sprites without distortion?
Post by: JayhawkZombie on July 27, 2017, 10:12:05 pm
If I may also chime in.

Try using interpolation when rendering.  If you have a fast moving object, it can appear to "jump" between positions, especially when limiting the framerate.

The rendering will not be entirely accurate (at most one frame behind), but the movement will appear much smoother.

Here's a good example of an implementation (but by no means the only one): http://kirbysayshi.com/2013/09/24/interpolated-physics-rendering.html (http://kirbysayshi.com/2013/09/24/interpolated-physics-rendering.html)