1
\$\begingroup\$

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?

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

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 ...

\$\endgroup\$
  • \$\begingroup\$ Thanks Gregory. Most of the other explanations on this topic I read were very convoluted, this is such a clean approach, very nice. \$\endgroup\$ – Ramon Johannessen Apr 18 '16 at 18:26
  • \$\begingroup\$ 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? \$\endgroup\$ – Ramon Johannessen Apr 19 '16 at 20:39
  • 1
    \$\begingroup\$ Speed is just the length of the velocity vector. velocity = speed * (cos(angle) * horizontal + sin(angle) * vertical) \$\endgroup\$ – DMGregory Apr 19 '16 at 20:44
  • \$\begingroup\$ 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! \$\endgroup\$ – Ramon Johannessen Apr 19 '16 at 20:47
  • \$\begingroup\$ (If you look closely, you'll find a version of that formula in the previous answer too) ;) \$\endgroup\$ – DMGregory Apr 19 '16 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.