1
\$\begingroup\$

I have been building a pong clone. I've been using a mixture of peoples different code to understand the way they structure their games as well as reading on game patterns. Currently I've gotten to draw a paddle on the screen and the player can move it with the keyboard no problem.

The issue comes when a player presses a button that is not 'Up' or 'Down'. For instance when the player holds the 'Up' key the paddle travels upwards, but if they simultaneously hold the 'Right' key it causes the paddle to fly downwards at a much greater speed.

My keyboard checking code is:

void GameLoop::Input(SDL_Event &ev)
{
    if (ev.type == SDL_KEYDOWN)
    {
        if (ev.key.keysym.scancode == SDL_SCANCODE_DOWN)
        {
            player1->IsMoving(true);
            player1->velocity = 10;
        }
        if (ev.key.keysym.scancode == SDL_SCANCODE_UP)
        {
            player1->IsMoving(true);
            player1->velocity = -10;
        }

    }

    else if (ev.type == SDL_KEYUP)
    {
        if (ev.key.keysym.scancode == SDL_SCANCODE_DOWN)
        {
            player1->IsMoving(false);
            player1->velocity = 0;
        }
        if (ev.key.keysym.scancode == SDL_SCANCODE_UP)
        {
            player1->IsMoving(false);
            player1->velocity = 0;
        }
    }
}

The GameLoop is ran within loop that is configured to run at 60FPS. You can see this in this code block here:

int main(int argc, char **argv){

    //Initialise the app and SDL
    App app(SDL_INIT_VIDEO);
    GameLoop game;

    double lastTime = 0.0, currentTime;

    SDL_Event e;

    while (app.isRunning())
    {
        if (SDL_PollEvent(&e))
        {
            if (e.type == SDL_QUIT)
            {
                app.exit();
            }
        }


        currentTime = SDL_GetTicks() / 1000.0;
        if (currentTime >= lastTime + 0.01666667)
        {
            game.Input(e);
            game.Update();
            game.Render();

            lastTime = currentTime;
        }


    }

    return 0;
}

This is the paddles update code:

void Paddle::Update()
{
    if(moving == true)
    {
        rect.y += velocity;
        velocity = 50;
    }

    X = rect.x;
    Y = rect.y;
}

I'm unsure of what is causing the paddle to behave so strangely. Any insight given into this problem or even just the way I've approached this problem would be appreciated.

\$\endgroup\$
2
  • \$\begingroup\$ This looks fine to me. Is there any other relevant code you could think of that could be causing this? You are not incrementing the velocity, you are setting a static speed, so it should work. Possibly post your Update method code. \$\endgroup\$
    – jgallant
    Apr 24, 2014 at 15:09
  • \$\begingroup\$ I edited the original post with my paddles update code. \$\endgroup\$
    – Keeko
    Apr 24, 2014 at 15:39

1 Answer 1

0
\$\begingroup\$

My guess is that your problem resides in the fact that you are not utilizing your delta time when updating your position.

if(moving == true)
{
    rect.y += velocity * delta;
    velocity = 50;
}

I am also not sure why you are setting the velocity to 50 here, whereas, in your Input function, you are setting it to -10 or 10.

\$\endgroup\$
3
  • \$\begingroup\$ Forgive me the velocity i added to do some debugging to ensure that it was indeed being changed. I have removed it and multiplied velocity by delta time. It works fine when the paddle goes up, it however doesnt work when holding down, the paddle just wont move anymore, its y position is not being changed despite the correct value being calculated \$\endgroup\$
    – Keeko
    Apr 24, 2014 at 16:11
  • \$\begingroup\$ Actually I discovered the root of the problem. The end value from delta time was <1 decimal number, which was causing it to be rounded down to 0 when it was being added the rect.y which is an integer. How should I avoid this, should i make my coordinates floats? \$\endgroup\$
    – Keeko
    Apr 24, 2014 at 16:13
  • \$\begingroup\$ @KyleMotherwell Ah, yeah, what I typically do is store a Vector2 for the position, which keeps track of floating points. Then on the update, I set the Rectangle position based on the position. When calculating the position change, always use your Vector2 instead of the Rectangle, to maintain floating point values. \$\endgroup\$
    – jgallant
    Apr 24, 2014 at 16:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .