• Advertisement
Sign in to follow this  

problem with paddle shaking in pong game

This topic is 1506 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

what type of variables are your p.y and p.last_y ?  float? int?

 

If I were you, I would change the "moveUp" and "moveDown" functions to accept an argument for the elapsed time (in seconds) since the last frame and use that value to

interpolate the movement within the paddle function - like this

  public void moveUp(float elapsedTime){

      last_y = y;
      last_x = x;

      y -= (int)(50.0f * elapsedTime);


  }




  public void moveDown(float elapsedTime){

      last_y = y;
      last_x = x;

      y += (int)(50.0f * elapsedTime);


  }

and then change your updateGame function to accept the elapsed time (again in seconds) since the last frame

updateGame(float elapsedTime) 
{
    if(p1_up)
    {
        if(player.equals("p1"))
        {
            p1.moveUp(elapsedTime);
        }
        else
        {
            p2.moveUp(elapsedTime);
        }
    }
    else if(p1_down)
    {
         if(player.equals("p1"))
        {
            p1.moveDown(elapsedTime);
        }
        else
        {
            p2.moveDown(elapsedTime);
        }
    }

Then in your game loop, it would look something like this

            while( now - lastUpdateTime > TIME_BETWEEN_UPDATES && updateCount < MAX_UPDATES_BEFORE_RENDER )
            {
               float elapsedTime = (now - lastUpdateTime) / 1000000000.0f;
               updateGame(elapsedTime);
               lastUpdateTime += TIME_BETWEEN_UPDATES;
               updateCount++;
            }

and finally your draw function then does not need any interpolated value for the paddle - just use the x and y of the paddle

      public void drawPaddle(Graphics g, Paddle p){


          // Do not need this anymore : paddle_drawX = (int)((p.x - p.last_x)*interpolation + p.last_x);
          // Do not need this anymore : paddle_drawY = (int)((p.y - p.last_y)*interpolation + p.last_y);



              g.drawRect(p.x, p.y, 10, 50);


      }

Anyways - I'm a c++ programmer - I have programmed a few java things but it was a while back so my syntax may not be exactly correct - but that system should work better for updating the paddles position - you would then change the "50" value to adjust the movement speed of the paddle - that 50 means that it will move 50 pixels per second doing the code in the way I show here

 

Hope this helps!

Share this post


Link to post
Share on other sites

The shaking effect for movement can be due to the extra piece of logic missing for evaluating what the object should do when it reaches a certain pixel.

 

I ran into that same issue when I started out game programming.

 

I sense something is strange about your position values from your other post. Those position values are random.

 

Your code design will make things more prone to bugs and difficult to maintain. You can definitely structure it more simpler without "setting many boolean flags". 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement