Sign in to follow this  
lorddanny

Projectile Motion

Recommended Posts

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

Share this post


Link to post
Share on other sites
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:
[code]delta_t = time_elapsed_since_last_step;
velocity = velocity + delta_t * acceleration;
position = position + delta_t * velocity;[/code]

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:
[code]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;[/code]

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Are you confused as to how the code makes the curve, or how reality makes the curve? You probably need to understand [url="http://en.wikipedia.org/wiki/Acceleration#Uniform_acceleration"]uniform acceleration[/url] before you understand my pseudo-code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this