Smooth Sprite Movement

Started by
1 comment, last by neomaycry 10 years, 9 months ago

I am continuing work on a 'space shooter' game in SFML 2.0, and everything is going well. The question I have pertains to changing the sprite's position using velocity along the 'x' axis (velX) and velocity along the 'y' axis (velY). Assuming each variable is set to a constant value of 5, the sprite will "skip" pixels until it reaches a new destination equal to its current position plus 5 per game cycle.

This creates a rough effect, as the sprite apparently jumps from one position along a diagonal line to the next. Is there a way to have the game loop render all the "missing" frames between the sprite's old position and its new destination? This is mostly a cosmetic concern. The current method looks okay ... I just want to see if there is a "smoother" way of rendering the sprite's position change.

Thanks.

Advertisement

This creates a rough effect, as the sprite apparently jumps from one position along a diagonal line to the next. Is there a way to have the game loop render all the "missing" frames between the sprite's old position and its new destination? This is mostly a cosmetic concern. The current method looks okay ... I just want to see if there is a "smoother" way of rendering the sprite's position change.

Question 1: How frequently are you updating the logic?

Question 2: How frequently are you rendering?

You can't display incremental steps any faster than you are rendering.

But you can update your logic faster to make it seem smoother. However, there are two better ways to do it.

A) You can update using difference in the time from the last frame (often called 'delta time'). If 'delta time' is a float measured in seconds, then you can update as fast as you like, and still keep consistent movement speeds by doing this: "pos += (velocity.x * deltaTime);". Where 'velocity.x' is the amount of movement in one full second.

B) You can use fixed time steps... but then interpolate the position between the separate timesteps during rendering. I've never done that myself (lack of experience), so I can't advise you on that.

(Most of those links are talking about online games - but they all also apply to singleplayer games)

Servant,

Your explanation was superb, and the resources you shared are wonderful. Everything looks beautiful now. Delta time was the answer :).

Thank you!

This topic is closed to new replies.

Advertisement