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

Author Topic: Animation "flickering" when using float position.  (Read 2445 times)

0 Members and 1 Guest are viewing this topic.

[R]Viper

  • Newbie
  • *
  • Posts: 27
    • View Profile
Animation "flickering" when using float position.
« on: October 06, 2018, 05:58:46 pm »
Hi there, I'm doing a little project and I have a flickering problem.

You can clearly see the problem here (the while line on each side of the sprite) : https://www.youtube.com/watch?v=1Xu1ZlWiODg

My movement  code is very simple.
heroine.Position += this._Direction * time.AsSeconds() * 100;

So my position is "always" a float.

If I cast my position to some int, everything is working fine.

this._Heroine.Position = new Vector2f((int) this._Heroine.Position.X, (int)this._Heroine.Position.Y);

But now, when I hit a wall, the sprite "shake".

And for information, I'm using a really simple spritesheet : https://opengameart.org/sites/default/files/OpenGunnerEnemySoldier_0.png where all sprite is 50*50 pixels.

I'm pretty sure there's a way to correct this flickering problem, but I can't figure it out...

Thank in advance guys!
« Last Edit: October 07, 2018, 04:33:29 pm by [R]Viper »

tomvidm

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Animation "flickering" when using float position.
« Reply #1 on: October 08, 2018, 10:31:25 am »
By looking at the spritesheet, you can see that there are white edges, which coincidentally looks very much like the white lines you consider a problem here. I would suggest measuring the sprite dimension again and consider the offset between the sprites. This is definitely not a bug  :)

Also, the position of your sprite should not change the texture coordinates of the undrlying vertices.

Show us instead the code that handles the animation itself, not the position of the sprite. Right now, I can only base my guess of off the video and the spritesheet.

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Animation "flickering" when using float position.
« Reply #2 on: October 15, 2018, 10:25:51 pm »
This seems to be the result of a very commonly experienced issue. When using non-integer values for positions and sizing/scaling, the resulting texture pixels can be a neighbouring pixel when OpenGL renders each pixel.

There are a few solutions:
The main one is to make sure that each sprite is positioned using integers (remember this can be done separated from the actual float position values you store).
Another is to add a copy of the pixel edge around the texture part that you use. That is, each edge should then be two pixels wide; when specifying the texture rectangle, you would ignore the outer pixel edge.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*