- Home /
Finding Relation Between Two Points on a Sphere
Basically I want to know how I can determine the general direction between two points on a sphere, that is, is point 2, north, west, east, or south of point 1. Don't know what other info I need to add but how can I do this?
You have to have a frame of reference to solve this problem. It can be the transform of point 1, the sphere the points or on, or the world axes. As I think about it there are also some situations you need to decide on. For example say the sphere is the reference and point 1 is near the pole and point 2 is on the opposite side of the sphere and down a ways. The shortest distance will be to head north for just a bit, travel across the north pole and then to head south. Does the point count as north because that is the initial setting, or does it count as south because point 2 is closer to the equator.
Answer by robertbu · Mar 17, 2014 at 02:58 AM
Let's assume the frame of reference is the sphere and the local 'y' axis forms the poles (positive 'y' is north and negative 'y' is south). Further, lets assume you want the direction of the first step you would take following the shortest path between point 1 and point 2.
Step 1: convert the two points into local coordinates of the sphere. You can do that using Transform.InverseTransformPoint().
Step 2: Use Vector3.Slerp() to get a point just a bit along the path from point 1 to point 2.
var nextStep = Vector3.Slerp(localPoint1, localPoint2, 0.05);
Step 3: Get a direction:
var dir = nextStep - localPoint1;
if (dir.y) is positive, then the next step will be northerly. If dir.x is positive the next direction will be easterly. You will have to figure out how you want to handle the ratio between the two to detect N vs NE vs E for example. You could use Vector3.Angle() or Vector3.Dot() to figure that out.