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?
Answer
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 ...
No comments:
Post a Comment