# How would I apply gravity to a bullet so that it would drop slightly the longer that it travels?

How would I apply gravity to a bullet so that it would drop slightly the longer that it travels?

I somewhat understand how the gravity works, Im just asking for second opinions for more optimized ways of doing this.

VOID update()
{
veloc_Y += gravity;//0.5f
posX += veloc_X;
posY += veloc_Y;

if(posY > 650)
{
posY = 650.0f;
veloc_Y = 0.0f;
on_g = true;//lets my game know im on the ground
}
}


so, would I apply gravity to bullet veloc_Y?

ex:

b_veloc_Y += gravity;
b_pos_Y += b_veloc_Y;


would this make my bullet drop?

You'll want to more than just add velocity every frame. That's basic Euler math, but doesn't work properly in a time sliced environment. Specifically, a variable framerate means that you'll get different results.

You'll want to do basic integration. There's a good article that goes into depth on that subject here: http://gafferongames.com/game-physics/integration-basics/

• Your advice may be true in general, but not really applicable in this particular case. Whatever discrepancies the Euler method has compared to the "true" integration of variables, the difference can be nullified by setting the initial Y velocity equal to -gravity/2. – Pasha May 22 '13 at 0:26
• Great article, but I feel the take-away message should be "use fixed frame rate (for physics), failing that, use integration". The RK4 method is simple but by no means trivial, and most of the time a fixed simulation rate is good enough. @PashaS that's not the only weakness of Euler. – congusbongus May 22 '13 at 0:29

Yes, that's how simplified gravity (or acceleration in general) works. You might want to limit your velocity (and gravity) to something (in reality: terminal velocity), e.g.

b_veloc_Y = std::max(-max_veloc_y, std::min(b_veloc_Y + gravity, max_veloc_y));


You might want to add some friction as well (maybe even make it velocity dependant) to lower acceleration over time.

• What's the point of introducing air friction in Y direction, but not in X direction? Once you hit the speed limit your bullet just flies in a straight line, which doesn't look very natural. – Pasha May 21 '13 at 17:33
• I only added one dimension, because I thought it's enough to show the basic idea/concept, especially considering the question already started with gravity. – Mario May 22 '13 at 9:33