Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Jittering in Player Sprite and Camera Movement  (Read 3741 times)

0 Members and 1 Guest are viewing this topic.

HiperDoo

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Jittering in Player Sprite and Camera Movement
« on: October 10, 2021, 02:43:10 am »
Hi, I'm starting a little game and I'm having some basic problems with jittering. My project is a bit long, so I simplified everything into a single main.cpp with around 250 lines of code.

https://github.com/HiperDoo/Jittering-Problem

I tryed to make the camera always follow the player and that the camera can move smoothly when the player accelerates and when brakes hard. And it works, the bad thing is that there is a very annoying little jitering (and its not solved only by placing a maximum speed, since you can see the jittering when accelerating and decelerating).

I really tried a lot of convinations with the std::floor(), std::round() and std::fmod() functions. I've been trying to solve it for 3 days.

An example (obviously you can't see the jittering in an image):


I would be REALLY GRATEFUL if someone could modify the code I made to fix this jittering pleace.

#Details:
   - The TileMap and the red box (player's hitbox) are drawn on the same Render Texture and moved with the camera. The player sprite is drawn in the window using "mapCoordsToPixel" from the hitbox (for some reason a jittering occurs, even having the same coordinates).
   - There are 3 factors to take into account, the movement of the camera, the hitbox of the player and the sprite of the player. There is jittering between the camera position and the hitbox, also between the hitbox and the sprite (terrible :().
   - The player's sprite must continue to be drawn outside the tilemap's render texture (I want to make modifications to it without altering the tilemap, in addition to being scaled).

Paul

  • Jr. Member
  • **
  • Posts: 78
    • View Profile
Re: Jittering in Player Sprite and Camera Movement
« Reply #1 on: October 10, 2021, 11:42:33 pm »
Hi, it's not entirely easy to achieve 100% smooth movement on todays computers, you can check articles like https://gafferongames.com/post/fix_your_timestep/. Each step gives better result. There is also Kairos library which implements it via SFML - https://github.com/Hapaxia/Kairos/.

About your code, I guess it can't work if dt lacks precision and you have locked framerate:

Code: [Select]
dt:
0.015995
0.016521
0.016429
0.015952
0.017041

It should be constant: dt = 1 / 60.f

Calculation formulas seems overcomplicated. Is hard to say what is going on, whereas it's just player character movement, camera tied somehow to player and camera lag. Same for the rest, forget about rendertexture, keep it simple first. Do not add a camera until the character's movement isn't smooth. It's better to use sf::Vector2f everywhere for coordinates instead of variables like fix_minPosX and fix_minPosY.

If you implement approach mentioned in article + vsync on + clearer and unified code, it will be way better and you can fix minor issues.