Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behaviour updating sprite position

Tags:

c++

sdl

I'm coding a simple roguelike game in C++ using SDL library, and I have some problems moving my character on the screen. Each time a frame needs to be rendered, I update the position of the sprite using the update() function, which does nothing if the player is standing still. To issue the movement command, and thus starting the animation, I use the step() function called only once per each player movement from one tile to another. Upon receiving the "up" command, the game behaves fine and the character moves smoothly in one second to the new position. However, when the "down" command is given, he moves at about half the speed, and obviously after one second has passed, he is instantly "teleported" to the final position, with a sudden flicker. The code for the movement is basically identical, but for the fact that in one case the delta movement is summed to the y position, in the other case is subtracted. Maybe the fact that the position is an integer and the delta is a double is causing problems? Does sum and subract behave differently (maybe different rounding)? Here is the relevant code (sorry for the length):

void Player::step(Player::Direction dir)
{
    if(m_status != STANDING) // no animation while standing
        return;

    switch(dir)
    {
    case UP:
        if(m_currMap->tileAt(m_xPos, m_yPos - m_currMap->tileHeight())->m_type == Tile::FLOOR)
        {
            // if  next tile is not a wall, set up animation
            m_status = WALKING_UP;
            m_yDelta = m_currMap->tileHeight(); // sprite have to move by a tile
            m_yVel = m_currMap->tileHeight() / 1000.0f; // in one second
            m_yNext = m_yPos - m_currMap->tileHeight(); // store final destination
        }
        break;
    case DOWN:
        if(m_currMap->tileAt(m_xPos, m_yPos + m_currMap->tileHeight())->m_type == Tile::FLOOR)
        {
            m_status = WALKING_DOWN;
            m_yDelta = m_currMap->tileHeight();
            m_yVel = m_currMap->tileHeight() / 1000.0f;
            m_yNext = m_yPos + m_currMap->tileHeight();
        }
        break;

    //...

    default:
        break;
    }

    m_animTimer = SDL_GetTicks();
}

void Player::update()
{
    m_animTimer = SDL_GetTicks() - m_animTimer; // get the ms passed since last update

    switch(m_status)
    {
    case WALKING_UP:
        m_yPos -= m_yVel * m_animTimer; // update position
        m_yDelta -= m_yVel * m_animTimer; // update the remaining space
        break;
    case WALKING_DOWN:
        m_yPos += m_yVel * m_animTimer;
        m_yDelta -= m_yVel * m_animTimer;
        break;

    //...

    default:
        break;
    }

    if(m_xDelta <= 0 && m_yDelta <= 0) // if i'm done moving
    {
        m_xPos = m_xNext; // adjust position
        m_yPos = m_yNext;
        m_status = STANDING; // and stop
    }
    else
        m_animTimer = SDL_GetTicks(); // else update timer
}

EDIT: I removed some variables and only left the elapsed time, the speed and the final position. Now it moves without flickering, but the down and right movements are visibly slower than the up and left ones. Still wonder why...

EDIT 2: Ok, I figured out why this is happening. As I supposed in the first place, there is a different rounding from double to integer when it comes to sum and subtraction. If I perform a cast like this:

m_xPos += (int)(m_xVel * m_animTimer);

the animation speed is the same, and the problem is solved.

like image 434
Pietro Lorefice Avatar asked Jan 15 '12 21:01

Pietro Lorefice


1 Answers

Consider the following:

#include <iostream>

void main()
{
    int a = 1, b = 1;
    a += 0.1f;
    b -= 0.1f;

    std::cout << a << std::endl;
    std::cout << b << std::endl;
}

During the implicit conversion of float to int when a and b are assigned, everything past the decimal point will be truncated and not rounded. The result of this program is:

1
0

You've said that m_yPos is an integer and m_yVel is a double. Consider what happens in Player::update if the result of m_yVel * m_animTimer is less than 1. In the UP case, the result will be that your sprite moves down one pixel, but in the DOWN case, your sprite won't move at all, because if you add less than one to an integer, nothing will happen. Try storing your positions as doubles and only converting them to integers when you need to pass them to the drawing functions.

A trick you can do to ensure rounding instead of truncation during conversion is to always add 0.5 to the floating point value during assignment to an integer.

For example:

double d1 = 1.2;
double d2 = 1.6;
int x = d1 + 0.5;
int y = d2 + 0.5;

In this case, x will become 1, while y will become 2.

like image 75
brendanw Avatar answered Sep 21 '22 07:09

brendanw