public void FixedUpdate() { var vessel = GetComponent <Vessel>(); if (vessel != FlightGlobals.ActiveVessel) { Destroy(this); return; } if (TimeWarp.CurrentRateIndex != 0) { TimeWarp.SetRate(0, true); Telemagic.logTM("cancel time warp"); } if (AlreadyTeleported) { if (vessel.LandedOrSplashed) { Destroy(this); } else { var accel = (vessel.srf_velocity + vessel.upAxis) * -0.5; vessel.ChangeWorldVelocity(accel); } } else { //NOT AlreadyTeleported //Still calculating var pqs = Body.pqsController; if (pqs == null) // The sun has no terrain. Everthing else has a PQScontroller. { Destroy(this); return; } var alt = pqs.GetSurfaceHeight(Body.GetRelSurfaceNVector(Latitude, Longitude)) - Body.Radius; var tmpAlt = Body.TerrainAltitude(Latitude, Longitude); double landHeight = FlightGlobals.ActiveVessel.altitude - FlightGlobals.ActiveVessel.pqsAltitude; double finalAltitude = 0.0; var checkAlt = FlightGlobals.ActiveVessel.altitude; var checkPQSAlt = FlightGlobals.ActiveVessel.pqsAltitude; double terrainAlt = GetTerrainAltitude(); Telemagic.logTM("-------------------"); Telemagic.logTM($"m1. Body.Radius = {Body.Radius}"); Telemagic.logTM("m2. PQS SurfaceHeight = " + pqs.GetSurfaceHeight(Body.GetRelSurfaceNVector(Latitude, Longitude))); Telemagic.logTM("alt ( m2 - m1 ) = " + alt); Telemagic.logTM("Body.TerrainAltitude = " + tmpAlt); Telemagic.logTM("checkAlt = " + checkAlt); Telemagic.logTM("checkPQSAlt = " + checkPQSAlt); Telemagic.logTM("landheight = " + landHeight); Telemagic.logTM("terrainAlt = " + terrainAlt); Telemagic.logTM("-------------------"); Telemagic.logTM($"Latitude: {Latitude} Longitude: {Longitude}"); Telemagic.logTM("-------------------"); alt = Math.Max(alt, 0d); // HoldVesselUnpack is in display frames, not physics frames Vector3d teleportPosition; if (!teleportedToLandingAlt) { Telemagic.logTM("teleportedToLandingAlt == false"); Telemagic.logTM("interimAltitude: " + InterimAltitude); Telemagic.logTM("Altitude: " + Altitude); if (InterimAltitude > Altitude) { if (Planetarium.GetUniversalTime() - lastUpdate >= 0.5) { InterimAltitude = InterimAltitude / 10; terrainAlt = GetTerrainAltitude(); if (InterimAltitude < terrainAlt) { InterimAltitude = terrainAlt + Altitude; } //InterimAltitude = terrainAlt + Altitude; teleportPosition = Body.GetWorldSurfacePosition(Latitude, Longitude, InterimAltitude) - Body.position; Telemagic.logTM("1. teleportPosition = " + teleportPosition); Telemagic.logTM("1. interimAltitude: " + InterimAltitude); if (lastUpdate != 0) { InterimAltitude = Altitude; } lastUpdate = Planetarium.GetUniversalTime(); } else { Telemagic.logTM("teleportPositionAltitude (no time change):"); teleportPosition = Body.GetWorldSurfacePosition(Latitude, Longitude, alt + InterimAltitude) - Body.position; Telemagic.logTM("2. teleportPosition = " + teleportPosition); Telemagic.logTM("2. alt: " + alt); Telemagic.logTM("2. interimAltitude: " + InterimAltitude); } } else { //InterimAltitude <= Altitude Telemagic.logTM("3. teleportedToLandingAlt sets to true"); landHeight = FlightGlobals.ActiveVessel.altitude - FlightGlobals.ActiveVessel.pqsAltitude; terrainAlt = GetTerrainAltitude(); //trying to find the correct altitude here. if (checkPQSAlt > terrainAlt) { alt = checkPQSAlt; } else { alt = terrainAlt; } if (alt == 0.0) { //now what? } teleportedToLandingAlt = true; //finalAltitude = alt + Altitude; if (alt < 0) { finalAltitude = Altitude; } else if (alt > 0) { finalAltitude = alt + Altitude; } else { finalAltitude = alt + Altitude; } teleportPosition = Body.GetWorldSurfacePosition(Latitude, Longitude, finalAltitude) - Body.position; Telemagic.logTM("3. teleportPosition = " + teleportPosition); Telemagic.logTM($"3. alt = {alt} Altitude = {Altitude} InterimAltitude = {InterimAltitude}"); Telemagic.logTM($"3. TerrainAlt = {terrainAlt} landHeight = {landHeight}"); } } else { Telemagic.logTM("teleportedToLandingAlt == true"); landHeight = FlightGlobals.ActiveVessel.altitude - FlightGlobals.ActiveVessel.pqsAltitude; terrainAlt = GetTerrainAltitude(); Telemagic.logTM($"4. finalAltitude = {finalAltitude}"); //finalAltitude = alt + Altitude; if (alt < 0) { finalAltitude = Altitude; } else if (alt > 0) { finalAltitude = alt + Altitude; } else { finalAltitude = alt + Altitude; } //teleportPosition = Body.GetRelSurfacePosition(Latitude, Longitude, finalAltitude); teleportPosition = Body.GetWorldSurfacePosition(Latitude, Longitude, finalAltitude) - Body.position; Telemagic.logTM("4. teleportPosition = " + teleportPosition); Telemagic.logTM($"4. alt = {alt} Altitude = { Altitude} InterimAltitude = { InterimAltitude}"); Telemagic.logTM($"4. TerrainAlt = {terrainAlt} landHeight = {landHeight}"); Telemagic.logTM("4. finalAltitude = " + finalAltitude); } var teleportVelocity = Vector3d.Cross(Body.angularVelocity, teleportPosition); // convert from world space to orbit space teleportPosition = teleportPosition.xzy; teleportVelocity = teleportVelocity.xzy; Telemagic.logTM("0. teleportPosition(xzy): " + teleportPosition); Telemagic.logTM("0. teleportVelocity(xzy): " + teleportVelocity); Telemagic.logTM("0. Body : " + Body); // counter for the momentary fall when on rails (about one second) teleportVelocity += teleportPosition.normalized * (Body.gravParameter / teleportPosition.sqrMagnitude); Quaternion rotation; if (SetRotation) { // Need to check vessel and find up for the root command pod vessel.ActionGroups.SetGroup(KSPActionGroup.SAS, false); //hopefully this disables SAS as it causes unknown results! var from = Vector3d.up; //Sensible default for all vessels if (vessel.displaylandedAt == "Runway" || vessel.vesselType.ToString() == "Plane") { from = vessel.vesselTransform.up; } var to = teleportPosition.xzy.normalized; rotation = Quaternion.FromToRotation(from, to); } else { var oldUp = vessel.orbit.pos.xzy.normalized; var newUp = teleportPosition.xzy.normalized; rotation = Quaternion.FromToRotation(oldUp, newUp) * vessel.vesselTransform.rotation; } var orbit = Telemagic.Clone(vessel.orbitDriver.orbit); orbit.UpdateFromStateVectors(teleportPosition, teleportVelocity, Body, Planetarium.GetUniversalTime()); Telemagic.SetOrbit(vessel, orbit); vessel.SetRotation(rotation); if (teleportedToLandingAlt) { AlreadyTeleported = true; Telemagic.logTM(" :FINISHED TELEPORTING:"); } } }