title: Game Physics - Friction
body: <p>My very simple question is: how would I implement friction? Nothing I try seems to be working.</p>
  
  <p>Oh, and <code>speedX</code> = 1.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>public void update() {
      x += velocityX;
  }
  
  public void keyPressed(KeyEvent e) {
      int key = e.getKeyCode();
  
      if (key == KeyEvent.VK_RIGHT)
          vx = xSpeed;
  
      if (key == KeyEvent.VK_LEFT)
          vx = -xSpeed;
  }
  
  public void KeyReleased(KeyEvent e) {
      int key = e.getKeyCode();
  
      if (key == KeyEvent.VK_RIGHT)
          vx = 0;
  
      if (key == KeyEvent.VK_LEFT)
          vx = 0;
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>Now, once the player stops, he slows down (which is good), but he doesn't stop completely.</p>
  
  <pre><code>if (key == KeyEvent.VK_RIGHT) {
          vx = 0.20 * vx;
  
          if (vx &lt; 0.2)
              vx = 0;
  
          playerAction = "still";
      }
  </code></pre>