0
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ What are your options to rotate the object? \$\endgroup\$ – Jay Nov 16 '17 at 1:55
  • \$\begingroup\$ 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.. \$\endgroup\$ – user100681 Nov 16 '17 at 6:24
  • \$\begingroup\$ @Jay, either Quaternion or Matrix. \$\endgroup\$ – J. Dove Nov 16 '17 at 7:48
  • \$\begingroup\$ 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/… \$\endgroup\$ – Jay Nov 16 '17 at 8:12
  • \$\begingroup\$ @GabrieleVierti Well I was looking at the Unity version as a guide to Joels answer. \$\endgroup\$ – J. Dove Nov 17 '17 at 13:14
0
\$\begingroup\$

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.

edit: this was meant as a comment, but was posted as an answer and I can't delete it using the mobile app. I'll fix it up to a proper answer tomorrow.

\$\endgroup\$
  • \$\begingroup\$ Well I transformed my method with surface normals into this and it's working now, don't know what to do with this answer though because it's not answering the question but it was a great answer to my new approach. \$\endgroup\$ – J. Dove Nov 17 '17 at 13:13

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.