# How do I take 2D ballistic trajectory planning and apply it to 3D?

I am currently using this formula

speed = sqrt((gravity.y*target.x^2)/(2*cos^2(angle)*(target.y - target.x*tan(angle)))


provided by DMGregory as an answer for my question here to determine the speed of a trajectory given an angle and start/end points. So now I need to be able to apply this to a 3D scenario. How would I go about doing that?

• do you plan on taking curvature of the body into account or just the initial angles/speeds and gravity? – costrom Apr 18 '16 at 17:48
• No, I'm not concerned with curvature of the body. – Ramon Johannessen Apr 18 '16 at 17:52

First, we pick out a unit vector describing our vertical axis (or, more generally, the axis on which the constant acceleration is being applied):

Vector3 vertical = Vector3.up;  // If wind introduces diagonal acceleration,
// include it here too and normalize.


Then we isolate the perpendicular/horizontal component of the offset from the start to the end of the trajectory:

Vector3 toTarget3D = end - start;
Vector3 horizontal = (toTarget3D - Dot(toTarget3D, vertical) * vertical).normalized;


Now we can project our problem into 2D using this basis:

Vector2 toTarget2D = new Vector2(
Dot(toTarget3D, horizontal),
Dot(toTarget3D, vertical));


You can now solve this the same as the 2D version of the problem.

... // Solution logic goes here

Then, once you have your velocity in 2D, you can unproject back to 3D:

 Vector3 velocity3D = velocity2D.x * horizontal
+ velocity2D.y * vertical;


In practice, you might embed these translations into your formulae, so you don't explicitly switch dimensionality anywhere, but here I've shown it completely separate so you can drop whatever solution method you prefer into that ...

• Thanks Gregory. Most of the other explanations on this topic I read were very convoluted, this is such a clean approach, very nice. – Ramon Johannessen Apr 18 '16 at 18:26
• So if I were to get the speed based on the angle, using the formula you gave in my other question, how would I project that back to 3D? In your example you have a 2D velocity instead of speed. Do I need a 2D velocity vector or is there a way to utilize the speed? – Ramon Johannessen Apr 19 '16 at 20:39
• Speed is just the length of the velocity vector. velocity = speed * (cos(angle) * horizontal + sin(angle) * vertical) – DMGregory Apr 19 '16 at 20:44
• Ahhh, I couldn't figure out how to get the x/y out of that, that makes sense now...I'm learning so much here, you're a good man, thank you! – Ramon Johannessen Apr 19 '16 at 20:47
• (If you look closely, you'll find a version of that formula in the previous answer too) ;) – DMGregory Apr 19 '16 at 20:52