Projectile Trajectory, Start/End points known

Started by
4 comments, last by WhileTrue 19 years, 6 months ago
Greetings, I am working on the trajectories of the cannon balls that fly through my game. Right now I have it working pretty good for the case where the start and end point are at the same height. I am looking for help in determining the required initial velocity and position of the cannon ball when the start and end point heights are different. Right now, This is what I do: --------------------------------------

float angle = 70.0;
ball.gravity = -400;

float dist = sqrt((dest.x-start.x)*(dest.x-start.x) + (dest.z-start.z)*(dest.z-start.z));

// compute the magnitude of speed
float myRatio = sin(angle) * 2;
float magnitude = sqrt((dist * (-ball.gravity)) / myRatio);

// adjust speed components with angle in mind
ball.speed.x = magnitude * cos(angle);	
ball.speed.y = magnitude * sin(angle);
ball.speed.z = magnitude * sin(angle);

ball.speedMagXY = sqrt(ball.speed.x*ball.speed.x + ball.speed.y*ball.speed.y + ball.speed.z*ball.speed.z);
-------------------------------------- This code is finding the angle, and the required velocities that I need to animate the cannonball. It works great for same heights at start and end but I am looking for help to adjust these formulas for different heights. Any help is greatly appreciated.
Advertisement
Then, when animating the ball I do

float xt;
xt = (cannonBalls[g].speedMagXY * cos(cannonBalls[g].angleRadXY))*flyTime;
xt = xt * cannonBalls[g].normalDist.x;

float yt;
yt = (-1)*(cannonBalls[g].speedMagXY * sin(cannonBalls[g].angleRadXY))*flyTime - ((cannonBalls[g].gravity)*(flyTime)*(flyTime))/2;
yt = yt*(-1);

float zt;
zt = (cannonBalls[g].speedMagXY * cos(cannonBalls[g].angleRadXY))*flyTime;
zt = zt * cannonBalls[g].normalDist.z;


D3DXVECTOR3 posNow = D3DXVECTOR3(cannonBalls[g].startPos.x + xt, cannonBalls[g].startPos.y + yt, cannonBalls[g].startPos.z + zt);
The initial velocity of your cannonballs should always be the same. The endpoint should vary as a function of trajectory (this, after all, is what it means to aim). You've somehow got it backwards.
Quote:Original post by Oluseyi
The initial velocity of your cannonballs should always be the same. The endpoint should vary as a function of trajectory (this, after all, is what it means to aim). You've somehow got it backwards.


Hi, Thanks for your reply.

In my game, the end point is always set (each time the ball is fired, it hits it's end point everytime). So the endpoint is fixed going into the function. My angle is also fixed because I want the ball to always come out at a 70 degree angle.

So in my game, the only variable I have to influence is the initial velocity, this is how I get greater distances so that I can always hit the required target exactly.

For posterity, having been solved in #gamedev (thanks to RedBeard for doing the heavy lifting)...

Vy/Vy = tan θ                      -(1)x = x0 + Vx * t                    -(2)y = y0 + Vy * t - 0.5 * g * t * t  -(3)from (1):x - x0 = Vx * t=> t = (x - x0) / Vx               -(4)from (2), substituting (4):y - y0 = Vy * ((x - x0) / Vx) - 0.5 * g * ((x - x0) / Vx) * ((x - x0) / Vx)=> (y - y0) = (x - x0) * tan θ - 0.5 * g * ((x - x0) / Vx) * ((x - x0) / Vx)   -(5)

As (5) is an equation of a single unknown, Vx can be determined. Initial velocity as a 2-D vector, V, can then be obtained from
Vx = V cos θ
Great, thanks for the help.

I've been able to use these formulas in my game. With these formula's you can get perfect trajectories along the XY plane.

A slight modifcation gets them set up for any axis, to support a 3d world (x, y, z coords).

Everything is set and running 100% now, thanks again!

This topic is closed to new replies.

Advertisement