• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# problem with paddle shaking in pong game

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1nondescript  Members

Posted 03 December 2013 - 08:21 PM

Can you help me with this problem i am facing in creating a pong game? I appreciate your help!

### #2EarthBanana  GDNet+

Posted 04 December 2013 - 11:47 AM

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);
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!

### #3warnexus  Prime Members

Posted 04 December 2013 - 09:07 PM

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".

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.