Weird flickering while moving

Started by
6 comments, last by Zedrico 4 years, 8 months ago

   So, recently I've found myself in this problem that makes every Entity (that is not centered or doesn't have the same speed as the centered Entity) make this strange "flickering" while updates it's position, take a look: (I didn't put any reference object, but the Entities are actually moving to the right --->)

Tervonnia 08_08_2019 19_32_17.mp4

   I think it's being caused by the update logic:

                    void Entity::update(Uint32 ticks)

                    {
                       if (ticks > lastUpdate + 2000 - 1974 - myVelocity)
                        {
                            posX += velocityX;
                            posY += velocityY;
                            lastUpdate = ticks;
                        }
                    }

   And I know that this logic is terrible and will fail, because it even if the update logic is not the root of the problem I need to change it to something better. My questions are, where is my error? How can I find my error? Can I find a better logic to make the movement more smooth? what do I need to search for to implement a new logic?

Forgot to mention but, the skeleton's velocity is 2 Pixels per second and the frog's velocity is 1 Pixel per second.

Advertisement
1 hour ago, Zedrico said:

                       if (ticks > lastUpdate + 2000 - 1974 - myVelocity)

There are several issues with this line. The magic numbers '2000' and '1974' make no sense. Why would you not reduce this to 26? It makes no sense to subtract 'myVelocity' from a 'tick' value.

Usually this kind of update statement would be something like:

if(currentTick > (lastUpdate + updateInterval)

Where currentTick is probably obtained by millisecs() or the like.

Thank you for your reply, I made some changes:

void Entity::update(Uint32 ticks)
{
    if (ticks > lastUpdate + 26 - myVelocity)
    {
        posX += velocityX;
        posY += velocityY;
        lastUpdate = ticks;
    }
}

The magic number '26' is just to make 'myVelocity' a crescent value, if 'myVelocity' is 1 its going to wait 25miliseconds before moving again, and so on.

Ticks is actually the 'currentTick' and is obtained by SDL_GetTicks().

Minor point, but to me 'flickering' means more like appearing and disappearing or changing in brightness or color. I just mention it as it seems like the terminology could be confusing (I was confused at first). I'd probably call the behavior under discussion something like jitter. Maybe my understanding of those terms is idiosyncratic to me though, in which case never mind :)

Quote

The magic number '26' is just to make 'myVelocity' a crescent value...

I'll happily admit my ignorance here: what does 'crescent value' mean? I Googled it and found one hit that was also in reference to time values, but I couldn't find an explanation of the term. (Anyone is welcome to answer that question of course.)

It looks like the rendering is pixel-perfect, that is, no filtering or 'in-between' pixel values. Is that the case?

9 hours ago, Zedrico said:

No problem :), its a value that increments a positive number, 1, 2, 3, 4, 5. etc..

Looks like I was using the wrong word to describe my problem :P, thank you.

And yes, the rendering is pixel perfect.

 

There is no filtering or 'in-between' pixel values.

Work out the values for the two objects by hand for a few iterations, and compare the values to what you expected. The computer does what you tell it. If you only move objects by 1 pixel resolutions, then there's going to be some pixel-resolution jitter. You'll need sub-pixel positioning or interpolation to remove that.

Thanks Scienthsine and Zakwayda for the help :)

I've found another way of doing it, let me explain.

Btw, here is the code

void Entity::update()
{
    if (movCycle <= myVelocity) {
        posX += velocityX * myVelocity;
        posY += velocityY * myVelocity;
        movCycle++;
    }
    else {
        movCycle = 0;
    }
}

This is kind of simple, the Entity has this 'movCycle' variable that increments every time it moves, and when the 'movCycle' is equal to 'myVelocity', instead of making the movement logic it is going to reset 'movCycle' to zero, so its basically have a 'move 4, wait 1' or 'move 1, wait 1', and so on.

Idk if it is the best way of doing it, but works for now ?

This topic is closed to new replies.

Advertisement