Jump to content

[SOLVED] Upwards rotation on planet, knowing only latitude and longitude?


Recommended Posts

I'm creating a new ship through ProtoVessel.CreateVesselNode. I managed to place it correctly on the ground and got rotation to work properly, which I tested by using data from vessels saved in persistent.sfs.

The next step is to get this working for any point on the planet, and that's where I got stuck. No matter how I try, I can't get the ship to point upwards like a proper rocket should. How can I calculate the rotation quaternion to point the ship upwards for any latitude and longitude on a celestial body?

Edited by Rahjital
Link to comment
Share on other sites

Thanks! I've already tried that, among other things. This is how it looked:

Vector3d upAxis = FlightGlobals.getUpAxis(planet, planet.GetWorldSurfacePosition(latitude, longitude, altitude));
Quaternion rotation = Quaternion.LookRotation(upAxis);

(FlightGlobals.getUpAxis does exactly what you said, and then normalizes the vector.)


And this is the result ingame:

Spoiler

CoD1mUf.png

 

I suppose the vector could be somehow manipulated to yield the correct rotation, but I'm at a loss how to do that correctly...

Link to comment
Share on other sites

That's KSP's vessel rotations at work. Always remember that vessel.transform.up is the direction your vessel is pointing in (lets call it vessel.forward) . On the other hand, the default "no rotation" quaternion indicates vessel.transform.forward (vessel.up * -1, aka vessel.down)

Any time you're dealing with vessel rotations, the following transform becomes very handy

// NOTE: Quaternion maths is not commutative. You can't change the order of operations and get the same result!!! (x * y != y * x)
Quat vesselrot = upRot * Quaternion.Euler(-90, 0, 0); // rotate the direction 90 degrees about the x (right) axis. May be 90 or -90, I haven't checked

 

Edited by Crzyrndm
Link to comment
Share on other sites

Got it to work, thank you both! Turns out that apart from having to fix the LookRotation, rotation for landed vessels is relative to the planet's transform, rather than global. The solution ended up being this:

Quaternion rotation = Quaternion.Inverse(planet.bodyTransform.rotation) * Quaternion.LookRotation(northAxis, upAxis);

 

Edited by Rahjital
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...