예제 #1
0
        public void TestAngleBetweenPoints()
        {
            Vector2 origin = Vector2.Zero;

            Vector2 point3        = new Vector2(100, 0);
            float   actualAngle   = MathUtils.AngleBetweenPoints(origin, point3);
            float   expectedAngle = MathHelper.PiOver2;

            Assert.AreEqual(expectedAngle, actualAngle);

            Vector2 point2 = new Vector2(0, 100);

            actualAngle   = MathUtils.AngleBetweenPoints(origin, point2);
            expectedAngle = MathHelper.Pi;
            Assert.AreEqual(expectedAngle, actualAngle);

            Vector2 point4 = new Vector2(-100, 0);

            actualAngle   = MathUtils.AngleBetweenPoints(origin, point4);
            expectedAngle = 1.5f * MathHelper.Pi;
            Assert.AreEqual(expectedAngle, actualAngle);

            Vector2 point5 = new Vector2(0, -100);

            actualAngle   = MathUtils.AngleBetweenPoints(origin, point5);
            expectedAngle = 0;
            Assert.AreEqual(expectedAngle, actualAngle);
        }
예제 #2
0
        /// <summary>
        /// Sets the rotation of the turret to be so that it is pointing at the inputted world space position.
        /// </summary>
        /// <param name="worldSpaceTarget"></param>
        public void RotateToTarget(Vector2 worldSpaceTarget)
        {
            float desiredAngle = MathUtils.AngleBetweenPoints(WorldPosition, worldSpaceTarget);

            DebugUtils.AssertNotNull(Parent);
            LocalRotation = desiredAngle - Parent.WorldRotation;
        }
예제 #3
0
        /// <summary>
        /// Uses our fixed and target position to recalculat the angle, position and size of this line.
        /// This will draw the line between the fixed and target positions.
        /// </summary>
        private void CalculatePositionAndAngle()
        {
            float targetAngle = MathUtils.AngleBetweenPoints(FixedPosition, TargetPosition);

            LocalRotation = targetAngle;
            LocalPosition = (FixedPosition + TargetPosition) * 0.5f;
            Size          = new Vector2(Size.X, (FixedPosition - TargetPosition).Length());
        }
예제 #4
0
        public Projectile(GameObject target, Vector2 worldPosition, ProjectileData projectileData) :
            base(worldPosition, "")
        {
            Target = target;
            Data   = projectileData;

            float angle = MathUtils.AngleBetweenPoints(worldPosition, target.WorldPosition);

            LocalRotation = angle;
        }
예제 #5
0
        /// <summary>
        /// Searches for a ship to attack, attacks it and resets our timer for between attacks.
        /// Inputted ship guaranteed to be able to fire at this point.
        /// </summary>
        /// <param name="attackingShip"></param>
        private void AttackOpponentShip(Ship attackingShip)
        {
            foreach (CardShipPair pair in BattleScreen.Board.NonActivePlayerBoardSection.GameBoardSection.ShipCardControl)
            {
                DebugUtils.AssertNotNull(pair.Ship.DamageModule);
                if (pair.Ship.DamageModule.Health > 0)
                {
                    float targetAngle = MathUtils.AngleBetweenPoints(attackingShip.WorldPosition, pair.CardObject.WorldPosition);
                    attackingShip.Turret.LocalRotation = targetAngle - attackingShip.WorldRotation;

                    // Attack the selected ship
                    attackingShip.Turret.Attack(pair.Ship, false);
                    break;
                }
            }

            currentTimeBetweenAttacks = 0;
        }
예제 #6
0
        /// <summary>
        /// Updates our bullet's position and kill's it if it has collided with our target
        /// </summary>
        /// <param name="elapsedGameTime"></param>
        public override void Update(float elapsedGameTime)
        {
            base.Update(elapsedGameTime);

            // The missile may have been killed by a lifetime module already so we should not progress any further with it's updating
            if (!IsAlive)
            {
                return;
            }

            if (!finishedJutting)
            {
                LocalPosition = Vector2.Lerp(LocalPosition, jutFinishPosition, 5 * elapsedGameTime);
                if ((LocalPosition - jutFinishPosition).LengthSquared() < 10)
                {
                    finishedJutting = true;
                    Engine.Show();      // Have to call show because by default our Engine is hidden (since it is a CardObject)
                    FiringSFX.Play();
                }
            }
            else
            {
                Vector2 diff = Target.WorldPosition - WorldPosition;
                diff.Normalize();

                float angle = MathUtils.AngleBetweenPoints(WorldPosition, Target.WorldPosition);
                LocalRotation = angle;

                LocalPosition += diff * 700 * elapsedGameTime;

                DebugUtils.AssertNotNull(Collider);
                DebugUtils.AssertNotNull(Target.Collider);
                if (Collider.CheckCollisionWith(Target.Collider))
                {
                    // Kills the projectile if it has collided with the target
                    Die();

                    ExplosionSFX.Play();
                    Target.OnCollisionWith(this);
                }
            }
        }
        protected async override Task <TripDetectionContext> ProcessInternal(TripDetectionContext input)
        {
            var tripSegments = new List <TripSegmentBase>();

            foreach (var segment in input.TripSegments)
            {
                if (!segment.IsMovingSegment)
                {
                    tripSegments.Add(segment);
                    continue;
                }

                var currentStopCluster   = new StoppedSegment();
                var currentMovingCluster = new MovingSegment();

                State previousState = State.Stopped;
                State currentState  = State.Unknown;

                var points = segment.Points;
                currentStopCluster.Points.Add(points.First());

                // Loop over points and extract segments
                for (var i = 1; i < points.Count - 1; i++)
                {
                    var previousTrackingPoint = points[i - 1];
                    var currentTrackingPoint  = points[i];
                    var nextTrackingPoint     = points[i + 1];

                    double cumulativeAngle = 0;

                    for (int j = i - this.arcWindowSize / 2; j <= i + this.arcWindowSize / 2; j++)
                    {
                        if (j >= 0 && j < points.Count)
                        {
                            cumulativeAngle += MathUtils.AngleBetweenPoints(points[j], points[i]);
                        }
                    }

                    cumulativeAngle /= this.arcWindowSize;

                    double calculatedSpeed = MathUtils.AverageSpeed(previousTrackingPoint, currentTrackingPoint, nextTrackingPoint);

                    currentState = (calculatedSpeed <= this.maximumDwellSpeed && cumulativeAngle > this.minimumAngleDifference)
                        ? State.Stopped : State.Moving;

                    if (currentState == State.Moving)
                    {
                        currentMovingCluster.Points.Add(currentTrackingPoint);

                        if (previousState == State.Stopped)
                        {
                            if (currentStopCluster.GetDurationInSeconds() < this.minimumDwellTime)
                            {
                                tripSegments.Add(new MovingSegment(currentStopCluster.Points));
                            }
                            else
                            {
                                currentStopCluster.Points.Add(currentTrackingPoint);
                                tripSegments.Add(currentStopCluster);
                            }

                            currentStopCluster = new StoppedSegment();
                        }
                    }

                    if (currentState == State.Stopped)
                    {
                        currentStopCluster.Points.Add(currentTrackingPoint);

                        if (previousState == State.Moving)
                        {
                            currentMovingCluster.Points.Add(currentTrackingPoint);

                            tripSegments.Add(currentMovingCluster);

                            currentMovingCluster = new MovingSegment();
                        }
                    }

                    previousState = currentState;
                }


                // Add the last segment to the list of segments including the last
                // unprocessed point
                if (currentStopCluster.Points.Any())
                {
                    currentStopCluster
                    .Points
                    .Add(points[points.Count - 1]);

                    tripSegments.Add(currentStopCluster);
                }
                else if (currentMovingCluster.Points.Any())
                {
                    currentMovingCluster
                    .Points
                    .Add(points[points.Count - 1]);

                    tripSegments.Add(currentMovingCluster);
                }
            }

            input.TripSegments = tripSegments;

            return(input);
        }