title: Applying gravity to objects
body: <p>I'm having trouble with implementing gravity on objects.</p>
  
  <p>I have a bunch of circle objects that I'm drawing using OpenGL. I'm using <code>delta-x</code> and <code>delta-y</code> to move the circles (balls). I'm trying to add a gravitational constant to the <code>y</code> coordinate each frame so it simulates being pulled downward, but I'm not  sure exactly how to do that.</p>
  
  <p>Here's the relevant code:</p>
  
  <pre><code>class Ball
  {
      public:
          double x;
          double y;
          double radius;
          double deltaX;
          double deltaY;
  };
  
  
  std::vector&lt;Ball&gt; gAllBalls;  // a vector of balls with random positions and delta-values
  double gGravity = ?;  // gravitational constant - I know it's 92 m/s^s, but I don't know how to apply that to deltaX and deltaY
  
  
  void display(void)
  {
      glClear(GL_COLOR_BUFFER_BIT);
  
      for (auto &amp;thisBall : gAllBalls)
      {
          // draw
          glColor3d(thisBall.red, thisBall.green, thisBall.blue);
          DrawCircle(thisBall.x, thisBall.y, thisBall.radius);
  
          // gravity - not working
          if (thisBall.y + thisBall.radius &lt; gScreenHeight - gGravity)
          {
              thisBall.y += gGravity;
          }
  
          // wall bouncing
          if (thisBall.y + thisBall.radius + thisBall.deltaY &gt;= gScreenHeight)  // up
          {
              thisBall.deltaY = -thisBall.deltaY;
          }
  
          if (thisBall.y + thisBall.deltaY - thisBall.radius &lt; 0)  // down
          {
              thisBall.deltaY = -thisBall.deltaY;
          }
  
          if (thisBall.x + thisBall.deltaX - thisBall.radius &lt; 0) // left
          {
              thisBall.deltaX = -thisBall.deltaX;
          }
  
          if (thisBall.x + thisBall.radius + thisBall.deltaX &gt;= gScreenWidth)  // right
          {
              thisBall.deltaX = -thisBall.deltaX;
          }
  
          // move
          thisBall.x += thisBall.deltaX;
          thisBall.y += thisBall.deltaY;
      }
      glutSwapBuffers();
      glutPostRedisplay();
  }
  </code></pre>
  
  <p>A big problem I'm having is that I don't know how to calculate the gravity, since I'm using <code>deltaX</code> and <code>deltaY</code> instead of having separate speed and distance variables to calculate 92 m/s^2. However, no matter what I set the gravity to, the balls don't behave like they should - regardless of gravity strength, so there has to be something else wrong, but I don't know what.</p>