title: How do I apply gravity to my bouncing ball application?
body: <p>I've written a fairly simple java application that allows you to drag your mouse and based on the length of the mouse drag you did, it will shoot a ball in that direction, bouncing off walls as it goes.</p>
  
  <p>Here is a quick screenshot:<br>
  <a href="http://img222.imageshack.us/img222/3179/ballbouncemf9.png">alt text http://img222.imageshack.us/img222/3179/ballbouncemf9.png</a></p>
  
  <p>Each one of the circles on the screen is a Ball object.  The balls movement is broken down into an x and y vector;</p>
  
  <pre><code>public class Ball {
      public int xPos;
      public int yPos;
      public int xVector;
      public int yVector;
  
      public Ball(int xPos, int yPos, int xVector, int yVector) {
          this.xPos = xPos;
          this.yPos = yPos;
          this.xVector = xVector;
          this.yVector = yVector;
      }
  
      public void step()
      {
          posX += xVector;
          posY += yVector;
  
          checkCollisions();
      }
  
      public void checkCollisions()
      {
          // Check if we have collided with a wall
          // If we have, take the negative of the appropriate vector
          // Depending on which wall you hit
      }
  
      public void draw()
      {
          // draw our circle at it's position
      }
  }
  </code></pre>
  
  <p>This works great.  All the balls bounce around and around from wall to wall.  </p>
  
  <p>However, I have decided that I want to be able to include the effects of gravity.  I know that objects accelerate toward the earth at 9.8m/s but I don't directly know how this should translate into code. I realize that the yVector will be affected but my experimentation with this didn't have the desired effect I wanted.</p>
  
  <p>Ideally, <strong><em>I would like to be able to add some gravity effect to this program and also allow the balls to bounce a few times before settling to the "ground."</em></strong>  </p>
  
  <p>How can I create this bouncing-elastic, gravity effect?  How must I manipulate the speed vectors of the ball on each step?  What must be done when it hits the "ground" so that I can allow it to bounce up again, but somewhat shorter then the previous time?</p>
  
  <p>Any help is appreciated in pointing me in the right direction.</p>
  
  <hr>
  
  <p>Thanks you for the comments everyone!  It already is working great!</p>
  
  <p>In my step() I am adding a gravity constant to my yVector like people suggested and this is my checkCollision():</p>
  
  <pre><code>public void checkCollision()
  {
      if (posX - radius &lt; 0)              // Left Wall?
      {
          posX = radius;              // Place ball against edge
          xVector = -(xVector * friction);
      }
      else if (posX + radius &gt; rightBound) // Right Wall?
      {
          posX = rightBound - radius;     // Place ball against edge
          xVector = -(xVector * friction);
      }
  
      // Same for posY and yVector here.
  }
  </code></pre>
  
  <p>However, the balls will continue to slide around/roll on the floor.  I assume this is because I am simply taking a percentage (90%) of their vectors each bounce and it is never truly zero.  Should I add in a check that if the xVector becomes a certain absolute value I should just change it to zero?</p>