Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


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.

  • You cannot reply to this topic
2 replies to this topic

#1 nondescript   Members   -  Reputation: 102

Like
0Likes
Like

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!

 

http://stackoverflow.com/questions/20365539/the-paddle-of-pong-keeps-shaking-without-staying-at-one-position



Sponsor:

#2 EarthBanana   Members   -  Reputation: 994

Like
1Likes
Like

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



#3 warnexus   Prime Members   -  Reputation: 1504

Like
1Likes
Like

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.



PARTNERS