# Convert 2D direction vectors to 3D vectors around sphere

Hi,

I have a set of data with XY velocities (m/s), based upon longitude and latitude data. I am trying to visualize these vectors on a globe. I got very far: However, I keep getting stuck that the vectors point the wrong direction at certain sides of the planet. The planet has been made with spherical coordinates (and the plane version is just lat/lon * 100). East of longitude 0 things start to go horribly wrong.

I am clueless if I need to use complicated math functions or can just use normals/cross/dot products to get things done (much better for performance anyway). But I keep having the wrong direction problem in 3D.

Because of this, I build a 2D version version today, to check if things would work well. After a bit of fiddling around, it seems to work just fine. However, why does it not work on a sphere; if I use the sphere center as a normal to calculate the tangent? I am trying to solve this with normal functions that are easy to port across other languages (so none one of the LookAt , RotateTowards functions etc).

`

``````   Vector3 normalToCenter = (CenterOfEarth - myPosition);

// Draw normal to earth.
// Debug.DrawLine(myPosition, myPosition + normalToCenter * 60.0f);

// Calculate Tangent to planet.
Vector3 tangent = Vector3.Cross(myVelocity, normalToCenter);
Vector3 newVector = Vector3.Cross(normalToCenter, tangent);

// Draw actual line.
Vector3 finalVector = newVector.normalized * VelocityScale.magnitude;

// For 3D:
Gizmos.DrawLine(myPosition, myPosition + finalVector);
``````

`

Can someone give some advice? After spending last weekend and every evening this week on this, I am a bit desperate to get it fixed. Help is kindly appreciated.

Example (left 2D, right, 3D):

Thank you for any thoughts and help!

Well just ask yourself this: assume we are at some longitue where it all works. Lets call this longtitude zero. Now you go around the world sphere 180 degrees. if you take a cross product here you do it with the global “toCenter” vector and a non-global vector “myVelocity”. which will if you just draw that on a paper result in tangents pointing in different directions even if you take the same “myVelocity”

you need a proper coordinate transformation here. Once transform the position to a sphere. Then rotate the vector by the needed angles. There is already a function for this by multiplication with Quaternions. Another way would be to implement your own rotation matrix.
There should be enough material on the internet on this topic.

Hi @Captain_Pineapple , thank you for your message! Your example makes sense. However I have a bit of a struggle with your advice.

Would you suggest to convert the actual position of the particle point to a spherical coordinate (I assume a unit sphere, so from spherical coordinates to some normalized spherical coordinates) and then calculate the angles based upon the velocity vector? Vector math is not really my thing, but would the problem not be the same, as the velocity would still be based on the original northern/eastward direction? How would I transform this velocity to match with the unit sphere?

Rotation matrices will depend on Sin/Cos functions and I am trying to see if I can solve this with a little of those as possible considering their performance impact. However, if I were to construct one how would you suggest to base the input on (eg. how to determine the angles to put in there?), in order to prevent the same issue as I have now from happening? I have spend quite some time searching for information on this issue and even with my limited math background, and learned a lot from reading university slides and books about atmospheric math / linear algebra in general .

Yet I am struggling to see the full picture to get it done properly - as the amount of different symbols and formula’s can be quite overwhelming.