# Projectile Trajectory, Start/End points known

This topic is 4872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
Then, when animating the ball I do

float xt;
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 = zt * cannonBalls[g].normalDist.z;

D3DXVECTOR3 posNow = D3DXVECTOR3(cannonBalls[g].startPos.x + xt, cannonBalls[g].startPos.y + yt, cannonBalls[g].startPos.z + zt);

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by OluseyiThe 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.

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.

##### Share on other sites
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 θ

##### Share on other sites
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!