I'm trying to figure out how to move an object from one Cartesian point to another located on the surface of a 3D sphere. So that the object will follow the spherical coordinate system (Theta and Phi)

I've a function that converts a Cartesian to Spherical but how can I make it work on the surface of a sphere just like we are using the equation of a line y=mx+b when moving from one point to another on a flat surface?

I'm going to use it to move an object from its point to a selected point somewhere else on the sphere so that the object will move in a straight line but following the curvature.

  • \$\begingroup\$ Can you give some example start/stopping points and what exactly you need as the output? It sounds like you currently can get both the cartesian coordinates and the spherical coordinates but you need 3D vectors describing the position? \$\endgroup\$ – Jimmy Nov 17 '17 at 21:04
  • \$\begingroup\$ Yes that's right I can but I don't know how to make the movement in between the starting and stopping point in a straight line over the curved surface. I know how to do it in a completely straight line through the surface but not over on the curved surface. I don't even know where to begin my thinking on this one. \$\endgroup\$ – J. Dove Nov 17 '17 at 21:09

The general idea between interpolation between two points on a sphere is to use a "Spherical Lerp" function or "slerp" for short.

Some engines have it as a built in function, for others, either it's only available for Quaternions or missing entirely and you'll have to write it yourself. The page on wikipedia provides a good explanation of both the Vector and Quaternion versions of slerp, but to summarize the Vector3 part here:

The basic formula involves finding the spherical combination of the start and end points with a ratio of the sin of the subtended angle.

slerp(a , b, t) = 
  theta = acos(dot(a, b))
  (sin((1-t)theta) / sin(theta)) * a+ (sin(t * theta)/sin(theta)) * b

if you are animating in a loop, you can use a more efficient algorithm by iteratively reflecting each point after you've found the first intermediate point.

c = dot(p0, p1) * 2
pk+1 = c pk − pk−1
  • \$\begingroup\$ That was all I needed here's the final function in C# public Vector3 slerpIt(Vector3 a, Vector3 b, float t) { float theta = (float)Math.Acos(Vector3.Dot(Vector3.Normalize(a), Vector3.Normalize(b))); return (float)(Math.Sin((1 - t) * theta) / Math.Sin(theta)) * a + (float)(Math.Sin(t * theta) / Math.Sin(theta)) * b; } \$\endgroup\$ – J. Dove Nov 18 '17 at 12:36

It's very simple. First of all, you need to get the line between the starting and ending point (marked with purple on the following image):

enter image description here

(The green point is the center of the sphere, a is the vector from the center to the start point and b is the vector from the center to the end point, the red line is the path on the surface of the sphere)

Now to make the object follow the surface of the sphere you simply need to interpolate between the start and endpoint, get the vector between that point and the center (if c is the center and p is the interpolated position, then the vector is simply p - c), normalize it, multiply it by the radius of the sphere, then add the result to the center of the sphere to get the object's position.

Pseudo code:

vec3 getPositionOnSphere(vec3 startPos, vec3 endPos, vec3 center, float radius, float time) {
    vec3 pos = (endPos - startPos) * time + startPos
    return normalize(pos - center) * radius + center

The problem with this approach is that the end result won't be uniform. You should generate the line first, then move the object on that.

  • \$\begingroup\$ I've up voted your answer because I hade use of your explanation, it's very informative. My reputation is too low to make it seen here, so I wanted to tell you. \$\endgroup\$ – J. Dove Nov 18 '17 at 12:47

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.