Jump to content

Help me understand Mechjeb's vector math


Recommended Posts

For my own project, I need a bunch of things that mechjeb's VesselState helpfully defines - things like a north/east/up coordinate system, velocity vectors in various reference frames, and vectors for various useful attitudes (radial, normal etc.).

I'm having trouble grokking the definition and use of surfaceRotation. From the code (I'll assume for the discussion that we're analyzing a ship in a standard 100km orbit around Kerbin):


// Define a coordinate system in which up is away from the planet, east is in the direction of
// the planet's rotation, and north points north-ish (while remaining normal to east/up).
public Vector3d east, north, up;
public Vector3d CoM; // Vessel's center of mass.

// ???
public Quaternion surfaceRotation = Quaternion.LookRotation(north, up);

// Velocity in the universe inertial reference frame.
public Vector3d velocityVesselOrbit = vessel.orbit.GetVel();
// Velocity in the planet's rotating reference frame, i.e. velocity relative to the surface.
public Vector3d velocityVesselSurface = velocityVesselOrbit - vessel.mainBody.getRFrmVel(CoM);

// ???
public Vector3d velocityMainBodySurface = rotationSurface * velocityVesselSurface;

My current understanding is that surfaceRotation defines a rotation that would take the world coordinate system and rotate it such that, standing on the point of Kerbin directly below the ship, the Y axis increases away from the surface and X increases with latitude.

Then, we move on to the velocity vectors. velocityVesselOrbit is the ship's velocity vector in Unity world coordinates, roughly equivalent to an inertial reference frame anchored to the planet's center (this ignores the fact that the world origin moves around to maintain floating point precision, but everything is consistent within one physics update).

velocityVesselSurface is the ship's velocity vector in the planet's rotating reference frame, in other words speed relative to the point of the planet's surface directly below.

And finally, velocityMainBodySurface... Assuming my understanding of surfaceRotation is correct, I think that this just transforms the rotating frame velocity vector to a coordinate system where Y is away from the surface, instead of Y being an arbitrary direction defined by Unity.

In other words, if you plunk a Jeb down at the point directly below the ship, he would say that the vessel is moving at velocityMainBodySurface relative to him, assuming that X is north and Y is up into the sky, and Z is whatever direction matches unity's coordinate handedness.

Does all this sound correct? velocityMainBodySurface isn't used in MJ as far as I can tell, so all I have to go on is what I think I understand of quaternions and Unity's coordinate system.

Link to comment
Share on other sites

  • 1 year later...

Because this vector math involves Quaternion math. And quats are whack. For the general user, its safer not to try to understand them, unless you need to deal with rotations heavily and want to have complete control on what youre doing. Honestly, youre better off assuming this gives out what you need, and using it without understanding exactly what it does. If you really want to know what the heck this is about, I suggest you head to #RO on EsperNet and ask egg about quats.

Link to comment
Share on other sites

It would be a better idea to explain what you need that for me to comment on all MJ code :)

Post here or get on the #kspmodders channel on IRC. I am there most evening (GMT+1)

The short version is that I'm trying to make a space sim in Unity, and I'm trying to figure out how to make the ship do the things you can do in Smart ASS (turning prograde, retrograde, etc), and I'm having very little luck getting help on this particular subject in the Unity forums. So I'd thought I'd take a chance and see if I could find some ideas here. I've been looking at the MJ source code, but I'm kind of new to C# and I'm out of my depth I'm afraid.

Link to comment
Share on other sites

The short version is that I'm trying to make a space sim in Unity, and I'm trying to figure out how to make the ship do the things you can do in Smart ASS (turning prograde, retrograde, etc), and I'm having very little luck getting help on this particular subject in the Unity forums. So I'd thought I'd take a chance and see if I could find some ideas here. I've been looking at the MJ source code, but I'm kind of new to C# and I'm out of my depth I'm afraid.

Yeah you're gonna have to understand how Quaternions work then, you can't work in the dark with that.

Link to comment
Share on other sites

MJ code could help you a bit buy you have to keep in mind that KSP use multiple coordinate systems and that a lot of MJ code deal with that. That make it quite more complex that a basic Unity Scene where you have only one origin and don't have to convert everything.

The only relevant bit of code from you would be velocityVesselOrbit and velocityVesselSurface. Just ignore velocityMainBodySurface.

But I am afraid stupid_chris is right. You will have to understand quaternion at one point if you want to make what you are doing.

Link to comment
Share on other sites

Those are good points. It's not surprising that the code is as complex as it is.

That being said, I'm very interested in finding out as much as I can about how KSP works under the hood. Are there any places that I can go to where discussions of this subject are taking place?

Link to comment
Share on other sites

  • 3 weeks later...

It's not that difficult even if you only have high school maths..

Quarts is just a new name conjured up for doing the same thing - It's just presented in a different way.

Reading these topics would help

Maths:- Linear Algebra (same maths different angle - Vector maths will be in here), Basic Co-Ordinate rotations in n-planes (nothing more than Sines, Cosines)

You can go into Complex Numbers.. but it's unnecessary for these purposes (use only the real part)

Computers:- Any books on 3D game development - They all say the same thing.

Edited by ColKlonk
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...