# Moving 3D object over Spherical terrain

I'm trying to control a 3D object (just a box for the moment) moving over a spherical 3D planet, I'm using a Ray to find the triangle in the sphere under the object. I'm calculating the surface normal and it's working this far but now I want the object to be rotated or just forced to be align according to the surface normal of the triangle from the sphere so that the object's down vector will be in the same angle as the surface normal vector so that it has the right angle against the surface.

I've searched the Internet but haven't succeeded yet after a couple of weeks. How on earth can I make my object align to the surface normal while moving freely around the planet/sphere?

I'm trying to make like a car driving around on the surface of a sphere.

The question is, how can I align the object to the surface normal? The code I have this far is to find the triangle under the box, now I would like to align the object to it.

I'm using Monogame in C# VS Studio 2017

• What are your options to rotate the object? – Jay Nov 16 '17 at 1:55
• If you want to know how it actually works, here is how it can be done in Unity: youtu.be/gHeQ8Hr92P4 i've never used monogame, so i can't really help you with that.. – user100681 Nov 16 '17 at 6:24
• @Jay, either Quaternion or Matrix. – J. Dove Nov 16 '17 at 7:48
• You should start by determining the up, forward and right vectors. These should correspond to a row each in your rotation matrix. See math.stackexchange.com/questions/180418/… – Jay Nov 16 '17 at 8:12
• @GabrieleVierti Well I was looking at the Unity version as a guide to Joels answer. – J. Dove Nov 17 '17 at 13:14

no time for a full answer, but assuming your pivot is centered, you can get the "down" vector (from the car POV) with car.transform.position - sphere.transform.position and then align the car relative to that vector, no need to cast a ray or calculate a surface normal (assuming you're sticking with a sphere). if you keep track of the previous position of the car on the sphere, you can calculate a directional vector from that position to the new position and then rotate the down vector 90 degrees along that movement vector. that new vector becomes the new forward vector of your car. alternatively, take the cross product of the calculated down vector and the car's right vector to get the new forward vector.