- Home /
ballistic trajectory around a sphere
I am working on a project which requires a missile to travel around a sphere (a planet).
I am using a parabolic trajectory found with the equation:
f(x) = (-h / l2) * (x - l)2 + h + r where h is parabola height, l is x position of the apex (this is found from the target position as a ratio of the arc length which is known), and r is the radius of the planet the missile is on.
I then put the resulting f(x) values into a float-array. I now want to convert that array into Vector3 points as the missile flies. This is easy enough assuming I can find the shortest direction from the original point to the target point.
Like I say, I already know the shortest arc length d = sin-1(chord / 2r) * 2r but that does not tell me the shortest direction around the sphere.
So, how do I find the shortest direction around a sphere from the missile origin point to a target? Remember that the missile will be firing almost perpendicular to the tangent of the sphere's surface as per the shape of a parabola.
All help is appreciated, -Conan
Answer by Owen-Reynolds · Jan 23, 2012 at 06:20 PM
So, this is like firing nuclear missiles between Russia and us, and some go over the artic, since that's closer, but some might be shorter going over the Atlantic?
If you take the normals of the launch point and target, then the Cross-Product will give you the rotation axis between them. (Untested starting here) Since you're plotting in (x,y), that's the local z-axis as they fly. So, use Quaternion.FromToRotation(new Vector3(0,0,1), CrossProd)
to map your (x,y) points into that space.
Yes, exactly!
The cross-product, that makes sense and requires no extra objects or conversions. I'll plug it in and see if it'll also help with my number over-flow problem.
Answer by senad · Jan 23, 2012 at 09:54 AM
So, did I understand it well, that f(x) gives you points, which are all on a constant r around the planet center. And now you want to find out the shortest way from start to end point while maintaining a constant radius??
If yes, the problem sounds trivial to me. You can compute the vectors from starting point to the planet center and from the end point to the planet center. If you then look at the angle between the two you can see, if it is bigger or smaller than 180°.
This decides the direction, in which you traverse the points from f(x). Did I miss something? :) :)
Senad, thanks for your reply,
f(x) only gives the distance of the missile from the center of the sphere but not direction. The problem being that I was using a 2d Cartesian plane for finding height then trying to use those y values to plot points in 3d space. Such a thing requires knowing the orientation of the great circle but my math skills are far below adequate. I worked around this using an empty object in the center of the sphere and having it use the Quaternion.Slerp function and multiplying the f(x) results by its forward vector; which meets my needs.
I now run into another problem where the points produced are not always correctly calculated. I often find something like (175.002332, Nan, 0). I have scoured my functions and equations with calculators and graph paper in tow, but there are no fundamental errors. I have used all kinds of debugging techniques and feel that the problem lies in Unity trying to be too accurate. This would lead to overflows and other problems when I'm trying to push my numbers through.
So now its all about trying to make my own Vector3-like class using doubles (which I don't think Unity supports) or find some other way to fix this problem.
Any suggestions?
If you are looking for a trjectory in 3D space, best would be to use polar coordinates. (in the 2D-case it is similar but simpler) Read it up, if you do not know what they are. Basically they are a way to use 2D coordinates on a sphere.
Then you calculate the shortest way between two points in polar coordinates (2D on sphere).
Then you sample a few points between the start and end points. Still in polar coords, so they will all be on the sphere.
When you are done with all of this, you can transform back to 3D world coords and have your trajectory in discrete points.
All this you can do without using Unity functionality. I do not know if there is an easier way, but imo this knowledge will always be useful, if you are making 3D games. So it is very worth reading all this stuff up if you have the time and nerves. ;)
Right, the (A, W) or Longitude, Latitude. I was really trying to avoid using it because I have little experience and with my school & work schedule, didn't want to learn new things. So I'll take the time to read it up and learn. In the mean time thank you for your help.
I agree the other answer is a much better solution. :)
No need to go to polar coords and back. Also interpolation of quaternions is much easier then angles. :)