Orbits and PatchedConicsOrbit methods and info: Difference between revisions
(Initial version - very rough. Needs editing and a lot more info) |
m (Admin moved page Translating KSP1 code to KSP2 to Orbits and PatchedConicsOrbit methods and info without leaving a redirect: Name change) |
(No difference)
|
Revision as of 21:15, 16 April 2023
When porting from KSP1 to KSP2 one of the first things you're likely to encounter is the fact that where KSP1 defined an orbit using the Orbit class, in KSP2 it's now the PatchedConicsOrbit class. These classes have a lot in common; for example, there are a number of methods and properties where the names are almost identical other than a change of case for the initial character. Nevertheless, there are a few differences that you should know about when porting KSP1 code to work in KSP2.
So where you might have a variable of type Orbit, now you'll mostly use a variable of type PatchedConicsOrbit.
KSP1 | KSP2 |
---|---|
Orbit o; | PatchedConicsOrbit o; |
o.referenceBody.position | o.referenceBody.Position.localPosition |
o.referenceBody.transform.up | o.referenceBody.transform.up.vector |
o.referenceBody.transform.right | o.referenceBody.transform.right.vector |
o.referenceBody.Radius | o.referenceBody.radius |
o.referenceBody.orbit | o.referenceBody.Orbit |
o.LAN | o.longitudeOfAscendingNode |
Planetarium.up | o.ReferenceFrame.up.vector |
Planetarium.right | o.ReferenceFrame.right.vector |
o.PeR | o.Periapsis |
o.PeA | o.PeriapsisArl |
o.ApR | o.Apoapsis |
o.ApA | o.ApoapsisArl |
o.trueAnomaly | o.TrueAnomaly |
o.patchEndTransition | o.PatchEndTransition |
o.referenceBody.timeWarpAltitudeLimits [n] | o.referenceBody.TimeWarpAltitudeOffset*n |
KSP1 | KSP2 |
---|---|
o.getOrbitalVelocityAtUT() | o.GetOrbitalVelocityAtUTZup() |
o.getRelativePositionAtUT() | o.GetRelativePositionAtUT() |
o.GetOrbitNormal() | o.GetRelativeOrbitNormal() |
o.referenceBody.GetLatitude() | o.referenceBody.GetLatLonAltFromRadius() |
Body Type: Properties and Methods
TypeCelestialBody -> CelestialBodyComponent
Vessel Type: Properties and Methods
Coordinate Systems
Useful Code Block Conversions
Was
o.GetOrbitalStateVectorsAtUT(UT, out pos, out vel);
newOrbit.UpdateFromStateVectors(pos, vel, o.referenceBody, UT);
Is
o.GetOrbitalStateVectorsAtUT(UT, out pos, out vel);
KSP.Sim.Position position = new KSP.Sim.Position(o.referenceBody.coordinateSystem, OrbitExtensions.SwapYZ(pos - o.referenceBody.Position.localPosition));
KSP.Sim.Velocity velocity = new KSP.Sim.Velocity(o.referenceBody.relativeToMotion, OrbitExtensions.SwapYZ(vel));
newOrbit.UpdateFromStateVectors(position, velocity, o.referenceBody, UT);
Was
PatchedConics.SolverParameters solverParameters = new PatchedConics.SolverParameters();
Is
PatchedConicSolver.SolverParameters solverParameters = new PatchedConicSolver.SolverParameters();
Was
PatchedConics.CalculatePatch(o, nextOrbit, UT, solverParameters, null);
Is
nextOrbit = o.NextPatch as PatchedConicsOrbit;
Was
o.UTAtMeanAnomaly(o.GetMeanAnomalyAtEccentricAnomaly(o.GetEccentricAnomalyAtTrueAnomaly(trueAnomaly)), UT);
Is
o.GetUTforTrueAnomaly(trueAnomaly*UtilMath.Deg2Rad, o.period);
New Code
public static Vector3d DvToBurnVec(PatchedConicsOrbit o, Vector3d dV, double UT)
{
Vector3d burnVec;
burnVec.x = Vector3d.Dot(dV, o.RadialPlus(UT));
burnVec.y = Vector3d.Dot(dV, o.NormalPlus(UT));
burnVec.z = Vector3d.Dot(dV, -1 * o.Prograde(UT)); return burnVec;
}
public static Vector3d BurnVecToDv(PatchedConicsOrbit o, Vector3d burnVec, double UT) { return burnVec.x * o.RadialPlus(UT) + burnVec.y * o.NormalPlus(UT) - burnVec.z * o.Prograde(UT); }