Sign in to follow this  
transcendental

2D cannon game and its trajectory

Recommended Posts

transcendental    122
Hi guys, I want to make a 2D cannon game but I do not know how to calculate the trajectory and I am not good in calculus either. Here is the description of the game, The cannon is on cliff which is on the right side of the screen. Below the cliff, is an area where the target is determined by the computer. The player can control the angle(pivot) of the cannon, and the force of firing the cannon. Here is an example of how the game works typically; If the target is very near the cliff, the cannon will adjust at 80 degrees and the cannon ball will fire very high up and fall down on the target. Of course, if the player adjusts the cannon at 90 degree, the cannon ball will fall down on the cannon itself. The problem I have, is I do not know how to calculate the trajectory as it involves the pull of the gravity on the cannon ball. I don't know if the problem above sounds like homework but it isn't. Please help! Thank you!

Share this post


Link to post
Share on other sites
SpaceDude    208
define a vector (2 floats, for 2D environment) called Pos for the position of the cannon ball, and another vector called Vel for the velocity of the ball and finally you can define another called Acc for the acceleration of the ball.

What you want to do is perform a number of iterations, each iteration will have a time step. And at each iteration you will update the velocity and position of the ball. Gravity will remain constant. Something like this:

STRUCT XY
{
float x, y;
};


XY Pos, Vel, Acc;
float fTimeStep = 0.01f;

Acc.x = 0;
Acc.y = -9.8;

// Set Pos and Vel to whatever initial values you want (this will determine start point of the cannon ball and speed/direction)

while (1)
{
Vel += Acc * fTimeStep; // you'll need some operator overloading here in the XY struct
Pos += Vel * fTimeStep;
// Draw the position of the ball
}

Share this post


Link to post
Share on other sites
jyk    2094
Spacedude's answer pretty much covers it, but here is another thread you could look at. It presents the same solution as Spacedude's, applied to a different but basically equivalent problem.

Share this post


Link to post
Share on other sites
nilkn    960
First represent acceleration, velocity, and position as vectors. Next, represent gravity as the 2-vector g = (0, -9.8) and the force applied to the canonball by the cannon as the 2-vector f.

Now, observe that with no wind or anything similar, the x component of the velocity vector remains constant throughout the trip of the cannonball; all that changes is the y component, at the rate of -9.8 as defined by g.

Your description implies that you do not explicitly input f, but rather give the magnitude of f as a scalar and then the angle of elevation of the vector geometrically speaking. We can calculate the exact values of f as such (these formulae are known as direction cosines):

f_x = |f| * cos(a_x)
f_y = |f| * cos(a_y)

Note that in 2 dimensions a_x and a_y are in effect the angles of elevation and depression respectively. Thus, a_y = 90 - a_x.

We know that the acceleration is constant g throughout the entire trip, so we can skip it. It's easy to see that our velocity v(t) = f - g*t = (|f| * cos(a_x), |f| * cos(a_y) - t*g_y). We can integrate this to obtain our position function s(t):

s(t) = (a_x * cos(|f|) * t, a_y * cos(|f|) * t - (g_y * t^2)/2)

By subsitution of a_y = 90 - a_x we obtain

s(t) = (a_x * cos(|f|) * t, (90 - a_x) * cos(|f|) * t - (g_y * t^2)/2)

Hope this helps,
nilkn

[Edited by - nilkn on November 9, 2005 7:28:33 PM]

Share this post


Link to post
Share on other sites
transcendental    122
Hi guys,

I tried out nilkn's suggestion. I thank him/her for working out the equation for me, which I cannot do by my own effort, but the equation have some typos and the gravity vector should be added, instead of minus, for the y-coord. I thank Spacedude again, for without his post, I wouldn't have understood nilkn's post. I also thank jyk; I would want to make a firework effect in the future.

However I still have some questions; Most of the you said that the "x component of the velocity" would be constant, I am wondering how that can be. Let me give an analogy: A ball rolling on a flat ground will stop somehow due to gravity. So by right, the x component of the velocity of my cannon ball should slow down as well, isn't it?

My other question is the trajectory which I plot, isn't what I expect. The ascending curve and the descending curve are symmetrical. I expect the natural trajecory would be, how should I describe it, the ascending curve scretched and the descending curve squashed. Well that can be worked out by increasing the deceleration over time.

Share this post


Link to post
Share on other sites
rip-off    10976
Quote:
Original post by transcendental
Hi guys,

I tried out nilkn's suggestion. I thank him/her for working out the equation for me, which I cannot do by my own effort, but the equation have some typos and the gravity vector should be added, instead of minus, for the y-coord. I thank Spacedude again, for without his post, I wouldn't have understood nilkn's post. I also thank jyk; I would want to make a firework effect in the future.

However I still have some questions; Most of the you said that the "x component of the velocity" would be constant, I am wondering how that can be. Let me give an analogy: A ball rolling on a flat ground will stop somehow due to gravity. So by right, the x component of the velocity of my cannon ball should slow down as well, isn't it?

My other question is the trajectory which I plot, isn't what I expect. The ascending curve and the descending curve are symmetrical. I expect the natural trajecory would be, how should I describe it, the ascending curve scretched and the descending curve squashed. Well that can be worked out by increasing the deceleration over time.



y co-ord is plus because in most 2d systems the y origin is in the top-left hand of the screen. plus would move it down, which is the same effect but inverted, in 3d and in reality

Quote:

"x component of the velocity" would be constant.


it is when its flying in the air. what would slow it down when its moving sideways( ignoring air resistance ).

Quote:

A ball rolling on a flat ground will stop somehow due to gravity.


no, thats generally surface friction.

gravity always points towards the center of the earth, whcih can be assumed to be just "down" in most games

Quote:

The ascending curve and the descending curve are symmetrical.


it should be symmetrical, ignoring air resistance.

Share this post


Link to post
Share on other sites
nilkn    960
Quote:
Original post by transcendental
I tried out nilkn's suggestion. I thank him/her for working out the equation for me, which I cannot do by my own effort, but the equation have some typos and the gravity vector should be added, instead of minus, for the y-coord.


I'm sorry about those mistakes. I did that completely from memory, so I'm sure I made a typo somewhere. The gravity vector should be (0, 9.8) rather than (0, -9.8) which lets vector subtraction work; that was a mistake on my part.

I've dug up my old physics books, and found the actual equations for you. My book however provides three sets of equations, one for each of these cases:

- the start and end points have the same height
- the start point is lower than the end point
- the start point is higher than the end point

I'll just post the set for the first case for now. If you need the others just tell me.

x(t) = (L * cos(alpha))*t
y(t) = (L * sin(alpha))*t - (gt2)/2

where

L = length of cannon *or* magnitude of initial velocity
alpha = angle of elevation of cannon

Hope this helps.

[Edited by - nilkn on November 13, 2005 12:15:53 PM]

Share this post


Link to post
Share on other sites
Simplest way to do air resistance is just

a += -(k*v)/m

where a is the acceleration of the cannon ball, v is velocity of the cannon ball, m is the mass of the cannon ball and k is a constant that you can either pick yourself or lookup the value somewhere! You should get what you expect a parabola that seems stretched on one side and squished on the other. You should try to realize that the above equation is a "differential equation" and your should try to know how you would go about solving it but just calling the above line every frame should work too.

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