Search code examples
c++game-physics

Gravity simulation for a ball


I'm implementing this game that uses gravity, but I don't know how to simulate gravity for a ball.

I have a timer that starts right after I "drop" the ball and I have to set the vertical position of my object ball.

The related functions are:

int ball->setVerticalPosition(int Y);
float timer->getTime();

Thank you!


Solution

  • ok, in general, you would calculate the new position (pos_y) with this equation:

    t = timer->getTime();
    float pos_y = pos_y0 + v_0*t - 4.9 * t *t;
    ball->setVerticalPosition(pos_y);
    

    (v_0 is the initial velocity and pos_y0 the initial coordinates of your ball). In your case, you say you are 'dropping' de ball, so probably it's better if you remove the v_0*t. And pos_y0 is the original heigth (depends on your coordinate system).

    Don't forget to check when pos_y=0 (your floor probably!)