Projectile Motion

Started by
2 comments, last by alvaro 12 years, 9 months ago
Hi guys,

I'm trying to create one of these "cannon game" in 2D where the cannon fires a ball at a target and the ball falls to the ground due to gravity. I have pretty much no knowledge of physics, so can someone explain to me how this projectile motion thing is calculated and how to track the position of the ball when being launch or in flight. All I understand is ( for a start ) that I need to know the gravity, the initial height of where the ball is lauching from, angle and velocity, but thats about it really... Any help is greatly appreciated.

Thanks

P.S. I just want to understand the maths first, which is why I didn't say which programming language I'm going use incase anyone is wondering :P
Advertisement
You need to keep track of the position of the cannonball and its velocity. The process of updating this state as time progresses is called "integration". If you consider time as being divided into frames, you can use one of several "numerical integration methods". The easiest one is called "Euler integration", and in pseudo-code it looks like this:
delta_t = time_elapsed_since_last_step;
velocity = velocity + delta_t * acceleration;
position = position + delta_t * velocity;


The velocity and position both have an x and a y component, so if you don't have classes to deal with those types, your code might look more like this:
delta_t = time_elapsed_since_last_step;

acceleration_x = 0;
acceleration_y = -gravity;

velocity_x = velocity_x + delta_t * acceleration_x;
velocity_y = velocity_y + delta_t * acceleration_y;

position_x = position_x + delta_t * velocity_x;
position_y = position_y + delta_t * velocity_y;


If you want to introduce wind, each frame you can compute a force that's proportional to the difference between the cannonball's velocity and the wind's velocity, divide it by the mass of the cannonball and add it to the acceleration. If you need help with that part you can ask for it later, but try to implement just gravity first.
Thanks for the reply. I think I have an idea how to track the ball, but I'm still a little confused on how it acturally makes the "curve", is there anyway you can explain it a bit more?

Thanks
Are you confused as to how the code makes the curve, or how reality makes the curve? You probably need to understand uniform acceleration before you understand my pseudo-code.

This topic is closed to new replies.

Advertisement