/// <summary>
        /// Checks the conic area defined by a radar sensor for detectable objects.
        /// </summary>
        /// <returns>Distance to the closest object.</returns>
        public static double scanCone(Radar radar, List<SimulatorObject> objList)
        {
            double distance = radar.MaxRange;
            double new_distance;
			double heading=radar.Owner.Heading;
			Point2D point=new Point2D(radar.Owner.Location.X,radar.Owner.Location.Y);
			
            double startAngle = radar.StartAngle + heading;
            double endAngle = radar.EndAngle + heading;
            double twoPi = 2 * Math.PI;

            if (startAngle < 0)
            {
                startAngle += twoPi;
            }
            else if (startAngle > twoPi)
            {
                startAngle -= twoPi;
            }
            if (endAngle < 0)
            {
                endAngle += twoPi;
            }
            else if (endAngle > twoPi)
            {
                endAngle -= twoPi;
            }

            foreach (SimulatorObject obj in objList)
            {
                bool found = false;

                if (obj == radar.Owner)
                    continue;

                new_distance = point.distance(obj.Location);
                     
                double angle = Math.Atan2(obj.Location.Y - point.Y, obj.Location.X - point.X);
                       
                if (angle < 0)
                {
                    angle += Utilities.twoPi;
                }

                if (endAngle < startAngle)
                {
                    if ((angle >= startAngle && angle <= Math.PI * 2) || (angle >= 0 && angle <= endAngle))
                    {
                        found = true;
                    }
                }

                else if ((angle >= startAngle && angle <= endAngle))
                    found = true;

                if (found)
                {
                    if (new_distance < distance)
                    {
                        distance = new_distance;
                    }
                }
            }

            return distance;
        }
Example #2
0
        /// <summary>
        /// Checks the conic area defined by a radar sensor for detectable objects.
        /// </summary>
        /// <returns>Distance to the closest object.</returns>
        public static double scanCone(Radar radar, List <SimulatorObject> objList)
        {
            double  distance = radar.MaxRange;
            double  new_distance;
            double  heading = radar.Owner.Heading;
            Point2D point   = new Point2D(radar.Owner.Location.X, radar.Owner.Location.Y);

            double startAngle = radar.StartAngle + heading;
            double endAngle   = radar.EndAngle + heading;
            double twoPi      = 2 * Math.PI;

            if (startAngle < 0)
            {
                startAngle += twoPi;
            }
            else if (startAngle > twoPi)
            {
                startAngle -= twoPi;
            }
            if (endAngle < 0)
            {
                endAngle += twoPi;
            }
            else if (endAngle > twoPi)
            {
                endAngle -= twoPi;
            }

            foreach (SimulatorObject obj in objList)
            {
                bool found = false;

                if (obj == radar.Owner)
                {
                    continue;
                }

                new_distance = point.distance(obj.Location);

                double angle = Math.Atan2(obj.Location.Y - point.Y, obj.Location.X - point.X);

                if (angle < 0)
                {
                    angle += Utilities.twoPi;
                }

                if (endAngle < startAngle)
                {
                    if ((angle >= startAngle && angle <= Math.PI * 2) || (angle >= 0 && angle <= endAngle))
                    {
                        found = true;
                    }
                }

                else if ((angle >= startAngle && angle <= endAngle))
                {
                    found = true;
                }

                if (found)
                {
                    if (new_distance < distance)
                    {
                        distance = new_distance;
                    }
                }
            }

            return(distance);
        }