Coordinate System

Recommended Posts

I've been playing around with kos and have started running into some issues with the coordinate system, specifically the locations of the x and z axes.  I would like to be able to determine my ship's position in a coordinate system that is centered about kerbin's CoM and use this vector along with other inputs to more precisely control my craft on its ascent.  My current program works OK but vastly simplifies things; working with vectors would greatly expand my capabilities.

The problem is that I can't find any rhyme or reason to the orientation of the x and z axes.  When I first logged in, i plotted my position with "print -kerbin:position" and got an interesting result.  I had expected that one of the axes would pass through the prime meridian.  What I found is that the x axis passed through 0N 10.791321E.  Odd as this is, it would be doable so long as it remains fixed exactly there.  When I logged back in later, I realized I was getting funny results.  Turns out the x axis was located at 0N ~20E.  What I've found is every time I start a new flight, the x and z axes move to a new, random location.  I haven't done enough testing to see if they move during a flight, but they certainly do every time I start a new one.

I've read the documentation on kos and even it says "However, it’s hard to predict exactly where the X and Z axes will be. They keep moving depending on where you are, to the point where it’s impossible to get a fix on just which direction they’ll point."

Can anyone shed some light on this?  Worst case scenario, I can get clever and come up with a work-around to find my coordinates, but I'd rather not fool with unnecessary vector addition/rotations just to simply get my position.

Share on other sites

x is horizontal (left & right)
y is vertical, height, altitude, etc.
z is depth, distance, etc

Share on other sites

not really in my wheelhouse, but from what i can understand, it seems like the axes will always be parallel to those of the arbitrary reference frame.  since planets rotate and orbit this will cause the drift you see.

Share on other sites

I'm not all that familiar with kOS, but the first thing that comes to mind: are you perhaps in a reference frame where X points to a certain position along the celestial equator, while Kerbin rotates underneath it?

That should be easy to check for: will the value of 10.791321E change over the course of a few seconds/minutes?

Share on other sites

Thanks for the responses.  I've been looking into this and here's what I've found:

Once set, the orientation of the X and Z axes in relation to Kerbin remains stationary...kind of.  The problem is that they can start moving again if you change your focus (go back to KSC or focus on another craft).  I tested this by launching a craft and printing its coordinates at several time intervals while on the pad; they didn't move at all.  I then went back to KSC for a few minutes and re-launched my craft.  When I returned and printed the coordinates again, they were different.  I did the same test, but switched focus to a probe orbiting Eve for a while; when i returned focus to my craft on the launchpad, the coordinates had drifted.  In this test, I measured the coordinates and mission time both times so I could find the rotation rate.  The coordinates were [537282.21 -973.52 267226.53] and then [540072.76 -973.52 261540.82] ~36 seconds later.  If you do the math on this, the coordinate system is rotating 360deg/kerbin day.  The weird thing is that I moved WEST!  This is backwards from my intuition.  If the coordinate system were to suddenly "freeze" when I switch focus while the planet continues to rotate around it, the I should be moving East.  Keep in mind that KSP has a left-handed coordinate system--it's worth me mentioning this before anyone "reminds" me   It's late and I'm tired, so maybe I'm hallucinating, but could someone check me on that?  It IS weird, right?

I've read somewhere that under certain conditions, the game calculates physics as if your focus planet/moon is stationary and everything around it is moving (i.e. sun orbits around earth).  I'm not sure at the moment how to test this..

My temporary (or maybe permanent) solution:

When my ship boots up, I load a position initialization program.  One of the KOS functions lets you draw a vector from your present position to any point on the planet's surface based on latitude and longitude.  You can also find the vector from your present position to the planet's center.  All of these are in terms of the "arbitrary" coordinate system  In this "arbitrary" coordinate system, I calculate vectors representing "Standard" X Y Z axes by performing vector subtraction (X axis = Vector to LatLon(0,-90) - Vector to center of planet).  Once I normalize each of these vectors, I have unit vectors representing "standard" XYZ in terms of "arbitrary" coordinates and I can then calculate a rotation matrix to go from "arbitrary" to "standard".  I proceed to fly my mission normally, using and logging the "arbitrary" coordinates.  My telemetry log is set up to paste directly into MatLab where the rotation matrix is used to convert everything into the "standard" coordinates.  If I'm creating a flight plan in matlab to be loaded onto my ship, the math is done in terms of "standard" coordinates, loaded onto the ship, and then converted to the current "arbitrary" coordinates using the inverse of the aforementioned rotation matrix.  Unfortunately, using the KOS CPU for vector algebra is very very slow; I can't find a way around this, however, as I don't know the inverse rotation matrix until I've loaded my ship on the launch pad and performed the position initialization.

I'll be posting my code on another thread in the near future to ask for some suggestions to make it run quicker.  Just thought I'd give ya'll a heads up

Share on other sites

I do not know about kOS, but coming from Addon coding, the KSP coordinate system is strange.

Probably for historic reason rockets in KSP do fly UP, not Forward by the Unity definitions. However Planes and Rockets in Orbit still fly UP to be consistent, not forward.

Some Addons tried to "fix" this by swapping Y and Z components, but every time they exchange with KSP code of course they need to use KSP conventions, so they can only be partially consistent.

Reference Frames is another Topic: Rockets fly around Moons, which fly around Planets, which circle Kerbol and all of these Bodies rotate. And intersolar suns move in a galaxy and galaxys around....

Seriously: Get comfortable to always think in terms of a reference. There is no "right" coordinate system and even if you define any as this, movement equations would be too complicated, so just get the reference right. Oh and one caveat about rotating coordinate systems: If you do calculate the movement equations when changing from one to the other  https://en.wikipedia.org/wiki/Coriolis_force and https://en.wikipedia.org/wiki/Centrifugal_force appear. They are small enough to be neglected if you fly around Kerbin and think about the Kerbol rotation once a year. However they get way bigger if you transfer into the rotating Kerbin system and to get fixed Latitude / Longitude. So better keep bodies rotating and just stay in a center of momentum frame.

Finally one advice: There are already tons of conversion systems, try to find a solution before solving something yourself: Unity has Quaternions and Transforms, KSP has Transforms and CelestialFrames. These are battle proven, handling corner conditions etc. Your life will be easier with finding the right reference transformation than coding it yourself. Since KSP is already pretty mature, someone will have had the same problem before and there is propably a solution out there. Somewhere.

Edited by CBase
Share on other sites

18 hours ago, CBase said:

Finally one advice: There are already tons of conversion systems, try to find a solution before solving something yourself: Unity has Quaternions and Transforms, KSP has Transforms and CelestialFrames. These are battle proven, handling corner conditions etc. Your life will be easier with finding the right reference transformation than coding it yourself. Since KSP is already pretty mature, someone will have had the same problem before and there is propably a solution out there. Somewhere.

I haven't found anything yet, but as you say, I would be surprised if someone hasn't already dealt with this.  It seems too elementary of a problem for no one to have touched yet.  In the meantime, this little "project" is a great way of distracting me from the whole Wuhan Virus situation.. Using the method outlined above, I've managed to make things work out quite nicely and even managed to speed up the maths performed in the kOS CPU by a factor of 5 by streamlining a few of my user-functions.  It can crunch through the entire ascent profile (data covering about 6 minutes of flight) in about 40 seconds now.  Not great, but it will do.

Quote

Oh and one caveat about rotating coordinate systems: If you do calculate the movement equations when changing from one to the other  https://en.wikipedia.org/wiki/Coriolis_force and https://en.wikipedia.org/wiki/Centrifugal_force appear

Yes sir!...(or ma'am...)  That's the whole reason behind this.  I'm trying to take the coordinates from kOS and transform them into a non-rotating coordinate system in MatLab that I can do some math in.  Likewise, when I use MatLab to create an ascent profile, I need to convert it to coordinates that correspond to ksp's.

Edited by arthur106
Share on other sites

1 hour ago, arthur106 said:

Yes sir!

Ups sorry, I didn't intent to sound like a teacher . Just as you were suprised that a coordinate of Kerbin changed gave me the impression that you expected that it is constant. Actually I do think once you change to a vessel or the vessel changes SOI, the frame is choosen by some comfortable transformation and then simply stays as it is.

Share on other sites

16 hours ago, CBase said:

Ups sorry, I didn't intent to sound like a teacher . Just as you were suprised that a coordinate of Kerbin changed gave me the impression that you expected that it is constant. Actually I do think once you change to a vessel or the vessel changes SOI, the frame is choosen by some comfortable transformation and then simply stays as it is.

No worries.  I'm wrong more often than right and just don't know it.  Won't turn down free advice or info ever.

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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.