I've looked around for hours and I simply don't get it. I am the first to admit I am severely lacking in mathematics. So please, keep it simple.
I am trying to get a vectors rotation (angle) around another vector so i can get useful Yaw, Pitch and Roll from said vectors. This on a "vehicle" that has a forward vector (and of course accompanying right and up vectors) which is "flying" over a planet (sphere) with gravity going towards it's center. This gravity vector is also known.
Here's an example of what i am looking at:
I am using Unity to EXPERIMENT with. The goal is to implement this in another project. Thus, all the powers of Unity doesn't help me find a solution for my other project. I am constricted to Vector3.Cross(), Vector3.Dot, Math.Cos/Sin/ACos/ASin/Tan/ATan/ATan2 etc.
Here's the code i have. I did remove all the calculations i tried beforehand as they all failed and i'd rather have a clean answer.
// Time that debug lines and rays will be displayed for. var duration = Time.deltaTime; // Draws a line through the south/north poles of the sphwere. Debug.DrawLine(Vector3.down * 100, Vector3.up * 100, Color.cyan, duration, false); // Gravity that affects the vehicle var gravity = Vector3.Normalize(planet.transform.position - transform.position) * 9.81F; Debug.DrawRay(transform.position, gravity, new Color(0.4F, 0.0F, 0.0F), duration, false); var gravityEast = Vector3.Cross(gravity.normalized, Vector3.down).normalized; var gravityNorth = Vector3.Cross(gravityEast.normalized, -gravity.normalized).normalized; Debug.DrawRay(transform.position, gravityEast, Color.yellow, duration, false); Debug.DrawRay(transform.position, gravityNorth, Color.white, duration, false); Debug.DrawRay(transform.position, transform.forward * 0.5F, Color.blue, duration, false); text.text = "gravity: " + gravity + "\n"; text.text += "gravityEast: " + gravityEast + "\n"; text.text += "gravityNorth: " + gravityNorth + "\n\n"; text.text += "forward: " + transform.forward + "\n\n";
I am looking to get Yaw in 360 degrees (-180 to 180 works too and even radians, that's not a problem), roll in -180 to 180 degrees and pitch in -90 to 90 degrees. This all in respect to the gravity vector and where zero Yaw is pointing to the north along the sphere. 90 Yaw is east, 180 is south and 270 is west.
As i said, i've been looking at hundreds of different examples (both on SE and elsewhere) and i still don't understand how to do this. All the things i have tried have either not worked at all or they have worked only in certain circumstances such as the "vehicle" being on one side of the sphere but not the others. As far as i can understand, i need to transform the calculations to local vector space so that the formulas stay relative to my ever changing gravity vector.
And because i am mathematically illiterate at best, a bunch of mathematical formulas won't really help me. I've looked at them all and it just doesn't click. Instead, if this can be shown in pseudocode or even straight up C# that would be optimal.
And just to be perfectly clear here. I know i can use "transform.eulerAngles" on my cube to get global and local angles right away. Again, i need to port the solution to another project that doesn't come with those simple properties/fields.