private static Location KSCLocation()
        {
            CelestialBody home = FlightGlobals.Bodies.Where(cb => cb.isHomeWorld).First();
            PQSCity       ksc  = home.GetComponentsInChildren <PQSCity>(true).Where(pqs => pqs.name == "KSC").First();

            return(new Location(home, home.GetLatitude(ksc.transform.position), home.GetLongitude(ksc.transform.position)));
        }
        protected bool isNearAnomaly()
        {
            CelestialBody mainBody = this.part.vessel.mainBody;

            PQSSurfaceObject[] anomalies = mainBody.pqsSurfaceObjects;
            double             longitude;
            double             latitude;
            double             distance = 0f;

            if (this.part.vessel.situation == Vessel.Situations.LANDED || this.part.vessel.situation == Vessel.Situations.PRELAUNCH)
            {
                for (int index = 0; index < anomalies.Length; index++)
                {
                    if (requiredAnomalies.Contains(anomalies[index].SurfaceObjectName))
                    {
                        //Get the longitude and latitude of the anomaly
                        longitude = mainBody.GetLongitude(anomalies[index].transform.position);
                        latitude  = mainBody.GetLatitude(anomalies[index].transform.position);

                        //Get the distance (in meters) from the anomaly.
                        distance = Utils.HaversineDistance(longitude, latitude,
                                                           this.part.vessel.longitude, this.part.vessel.latitude, this.part.vessel.mainBody) * 1000;

                        //If we're near the anomaly, then we're done
                        if (distance <= minAnomalyRange)
                        {
                            return(true);
                        }
                    }
                }
            }

            //Not near anomaly or not close enough
            return(false);
        }
        public static Coordinates SurfacePoint(Vector3 worldPos, CelestialBody body)
        {
            var c = new Coordinates(body.GetLatitude(worldPos), body.GetLongitude(worldPos), 0);

            c.SetAlt2Surface(body);
            return(c);
        }
Example #4
0
        //*******************************************************
        public static Vector3 SimAeroForce(CelestialBody body, IShipconstruct vessel, Vector3 v_wrld_vel, Vector3 position)
        {
            double latitude = body.GetLatitude(position) / 180.0 * Math.PI;
            double altitude = (position - body.position).magnitude - body.Radius;

            return(SimAeroForce(body, vessel, v_wrld_vel, altitude, latitude));
        }
Example #5
0
        //*******************************************************
        public static Vector3 SimAeroForce(List <Part> parts, CelestialBody body, Vector3 v_wrld_vel, Vector3 position)
        {
            double latitude = body.GetLatitude(position) / 180.0 * Math.PI;
            double altitude = (position - body.position).magnitude - body.Radius;

            return(SimAeroForce(parts, body, v_wrld_vel, altitude, latitude));
        }
Example #6
0
        // Credits to https://github.com/jrossignol/WaypointManager/blob/master/source/WaypointManager/CustomWaypointGUI.cs
        // for this code
        public static bool GetBodyRayIntersect(CelestialBody targetBody, bool map, out double latitude, out double longitude, out double altitude)
        {
            latitude  = 0;
            longitude = 0;
            altitude  = 0;
            if (targetBody.pqsController == null)
            {
                return(false);
            }

            Ray mouseRay = map ? PlanetariumCamera.Camera.ScreenPointToRay(Input.mousePosition) : FlightCamera.fetch.mainCamera.ScreenPointToRay(Input.mousePosition);

            if (map) // use scaled space
            {
                mouseRay.origin = ScaledSpace.ScaledToLocalSpace(mouseRay.origin);
            }
            var    bodyToOrigin = mouseRay.origin - targetBody.position;
            double curRadius    = targetBody.pqsController.radiusMax;
            double lastRadius   = 0;
            int    loops        = 0;

            while (loops < 10)
            {
                Vector3d relSurfacePosition;
                if (PQS.LineSphereIntersection(bodyToOrigin, mouseRay.direction, curRadius, out relSurfacePosition))
                {
                    var    surfacePoint = targetBody.position + relSurfacePosition;
                    double alt          = targetBody.pqsController.GetSurfaceHeight(
                        QuaternionD.AngleAxis(targetBody.GetLongitude(surfacePoint), Vector3d.down) * QuaternionD.AngleAxis(targetBody.GetLatitude(surfacePoint), Vector3d.forward) * Vector3d.right);
                    double error = Math.Abs(curRadius - alt);
                    if (error < (targetBody.pqsController.radiusMax - targetBody.pqsController.radiusMin) / 500)
                    {
                        latitude  = targetBody.GetLatitude(surfacePoint);
                        longitude = targetBody.GetLongitude(surfacePoint);
                        altitude  = targetBody.TerrainAltitude(latitude, longitude);
                        return(true);
                    }
                    else
                    {
                        lastRadius = curRadius;
                        curRadius  = 0.5 * alt + 0.5 * curRadius;
                        loops++;
                    }
                }
                else
                {
                    if (loops == 0)
                    {
                        break;
                    }
                    // Went too low, needs to try higher
                    else
                    {
                        curRadius = (lastRadius * 9 + curRadius) / 10;
                        loops++;
                    }
                }
            }
            return(true);
        }
        public static double GetPressure(Vector3 position, CelestialBody body)
        {
            double latitude = body.GetLatitude(position) / 180.0 * Math.PI;
            double altitude = (position - body.position).magnitude - body.Radius;
            return GetPressure(altitude, latitude, body);

        }
Example #8
0
        private void GeneratePQSCityCoordinates(WaypointData wpData, CelestialBody body)
        {
            LoggingUtil.LogVerbose(this, "   pqs city: {0}", wpData.pqsCity);
            LoggingUtil.LogVerbose(this, "   Generating a waypoint based on PQS city {0}...", wpData.pqsCity.name);

            Vector3d position = wpData.pqsCity.transform.position;

            LoggingUtil.LogVerbose(this, "    pqs city position = {0}", position);

            // Translate by the PQS offset (inverse transform of coordinate system)
            Vector3d v = wpData.pqsOffset;
            Vector3d i = wpData.pqsCity.transform.right;
            Vector3d j = wpData.pqsCity.transform.forward;
            Vector3d k = wpData.pqsCity.transform.up;

            LoggingUtil.LogVerbose(this, "    i, j, k = {0}, {1}, {2}", i, j, k);
            LoggingUtil.LogVerbose(this, "    pqs transform = {0}", wpData.pqsCity.transform);
            LoggingUtil.LogVerbose(this, "    parent transform = {0}", wpData.pqsCity.transform.parent);
            Vector3d offsetPos = new Vector3d(
                (j.y * k.z - j.z * k.y) * v.x + (i.z * k.y - i.y * k.z) * v.y + (i.y * j.z - i.z * j.y) * v.z,
                (j.z * k.x - j.x * k.z) * v.x + (i.x * k.z - i.z * k.x) * v.y + (i.z * j.x - i.x * j.z) * v.z,
                (j.x * k.y - j.y * k.x) * v.x + (i.y * k.x - i.x * k.y) * v.y + (i.x * j.y - i.y * j.x) * v.z
                );

            offsetPos *= (i.x * j.y * k.z) + (i.y * j.z * k.x) + (i.z * j.x * k.y) - (i.z * j.y * k.x) - (i.y * j.x * k.z) - (i.x * j.z * k.y);
            wpData.waypoint.latitude  = body.GetLatitude(position + offsetPos);
            wpData.waypoint.longitude = body.GetLongitude(position + offsetPos);
            LoggingUtil.LogVerbose(this, "    resulting lat, lon = ({0}, {1})", wpData.waypoint.latitude, wpData.waypoint.longitude);
        }
Example #9
0
        public static Vector3d WorldPositionToGeoCoords(Vector3d worldPosition, CelestialBody body)
        {
            if (!body)
            {
                return(Vector3d.zero);
            }
            var _lat = body.GetLatitude(worldPosition);
            var _lon = body.GetLongitude(worldPosition);

            var _random = new System.Random().Next(0, 10);
            var random  = _random / 5;

            double lat_ = 0.0f;
            double lon_ = 0.0f;

            if (_random <= 5)
            {
                lat_ = _lat + (random / 10000);
                lon_ = _lon + (random / 10000);
            }
            else
            {
                lat_ = _lat + (random / -10000);
                lon_ = _lon + (random / -10000);
            }

            double lat   = lat_;
            double longi = lon_;
            double alt   = body.GetAltitude(worldPosition);

            Debug.Log("[Spawn OrX ODST] Lat: " + lat + " - Lon:" + longi + " - Alt: " + alt);
            return(new Vector3d(lat, longi, alt));
        }
Example #10
0
        public static ConfigNode GenerateILSConfig(string siteName, Vector3 endpoint, float heading)
        {
            CelestialBody body      = FlightGlobals.ActiveVessel.mainBody;
            Vector2d      localizer = GenerateLocalizerCoords((Vector3d)endpoint, heading);

            ConfigNode ILScfg = new ConfigNode();

            ILScfg.name = cfgNodeName;
            ILScfg.AddValue("ident", siteName);
            // Well, if we make it editable, than NavUtilities will save this runway into custom.cfg
            // And next time there will be two duplicate runways. Not a good thing.
            // We'll try to address it from NavUtilities side
            // ILScfg.AddValue ("custom", "True"); // Make it editable via NavUtils menu
            ILScfg.AddValue("shortID", siteName);
            ILScfg.AddValue("hdg", heading);
            ILScfg.AddValue("body", body.name);
            ILScfg.AddValue("altMSL", body.GetAltitude((Vector3d)endpoint));
            ILScfg.AddValue("gsLatitude", body.GetLatitude(endpoint));
            ILScfg.AddValue("gsLongitude", body.GetLongitude(endpoint));
            ILScfg.AddValue("locLatitude", localizer.x);
            ILScfg.AddValue("locLongitude", localizer.y);
            // marker distances are blankly hardcoded as of now
            ILScfg.AddValue("outerMarkerDist", 8000);
            ILScfg.AddValue("middleMarkerDist", 2000);
            ILScfg.AddValue("innerMarkerDist", 300);

            ConfigNode wrapper = new ConfigNode();

            wrapper.AddNode(ILScfg);
            return(wrapper);
        }
Example #11
0
            public Vector3d Up()
            {
                double lat = body.GetLatitude(center);
                double lon = body.GetLongitude(center);

                return(body.GetSurfaceNVector(lat, lon));
            }
Example #12
0
        }         // So many ifs.....

        // Cinically stolen from Waypoint Manager source code :D
        private void PlaceTargetAtCursor()
        {
            CelestialBody targetBody = this.vessel.mainBody;

            if (targetBody.pqsController == null)
            {
                return;
            }

            Ray mouseRay = PlanetariumCamera.Camera.ScreenPointToRay(Input.mousePosition);

            mouseRay.origin = ScaledSpace.ScaledToLocalSpace(mouseRay.origin);
            var    bodyToOrigin = mouseRay.origin - targetBody.position;
            double curRadius    = targetBody.pqsController.radiusMax;
            double lastRadius   = 0;
            int    loops        = 0;

            while (loops < 50)
            {
                Vector3d relSurfacePosition;
                if (PQS.LineSphereIntersection(bodyToOrigin, mouseRay.direction, curRadius, out relSurfacePosition))
                {
                    var    surfacePoint = targetBody.position + relSurfacePosition;
                    double alt          = targetBody.pqsController.GetSurfaceHeight(
                        QuaternionD.AngleAxis(targetBody.GetLongitude(surfacePoint), Vector3d.down) * QuaternionD.AngleAxis(targetBody.GetLatitude(surfacePoint), Vector3d.forward) * Vector3d.right);
                    double error = Math.Abs(curRadius - alt);
                    if (error < (targetBody.pqsController.radiusMax - targetBody.pqsController.radiusMin) / 100)
                    {
                        targetLatitude  = (targetBody.GetLatitude(surfacePoint) + 360) % 360;
                        targetLongitude = (targetBody.GetLongitude(surfacePoint) + 360) % 360;

/*						this.distanceToTarget = GeoUtils.GetDistance(
 *                                                      this.vessel.latitude, this.vessel.longitude, this.targetLatitude, this.targetLongitude, this.vessel.mainBody.Radius
 *                                              );
 *                                              this.distanceTravelled = 0;*/
                        return;
                    }
                    else
                    {
                        lastRadius = curRadius;
                        curRadius  = alt;
                        loops++;
                    }
                }
                else
                {
                    if (loops == 0)
                    {
                        break;
                    }
                    // Went too low, needs to try higher
                    else
                    {
                        curRadius = (lastRadius * 9 + curRadius) / 10;
                        loops++;
                    }
                }
            }
        }
Example #13
0
        public static void logPlanetaryPosition(Vector3 v, CelestialBody planet)
        {
            double lng = planet.GetLongitude(v);
            double lat = planet.GetLatitude(v);
            double alt = planet.GetAltitude(v);

            Debug.Log(formatVector(planet.GetWorldSurfacePosition(lat, lng, alt)));
        }
Example #14
0
        public static void logLongLatAlt(Vector3 v, CelestialBody planet)
        {
            double lng = planet.GetLongitude(v);
            double lat = planet.GetLatitude(v);
            double alt = planet.GetAltitude(v);

            Debug.Log(string.Format("Long: {0}, Lat: {1}, Alt: {2}", lng, lat, alt));
        }
Example #15
0
        public static void logLongLatAlt(Vector3 v, CelestialBody planet)
        {
            double lng = planet.GetLongitude(v);
            double lat = planet.GetLatitude(v);
            double alt = planet.GetAltitude(v);

            Utils.Log("Long: {}, Lat: {}, Alt: {}", lng, lat, alt);
        }
Example #16
0
        //*******************************************************
        public static Vector3d SimAeroForce(Vector3d v_wrld_vel, Vector3 position)
        {
            CelestialBody body     = Trajectories.AttachedVessel.mainBody;
            double        latitude = body.GetLatitude(position) / 180.0 * Math.PI;
            double        altitude = (position - body.position).magnitude - body.Radius;

            return(SimAeroForce(v_wrld_vel, altitude, latitude));
        }
Example #17
0
        public static Vector3d planetaryPosition(Vector3 v, CelestialBody planet)
        {
            double lng = planet.GetLongitude(v);
            double lat = planet.GetLatitude(v);
            double alt = planet.GetAltitude(v);

            return(planet.GetWorldSurfacePosition(lat, lng, alt));
        }
Example #18
0
        /// <summary>
        /// Place target at cursor in the map mode. Borrowed Waypoint Manager and changed.
        /// Returns latitude and longitude of a target or double.MinValue, if target is not valid
        /// </summary>
        /// <param name="targetBody"></param>
        /// <returns>[latitude, longitude]</returns>
        internal static double[] PlaceTargetAtCursor(CelestialBody targetBody)
        {
            if (targetBody.pqsController == null)
            {
                return new double[2] {
                           double.MinValue, double.MinValue
                }
            }
            ;

            Ray mouseRay = PlanetariumCamera.Camera.ScreenPointToRay(Input.mousePosition);

            mouseRay.origin = ScaledSpace.ScaledToLocalSpace(mouseRay.origin);
            var    bodyToOrigin = mouseRay.origin - targetBody.position;
            double curRadius    = targetBody.pqsController.radiusMax;
            double lastRadius   = 0;
            int    loops        = 0;

            while (loops < 50)
            {
                Vector3d relSurfacePosition;

                if (PQS.LineSphereIntersection(bodyToOrigin, mouseRay.direction, curRadius, out relSurfacePosition))
                {
                    var    surfacePoint = targetBody.position + relSurfacePosition;
                    double alt          = targetBody.pqsController.GetSurfaceHeight(QuaternionD.AngleAxis(targetBody.GetLongitude(surfacePoint), Vector3d.down) * QuaternionD.AngleAxis(targetBody.GetLatitude(surfacePoint), Vector3d.forward) * Vector3d.right);
                    double error        = Math.Abs(curRadius - alt);
                    if (error < (targetBody.pqsController.radiusMax - targetBody.pqsController.radiusMin) / 100)
                    {
                        return(new double[2] {
                            (targetBody.GetLatitude(surfacePoint) + 360) % 360,
                            (targetBody.GetLongitude(surfacePoint) + 360) % 360
                        });
                    }
                    else
                    {
                        lastRadius = curRadius;
                        curRadius  = alt;
                        loops++;
                    }
                }
                else
                {
                    if (loops == 0)
                    {
                        break;
                    }
                    else // Went too low, needs to try higher
                    {
                        curRadius = (lastRadius * 9 + curRadius) / 10;
                        loops++;
                    }
                }
            }
            return(new double[2] {
                double.MinValue, double.MinValue
            });
        }
Example #19
0
        public override void OnFixedUpdate()
        {
            CelestialBody body = vessel.mainBody;
            ORSX_PlanetaryResourcePixel res_pixel =
                ORSX_PlanetaryResourceMapData.getResourceAvailability(vessel.mainBody.flightGlobalsIndex, resourceName,
                                                                      body.GetLatitude(vessel.transform.position), body.GetLongitude(vessel.transform.position));

            abundance = res_pixel.getAmount();
        }
        public void UpdateCoordinates(CelestialBody body)
        {
            if (target == null)
            {
                return;
            }
            Name = target.GetName();
            switch (TargetInfo.targetType)
            {
            case ProtoTargetInfo.Type.Vessel:
                var v = target as Vessel;
                if (v == null)
                {
                    break;
                }
                set_coordinates(v);
                return;

            case ProtoTargetInfo.Type.PartModule:
                var m = target as PartModule;
                if (m == null || m.vessel == null)
                {
                    break;
                }
                set_coordinates(m.vessel);
                return;

            case ProtoTargetInfo.Type.Part:
                // There may be ITargetable Part derivatives in some mods
                // ReSharper disable once SuspiciousTypeConversion.Global
                var p = target as Part;
                if (p == null || p.vessel == null)
                {
                    // ProtoTargetInfo.FindTarget() may return a Vessel when the Type == Part
                    goto case ProtoTargetInfo.Type.Vessel;
                }
                set_coordinates(p.vessel);
                return;

            case ProtoTargetInfo.Type.Generic:
                var t = target.GetTransform();
                if (t == null)
                {
                    break;
                }
                var position = t.position;
                set_coordinates(body.GetLatitude(position),
                                body.GetLongitude(position),
                                body.GetAltitude(position));
                return;
            }
            TargetInfo.targetType = ProtoTargetInfo.Type.Null;
            target = null;
            Valid  = false;
        }
        public void UpdateCoordinates(CelestialBody body)
        {
            if (target == null)
            {
                return;
            }
            Name = target.GetName();
            switch (TargetInfo.targetType)
            {
            case ProtoTargetInfo.Type.Vessel:
                var v = target as Vessel;
                if (v == null)
                {
                    break;
                }
                set_coordinates(v);
                return;

            case ProtoTargetInfo.Type.PartModule:
                var m = target as PartModule;
                if (m == null || m.vessel == null)
                {
                    break;
                }
                set_coordinates(m.vessel);
                return;

            case ProtoTargetInfo.Type.Part:
                var p = target as Part;
                if (p == null || p.vessel == null)
                {
                    break;
                }
                set_coordinates(p.vessel);
                return;

            case ProtoTargetInfo.Type.Generic:
                var t = target.GetTransform();
                if (t == null)
                {
                    break;
                }
                set_coordinates(body.GetLatitude(t.position),
                                body.GetLongitude(t.position));
                return;

//			case ProtoTargetInfo.Type.CelestialBody:
            default:
                break;
            }
            TargetInfo.targetType = ProtoTargetInfo.Type.Null;
            Name  += " last location";
            target = null;
        }
Example #22
0
        private static float GetHeading(Transform transform)
        {
            CelestialBody body     = FlightGlobals.ActiveVessel.mainBody;
            Vector3       upVector = body.GetSurfaceNVector(body.GetLatitude((Vector3d)transform.position),
                                                            body.GetLongitude((Vector3d)transform.position)).normalized;
            Vector3 north   = Vector3.ProjectOnPlane(body.transform.up, upVector).normalized;
            Vector3 east    = Vector3.Cross(upVector, north).normalized;
            Vector3 forward = Vector3.ProjectOnPlane(transform.forward, upVector);

            return(Vector3.Angle(forward, north));
        }
Example #23
0
        public static Vector3d WorldPositionToGeoCoords(Vector3d worldPosition, CelestialBody body)
        {
            if(!body)
            {
                return Vector3d.zero;
            }

            double lat = body.GetLatitude(worldPosition);
            double longi = body.GetLongitude(worldPosition);
            double alt = body.GetAltitude(worldPosition);
            return new Vector3d(lat,longi,alt);
        }
    public static Vector3d WorldPositionToGeoCoords(Vector3d worldPosition, CelestialBody body)
    {
      if (!body)
      {
        return Vector3d.zero;
      }

      double lat = body.GetLatitude(worldPosition);
      double longi = body.GetLongitude(worldPosition);
      double alt = body.GetAltitude(worldPosition);
      return new Vector3d(lat, longi, alt);
    }
        public static Location GetLocation(PQSCity city)
        {
            if (city == null)
            {
                return(null);
            }

            Vector3d      position = city.transform.position;
            CelestialBody body     = Part.GetComponentUpwards <CelestialBody>(city.sphere.gameObject);

            return(new Location(body, body.GetLatitude(position), body.GetLongitude(position)));
        }
Example #26
0
        protected float getAnomalyBonus()
        {
            CelestialBody mainBody = this.part.vessel.mainBody;

            PQSSurfaceObject[] anomalies = mainBody.pqsSurfaceObjects;
            double             longitude;
            double             latitude;
            double             distance = 0f;

            ConfigNode[] anomalyBlacklists = GameDatabase.Instance.GetConfigNodes(kAnomalyBlacklistNode);
            string       blacklist         = kAnomalyBlacklist;

            //We've covered the stock anomalies, now make sure we can blacklist anomalies from mods.
            try
            {
                foreach (ConfigNode blacklistNode in anomalyBlacklists)
                {
                    blacklist += ";" + blacklistNode.GetValue("anomalies");
                }
            }
            catch { }

            if (this.part.vessel.situation == Vessel.Situations.LANDED || this.part.vessel.situation == Vessel.Situations.PRELAUNCH)
            {
                for (int index = 0; index < anomalies.Length; index++)
                {
                    //If the anomaly is on the blacklist, skip it.
                    if (blacklist.Contains(anomalies[index].SurfaceObjectName))
                    {
                        continue;
                    }

                    //Get the longitude and latitude of the anomaly
                    longitude = mainBody.GetLongitude(anomalies[index].transform.position);
                    latitude  = mainBody.GetLatitude(anomalies[index].transform.position);

                    //Get the distance (in kilometers) from the anomaly.
                    distance = Utils.HaversineDistance(longitude, latitude,
                                                       this.part.vessel.longitude, this.part.vessel.latitude, this.part.vessel.mainBody);

                    //If we're near the anomaly, then we get a big location bonus.
                    if (distance < kMinAnomalyDistance)
                    {
                        anomalyName = anomalies[index].SurfaceObjectName;
                        return(kAnomalyBonus);
                    }
                }
            }

            //No bonus
            anomalyName = string.Empty;
            return(0f);
        }
Example #27
0
        public double PositionToLatitude(Vector pos)
        {
            CelestialBody parent = GetParentBody();

            if (parent == null) //happens when this Orbitable is the Sun
            {
                return(0.0);
            }
            Vector3d unityWorldPos = GetPosition() + (Vector3d)Shared.Vessel.findWorldCenterOfMass();

            return(Utils.DegreeFix(parent.GetLatitude(unityWorldPos), -180));
        }
Example #28
0
        public double PositionToLatitude(Vector pos)
        {
            CelestialBody parent = Orbit.referenceBody;

            if (parent == null) //happens when this Orbitable is the Sun
            {
                return(0.0);
            }
            Vector3d unityWorldPos = GetPosition() + Shared.Vessel.GetWorldPos3D();

            return(parent.GetLatitude(unityWorldPos));
        }
Example #29
0
        private static void PlaceWaypointAtCursor()
        {
            if (targetBody.pqsController == null)
            {
                return;
            }

            Ray mouseRay = PlanetariumCamera.Camera.ScreenPointToRay(Input.mousePosition);

            mouseRay.origin = ScaledSpace.ScaledToLocalSpace(mouseRay.origin);
            var    bodyToOrigin = mouseRay.origin - targetBody.position;
            double curRadius    = targetBody.pqsController.radiusMax;
            double lastRadius   = 0;
            int    loops        = 0;

            while (loops < 50)
            {
                Vector3d relSurfacePosition;
                if (PQS.LineSphereIntersection(bodyToOrigin, mouseRay.direction, curRadius, out relSurfacePosition))
                {
                    var    surfacePoint = targetBody.position + relSurfacePosition;
                    double alt          = targetBody.pqsController.GetSurfaceHeight(
                        QuaternionD.AngleAxis(targetBody.GetLongitude(surfacePoint), Vector3d.down) * QuaternionD.AngleAxis(targetBody.GetLatitude(surfacePoint), Vector3d.forward) * Vector3d.right);
                    double error = Math.Abs(curRadius - alt);
                    if (error < (targetBody.pqsController.radiusMax - targetBody.pqsController.radiusMin) / 100)
                    {
                        latitude  = targetBody.GetLatitude(surfacePoint).ToString();
                        longitude = targetBody.GetLongitude(surfacePoint).ToString();
                        return;
                    }
                    else
                    {
                        lastRadius = curRadius;
                        curRadius  = alt;
                        loops++;
                    }
                }
                else
                {
                    if (loops == 0)
                    {
                        break;
                    }
                    // Went too low, needs to try higher
                    else
                    {
                        curRadius = (lastRadius * 9 + curRadius) / 10;
                        loops++;
                    }
                }
            }
        }
Example #30
0
        public static bool CBhit(CelestialBody body, out Vector2 latlon)
        {
            Vector3d hitA;
            Vector3  origin, dir;

            if (MapView.MapIsEnabled)
            {
                //Use Scaled camera and don't attempt physics raycast if in map view.
                Ray ray = ScaledCamera.Instance.camera.ScreenPointToRay(Input.mousePosition);
                origin = ScaledSpace.ScaledToLocalSpace(ray.origin);
                dir    = ray.direction.normalized;
            }
            else
            {
                //Attempt ray cast and return results if successfull.
                Ray        ray = FlightCamera.fetch.mainCamera.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitB;
                float      dist = (float)(Vector3.Distance(body.position, ray.origin) - body.Radius / 2);
                if (Physics.Raycast(ray, out hitB, dist))
                {
                    latlon = new Vector2((float)body.GetLatitude(hitB.point), (float)body.GetLongitude(hitB.point));
                    return(true);
                }
                //if all else fails, try with good oldfashioned arithmetic.
                origin = ray.origin;
                dir    = ray.direction.normalized;
            }

            if (CBhit(body, origin, dir, out hitA))
            {
                latlon = new Vector2((float)body.GetLatitude(hitA), (float)body.GetLongitude(hitA));
                return(true);
            }
            else
            {
                latlon = Vector2.zero;
                return(false);
            }
        }
        public static Vector3d WorldPositionToGeoCoords(Vector3d worldPosition, CelestialBody body)
        {
            if (!body)
            {
                return(Vector3d.zero);
            }
            double lat   = body.GetLatitude(worldPosition);
            double longi = body.GetLongitude(worldPosition);
            double alt   = body.GetAltitude(worldPosition);

            Debug.Log("[Spawn OrX Waldo Attack] Lat: " + lat + " - Lon:" + longi + " - Alt: " + alt);
            return(new Vector3d(lat, longi, alt));
        }
            void FixedUpdate()
            {
                if (param != null && param.starJeb != null)
                {
                    if (param.velocity != null)
                    {
                        param.starJeb.SetPosition(param.starJeb.transform.position + (param.velocity.Value * Time.fixedDeltaTime));
                    }
                    else if (param.Destination != null)
                    {
                        Vector3 direction = (param.starJeb.transform.position - param.Destination.Value).normalized;

                        float t = Time.fixedTime - param.stepTime;
                        float x = t / 8.0f - 1.0f;
                        if (t + Time.fixedDeltaTime * 2.0f > 8.0f)
                        {
                            x = 0.0f;
                        }

                        if (param.starJeb.altitude < param.starJeb.terrainAltitude + 10.0f)
                        {
                            CelestialBody body = param.DestinationBody;
                            double        lat  = body.GetLatitude(param.starJeb.transform.position);
                            double        lon  = body.GetLongitude(param.starJeb.transform.position);
                            param.starJeb.SetPosition(body.GetWorldSurfacePosition(lat, lon, param.starJeb.terrainAltitude + 10.0f));
                        }
                        else
                        {
                            param.starJeb.SetPosition(param.Destination.Value + direction * param.startDistance * x * x);
                        }
                    }

                    if (param.Destination != null)
                    {
                        // Adjust the velocity, but only every frame - adjusting this too frequently
                        // seems to cause the velocity to resonate back and forth
                        if (Time.frameCount != lastAdjustment)
                        {
                            lastAdjustment = Time.frameCount;
                            param.starJeb.ChangeWorldVelocity(-param.starJeb.GetSrfVelocity());

                            // Keep us safe!
                            foreach (Part p in param.starJeb.parts)
                            {
                                p.crashTolerance = 10000000.0f;
                            }
                            CheatOptions.NoCrashDamage = true;
                        }
                    }
                }
            }
 public DMAnomalyObject(PQSCity City)
 {
     city = City;
     name = city.name;
     try
     {
         body = FlightGlobals.Bodies.FirstOrDefault(b => b.name == city.transform.parent.name);
     }
     catch (Exception e)
     {
         DMUtils.Logging("Something Went Wrong Here: {0}", e);
     }
     if (body != null)
     {
         worldLocation = city.transform.position;
         lat = clampLat(body.GetLatitude(worldLocation));
         lon = clampLon(body.GetLongitude(worldLocation));
     }
 }
Example #34
0
 public static string traceTrans(string prev, Transform tr, CelestialBody body = null)
 {
     string tmp;
     if (body != null)
     {
         tmp = prev + "." + tr.name + " - (" + body.GetLatitude(tr.position) + ", " + body.GetLongitude(tr.position) + ", " + body.GetAltitude(tr.position) + ")\n";
     }
     else
     {
         tmp = prev + "." + tr.name + " - (" + tr.position + ", " + tr.rotation + ")\n";
     }
     Component[] comps = tr.gameObject.GetComponents<Component>();
     foreach (Component comp in comps)
     {
         tmp += "\t" + comp.GetType().Name + " - " + comp.name + "\n";
     }
     for (int i = 0; i < tr.childCount; i++)
     {
         tmp += traceTrans(prev + "." + tr.name, tr.GetChild(i), body);
     }
     return tmp;
 }
Example #35
0
 public static Coordinates GetMouseCoordinates(CelestialBody body)
 {
     Ray mouseRay = PlanetariumCamera.Camera.ScreenPointToRay(Input.mousePosition);
     mouseRay.origin = ScaledSpace.ScaledToLocalSpace(mouseRay.origin);
     Vector3d relOrigin = mouseRay.origin - body.position;
     Vector3d relSurfacePosition;
     if (PQS.LineSphereIntersection(relOrigin, mouseRay.direction, body.Radius, out relSurfacePosition))
     {
         Vector3d surfacePoint = body.position + relSurfacePosition;
         return new Coordinates(body.GetLatitude(surfacePoint), MuUtils.ClampDegrees180(body.GetLongitude(surfacePoint)));
     }
     else
     {
         return null;
     }
 }
Example #36
0
        // relativePosition is in world frame, but relative to the body (i.e. inertial body space)
        // returns the altitude above sea level (can be negative for bodies without ocean)
        private double GetGroundAltitude(CelestialBody body, Vector3 relativePosition)
        {
            if (body.pqsController == null)
                return 0;

            double lat = body.GetLatitude(relativePosition + body.position) / 180.0 * Math.PI;
            double lon = body.GetLongitude(relativePosition + body.position) / 180.0 * Math.PI;
            Vector3d rad = new Vector3d(Math.Cos(lat) * Math.Cos(lon), Math.Sin(lat), Math.Cos(lat) * Math.Sin(lon));
            double elevation = body.pqsController.GetSurfaceHeight(rad) - body.Radius;
            if (body.ocean)
                elevation = Math.Max(elevation, 0.0);

            return elevation;
        }
        public Vector3d getForces_StockAero(CelestialBody body, Vector3d bodySpacePosition, Vector3d airVelocity, double angleOfAttack, double dt)
        {
            Vector3 position = body.position + bodySpacePosition;
            double latitude = body.GetLatitude(position) / 180.0 * Math.PI;
            double altitude = (position - body.position).magnitude - body.Radius;
            if (altitude > body.atmosphereDepth)
            {
                return Vector3d.zero;
            }
#if !USE_CACHE
            return computeForces_StockAero(altitude, airVelocity, new Vector3(0, 1, 0), angleOfAttack, dt);
#else
            //double approxMachNumber = useNEAR ? 0.0 : (double)FARAeroUtil_GetMachNumber.Invoke(null, new object[] { body_, body.maxAtmosphereAltitude * 0.5, new Vector3d((float)airVelocity.magnitude, 0, 0) });
            //Util.PostSingleScreenMessage("machNum", "machNumber = " + actualMachNumber + " ; approx machNumber = " + approxMachNumber);

            Vector2 force = getCachedForce(airVelocity.magnitude, altitude, angleOfAttack);

            Vector3d forward = airVelocity.normalized;
            Vector3d right = Vector3d.Cross(forward, bodySpacePosition).normalized;
            Vector3d up = Vector3d.Cross(right, forward).normalized;

            return forward * force.x + up * force.y;
#endif
        }
        private IEnumerator updateCoordinates(CelestialBody b)
        {
            yield return new WaitForSeconds(3);

            currentBodyAnomalies.Clear();

            if (anomalies.ContainsKey(b.name))
            {
                foreach (var anom in anomalies[b.name].Values)
                {
                    anom.WorldLocation = anom.City.transform.position;
                    anom.Lat = b.GetLatitude(anom.WorldLocation);
                    anom.Lon = b.GetLongitude(anom.WorldLocation);
                }

                currentBodyAnomalies = anomalies[b.name].Values.ToList();
            }
            else
                currentBodyAnomalies = new List<DMAnomalyObject>();
        }
 static Coordinates Search(CelestialBody body, Ray mouseRay)
 {
     Vector3d relSurfacePosition;
     Vector3d relOrigin = mouseRay.origin - body.position;
     double curRadius = body.pqsController.radiusMax;
     double lastRadius = 0;
     double error = 0;
     int loops = 0;
     float st = Time.time;
     while(loops < 50)
     {
         if(PQS.LineSphereIntersection(relOrigin, mouseRay.direction, curRadius, out relSurfacePosition))
         {
             var alt = body.pqsController.GetSurfaceHeight(relSurfacePosition);
             if(body.ocean && alt < body.Radius) alt = body.Radius;
             error = Math.Abs(curRadius - alt);
             if(error < (body.pqsController.radiusMax - body.pqsController.radiusMin) / 100)
             {
                 var surfacePoint = body.position + relSurfacePosition;
                 return new Coordinates(body.GetLatitude(surfacePoint), body.GetLongitude(surfacePoint));
             }
             else
             {
                 lastRadius = curRadius;
                 curRadius = alt;
                 loops++;
             }
         }
         else
         {
             if(loops == 0) break;
             else
             { // Went too low, needs to try higher
                 curRadius = (lastRadius * 9 + curRadius) / 10;
                 loops++;
             }
         }
     }
     return null;
 }
Example #40
0
        //*******************************************************
		public static Vector3 SimAeroForce(List<Part> parts, CelestialBody body, Vector3 v_wrld_vel, Vector3 position)
        {
            double latitude = body.GetLatitude(position) / 180.0 * Math.PI;
            double altitude = (position - body.position).magnitude - body.Radius;

			return SimAeroForce(parts, body, v_wrld_vel, altitude, latitude);
        }
Example #41
0
 public static double PQSAltitude(Vector3d pos, CelestialBody body)
 {
     return (pos - body.position).magnitude - body.Radius - PQSSurfaceHeight(body.GetLatitude(pos), body.GetLongitude(pos), body);
 }
Example #42
0
        /// <summary>
        /// Converts world position to Lat,Long,Alt form.
        /// </summary>
        /// <returns>The position in geo coords.</returns>
        /// <param name="worldPosition">World position.</param>
        /// <param name="body">Body.</param>
        public static Vector3d WorldPositionToGeoCoords(Vector3d worldPosition, CelestialBody body)
        {
            if(!body)
            {
                //Debug.Log ("BahaTurret.VectorUtils.WorldPositionToGeoCoords body is null");
                return Vector3d.zero;
            }

            double lat = body.GetLatitude(worldPosition);
            double longi = body.GetLongitude(worldPosition);
            double alt = body.GetAltitude(worldPosition);
            return new Vector3d(lat,longi,alt);
        }
        private void GeneratePQSCityCoordinates(WaypointData wpData, CelestialBody body)
        {
            LoggingUtil.LogVerbose(this, "   pqs city: " + wpData.pqsCity);
            LoggingUtil.LogVerbose(this, "   Generating a waypoint based on PQS city " + wpData.pqsCity.name + "...");

            Vector3d position = wpData.pqsCity.transform.position;
            LoggingUtil.LogVerbose(this, "    pqs city position = " + position);

            // Translate by the PQS offset (inverse transform of coordinate system)
            Vector3d v = wpData.pqsOffset;
            Vector3d i = wpData.pqsCity.transform.right;
            Vector3d j = wpData.pqsCity.transform.forward;
            Vector3d k = wpData.pqsCity.transform.up;
            LoggingUtil.LogVerbose(this, "    i, j, k = " + i + ", " + j + "," + k);
            LoggingUtil.LogVerbose(this, "    pqs transform = " + wpData.pqsCity.transform);
            LoggingUtil.LogVerbose(this, "    parent transform = " + wpData.pqsCity.transform.parent);
            Vector3d offsetPos = new Vector3d(
                (j.y * k.z - j.z * k.y) * v.x + (i.z * k.y - i.y * k.z) * v.y + (i.y * j.z - i.z * j.y) * v.z,
                (j.z * k.x - j.x * k.z) * v.x + (i.x * k.z - i.z * k.x) * v.y + (i.z * j.x - i.x * j.z) * v.z,
                (j.x * k.y - j.y * k.x) * v.x + (i.y * k.x - i.x * k.y) * v.y + (i.x * j.y - i.y * j.x) * v.z
            );
            offsetPos *= (i.x * j.y * k.z) + (i.y * j.z * k.x) + (i.z * j.x * k.y) - (i.z * j.y * k.x) - (i.y * j.x * k.z) - (i.x * j.z * k.y);
            wpData.waypoint.latitude = body.GetLatitude(position + offsetPos);
            wpData.waypoint.longitude = body.GetLongitude(position + offsetPos);
            LoggingUtil.LogVerbose(this, "    resulting lat, lon = (" + wpData.waypoint.latitude + ", " + wpData.waypoint.longitude + ")");
        }
Example #44
0
        Vector3d WorldPositionToGeoCoords(Vector3d worldPosition, CelestialBody body)
        {
            if(!body)
            {
                Debug.LogWarning ("WorldPositionToGeoCoords body is null");
                return Vector3d.zero;
            }

            double lat = body.GetLatitude(worldPosition);
            double longi = body.GetLongitude(worldPosition);
            double alt = body.GetAltitude(worldPosition);
            return new Vector3d(lat,longi,alt);
        }
Example #45
0
        public static Coordinates GetMouseCoordinates(CelestialBody body)
        {
            Ray mouseRay = PlanetariumCamera.Camera.ScreenPointToRay(Input.mousePosition);
            mouseRay.origin = ScaledSpace.ScaledToLocalSpace(mouseRay.origin);
            Vector3d relOrigin = mouseRay.origin - body.position;
            Vector3d relSurfacePosition;
            double curRadius = body.pqsController.radiusMax;
            double lastRadius = 0;
            double error = 0;
            int loops = 0;
            float st = Time.time;
            while (loops < 50)
            {
                if (PQS.LineSphereIntersection(relOrigin, mouseRay.direction, curRadius, out relSurfacePosition))
                {
                    Vector3d surfacePoint = body.position + relSurfacePosition;
                    double alt = body.pqsController.GetSurfaceHeight(QuaternionD.AngleAxis(body.GetLongitude(surfacePoint), Vector3d.down) * QuaternionD.AngleAxis(body.GetLatitude(surfacePoint), Vector3d.forward) * Vector3d.right);
                    error = Math.Abs(curRadius - alt);
                    if (error < (body.pqsController.radiusMax - body.pqsController.radiusMin) / 100)
                    {
                        return new Coordinates(body.GetLatitude(surfacePoint), MuUtils.ClampDegrees180(body.GetLongitude(surfacePoint)));
                    }
                    else
                    {
                        lastRadius = curRadius;
                        curRadius = alt;
                        loops++;
                    }
                }
                else
                {
                    if (loops == 0)
                    {
                        break;
                    }
                    else
                    { // Went too low, needs to try higher
                        curRadius = (lastRadius * 9 + curRadius) / 10;
                        loops++;
                    }
                }
            }

            return null;
        }