Esempio n. 1
0
        private Point2D GetShotTargetPosition(
            ChickenUnitState unitState,
            ChickenViewData enemyUnit)
        {
            if (!_features.IsAnySet(Features.PredictiveShot))
            {
                return(enemyUnit.Position);
            }

            Point2D?predictiveShotTargetPosition = null;

            var enemyUnitBeakTip   = GameHelper.GetBeakTipPosition(enemyUnit.Position, enemyUnit.BeakAngle);
            var enemyUnitVector    = enemyUnitBeakTip - enemyUnit.Position;
            var enemyToMeVector    = unitState.Position - enemyUnit.Position;
            var cosAlpha           = enemyUnitVector.GetAngleCosine(enemyToMeVector);
            var distanceToEnemySqr = enemyToMeVector.GetLengthSquared();
            var distanceToEnemy    = distanceToEnemySqr.Sqrt();

            if (!(cosAlpha.Abs() - 1f).IsZero())
            {
                var equation = new QuadraticEquation(
                    GameConstants.ShotToChickenRectilinearSpeedRatio.Sqr() - 1f,
                    2f * distanceToEnemy * cosAlpha,
                    -distanceToEnemySqr);

                float d1, d2;
                if (equation.GetRoots(out d1, out d2) > 0)
                {
                    var d = new[] { d1, d2 }
                    .Where(item => item > 0f)
                    .OrderBy(item => item)
                    .FirstOrDefault();
                    if (d.IsPositive())
                    {
                        predictiveShotTargetPosition = GameHelper.GetNewPosition(
                            enemyUnit.Position,
                            enemyUnit.BeakAngle,
                            d);
                    }
                }
            }

            var result = predictiveShotTargetPosition
                         ?? GameHelper.GetNewPosition(
                enemyUnit.Position,
                enemyUnit.BeakAngle,
                GameConstants.ChickenUnit.DefaultRectilinearSpeed);

            return(result);
        }
        //---------------------------------------------------------------------------------------------
        //Координаты x, соответствующие координате y
        public double[] GetCoordinatesX(double y)
        {
            double a = 1;
            double b = -2 * this.centre.X;
            double c =
                (y - this.centre.Y) * (y - this.centre.Y) +
                this.centre.X * this.centre.X -
                radius * radius;
            QuadraticEquation quadraticEquation = new QuadraticEquation(a, b, c);

            Complex[] complexRoots = quadraticEquation.GetRoots();
            double[]  realRoots    = NumbersManager.GetRealParts(complexRoots);
            return(realRoots);
        }
Esempio n. 3
0
        //-----------------------------------------------------------------------------------------------
        //Точки пересечения прямой и окружности
        public static Point2D[] IntersectionPointsOfLineAndCircle(
            LineDescriptor lineDescriptor,
            CircleDescriptor circleDescriptor
            )
        {
            double a = lineDescriptor.CoefficientOfX;
            double b = lineDescriptor.CoefficientOfY;
            double c = lineDescriptor.AbsoluteTerm;

            double xc = circleDescriptor.Centre.X;
            double yc = circleDescriptor.Centre.Y;
            double r  = circleDescriptor.Radius;

            double x1, x2, y1, y2;

            if (b != 0)
            {
                double koefficientA = 1 + ((a * a) / (b * b));
                double koefficientB = 2 * (c * a / (b * b) + yc * a / b - xc);
                double koefficientC =
                    xc * xc + yc * yc + (c * c) / (b * b) + 2 * yc * c / b - r * r;
                QuadraticEquation quadraticEquation =
                    new QuadraticEquation(koefficientA, koefficientB, koefficientC);

                Complex[] complexRoots = quadraticEquation.GetRoots();
                double[]  realRoots    = NumbersManager.GetRealParts(complexRoots);

                x1 = realRoots[0];
                y1 = (-c - a * x1) / b;
                x2 = realRoots[1];
                y2 = (-c - a * x2) / b;
            }
            else
            {
                double x = -c / a;
                x1 = x2 = x;
                double[] coordinatesY = circleDescriptor.GetCoordinatesY(x);
                y1 = coordinatesY[0];
                y2 = coordinatesY[1];
            }

            Point2D intersectionPoint1 = new Point2D(x1, y1);
            Point2D intersectionPoint2 = new Point2D(x2, y2);

            Point2D[] intersectionPoints = new Point2D[] { intersectionPoint1, intersectionPoint2 };

            return(intersectionPoints);
        }