Jump to content

Weird issues with Orbit constructors.


Recommended Posts

I was trying to make a method which would "clone" an existing Orbit object into a new Orbit object (as a starting point to be mutated), and looking at the KSP API there are 3 approaches to this, but only 1 of which I was successful with.  

This is what worked, using "new Orbit()" followed by "UpdateFromStateVectors()".  For some reason this failed when I tried setting UT to "o.epoch" which I don't understand.  So this method defaults to the current tick and if that isn't valid then its up to the caller to sort it out:

        // returns a new orbit that is identical to the current one (although the epoch will change)
        // (i tried many different APIs in the orbit class, but the GetOrbitalStateVectors/UpdateFromStateVectors route was the only
one that worked)
        public static Orbit Clone(this Orbit o, double UT = Double.NegativeInfinity)
        {
            Vector3d pos, vel;

            // hack up a dynamic default value to the current time
            if ( UT == Double.NegativeInfinity )
                UT = Planetarium.GetUniversalTime();

            Orbit newOrbit = new Orbit();
            o.GetOrbitalStateVectorsAtUT(UT, out pos, out vel);
            newOrbit.UpdateFromStateVectors(pos, vel, o.referenceBody, UT);

            return newOrbit;
        }

What failed was going in through the constructor that takes another Orbit object `Orbit new_orbit = new Orbit(o)` and going in through the constructor which takes keplerian elements `new Orbit(inc, e, sma, lan, argPe, maAtEpoch, epoch, body)`.  What I found is that I'd get a new Orbit that had the wrong true anomaly, and while it had the correct LAN, ArgPe and inclination (and SMA/ecc) the h vector was rotated and incorrect -- which seems internally inconsistent.  I beat my head against this problem for about 4.5 hours before just using the UpdateFromStateVectors() approach above.  What I'd kind of like to know is how the constructors are meant to be used and if there needs to be some other call made to initialize or update the object to make it internally consistent or some other undocumented voodoo that I'm missing?

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...