Пример #1
0
        /// <summary>
        /// Returns the distance from the segment to the specified position.
        /// </summary>
        /// <param name="_position"></param>
        /// <returns></returns>
        /// <remarks>This method analyzes the relative position of the segment to the line to determine the
        /// best mathematical approach.</remarks>
        public Distance DistanceTo(Position _position)
        {
            if (m_Start.Equals(m_End))
            {
                return(_position.DistanceTo(m_Start));
            }
            Position delta = m_End.Subtract(m_Start);
            double   ratio = ((_position.Longitude.DecimalDegrees - m_Start.Longitude.DecimalDegrees)
                              * delta.Longitude.DecimalDegrees +
                              (_position.Latitude.DecimalDegrees - m_Start.Latitude.DecimalDegrees)
                              * delta.Latitude.DecimalDegrees) /
                             (delta.Longitude.DecimalDegrees * delta.Longitude.DecimalDegrees +
                              delta.Latitude.DecimalDegrees
                              * delta.Latitude.DecimalDegrees);

            if (ratio < 0)
            {
                return(_position.DistanceTo(m_Start));
            }

            if (ratio > 1)
            {
                return(_position.DistanceTo(m_End));
            }

            Position destination = new Position(
                new Latitude((1 - ratio) * m_Start.Latitude.DecimalDegrees + ratio * m_End.Latitude.DecimalDegrees),
                new Longitude((1 - ratio) * m_Start.Longitude.DecimalDegrees + ratio * m_End.Longitude.DecimalDegrees));

            return(_position.DistanceTo(destination));
        }
Пример #2
0
        /// <summary>
        /// Returns the distance from the segment to the specified position.
        /// </summary>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        /// <remarks>This method analyzes the relative position of the segment to the line to determine the
        /// best mathematical approach.</remarks>
        public Distance DistanceTo(Position position)
        {
            if (_start.Equals(_end))
            {
                return(position.DistanceTo(_start));
            }
            Position delta = _end.Subtract(_start);
            double   ratio = ((position.Longitude.DecimalDegrees - _start.Longitude.DecimalDegrees)
                              * delta.Longitude.DecimalDegrees + (position.Latitude.DecimalDegrees - _start.Latitude.DecimalDegrees)
                              * delta.Latitude.DecimalDegrees) / (delta.Longitude.DecimalDegrees * delta.Longitude.DecimalDegrees + delta.Latitude.DecimalDegrees
                                                                  * delta.Latitude.DecimalDegrees);

            if (ratio < 0)
            {
                return(position.DistanceTo(_start));
            }
            if (ratio > 1)
            {
                return(position.DistanceTo(_end));
            }
            Position destination = new Position(
                new Latitude((1 - ratio) * _start.Latitude.DecimalDegrees + ratio * _end.Latitude.DecimalDegrees),
                new Longitude((1 - ratio) * _start.Longitude.DecimalDegrees + ratio * _end.Longitude.DecimalDegrees));

            return(position.DistanceTo(destination));
        }
Пример #3
0
        /// <summary>
        /// Returns the distance from the segment to the specified position.
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        /// <remarks>This method analyzes the relative position of the segment to the line to determine the
        /// best mathematical approach.</remarks>
        public Distance DistanceTo(Position position)
        {
            if (_Start.Equals(_End))
            {
                return(position.DistanceTo(_Start));
            }
            Position Delta = _End.Subtract(_Start);
            double   Ratio = ((position.Longitude.DecimalDegrees - _Start.Longitude.DecimalDegrees)
                              * Delta.Longitude.DecimalDegrees + (position.Latitude.DecimalDegrees - _Start.Latitude.DecimalDegrees)
                              * Delta.Latitude.DecimalDegrees) / (Delta.Longitude.DecimalDegrees * Delta.Longitude.DecimalDegrees + Delta.Latitude.DecimalDegrees
                                                                  * Delta.Latitude.DecimalDegrees);

            if (Ratio < 0)
            {
                return(position.DistanceTo(_Start));
            }
            else if (Ratio > 1)
            {
                return(position.DistanceTo(_End));
            }
            else
            {
                Position Destination = new Position(
                    new Latitude((1 - Ratio) * _Start.Latitude.DecimalDegrees + Ratio * _End.Latitude.DecimalDegrees),
                    new Longitude((1 - Ratio) * _Start.Longitude.DecimalDegrees + Ratio * _End.Longitude.DecimalDegrees));
                return(position.DistanceTo(Destination));
            }
        }
Пример #4
0
        internal double InRangeOfLeader(Player player)
        {
            Position leaderPosition = WorldManager.GetPlayerByGuidId(FellowshipLeaderGuid).Location;
            Position memberPosition = player.Location;

            if (Math.Abs(memberPosition.DistanceTo(leaderPosition)) <= 600)
            {
                return(1);
            }

            return(1 - ((Math.Abs(memberPosition.DistanceTo(leaderPosition)) - 600) / 600));
        }
Пример #5
0
    // helper ////////
    private static void helper4GetNextStupidTile(Position from, Position to, ref Position nextPos, ref int minDistance)
    {
        if (Dungeon.Instance.GetTile(from) == null)
        {
            return;
        }

        if (from.DistanceTo(to) < minDistance && !Dungeon.Instance.GetTile(from).IsBlockPath)
        {
            nextPos     = from;
            minDistance = from.DistanceTo(to);
        }
    }
Пример #6
0
        private ITrackSegment SimplifySegment(ITrackSegment segment)
        {
            GeoFramework.Position prevPosition = new Position();
            List<IWayPoint> wayPointsToRemove = new List<IWayPoint>();
            foreach (IWayPoint wayPoint in segment.SegmentWaypoints)
            {
                Position position = new Position(wayPoint.Latitude.ToString(), wayPoint.Longitude.ToString());

                if (position.DistanceTo(prevPosition) < _SimplifyingDistance)
                {
                    wayPointsToRemove.Add(wayPoint);

                }
                else
                {
                    prevPosition = position;
                }
                
            }

            foreach (IWayPoint wayPoint in wayPointsToRemove)
            {
                segment.SegmentWaypoints.Remove(wayPoint);
            }
            return segment;

        }
Пример #7
0
    private Vector2 GetVelocity()
    {
        var velocity = new Vector2();

        if (Position.DistanceTo(_target) > 10)
        {
            velocity = (_target - Position).Normalized() * Speed;
        }
        else
        {
            velocity = new Vector2();
        }


        if (Input.IsActionPressed(InputKeys.Right))
        {
            velocity.x += 1;
        }
        if (Input.IsActionPressed(InputKeys.Left))
        {
            velocity.x -= 1;
        }
        if (Input.IsActionPressed(InputKeys.Down))
        {
            velocity.y += 1;
        }
        if (Input.IsActionPressed(InputKeys.Up))
        {
            velocity.y -= 1;
        }
        return(velocity);
    }
Пример #8
0
    Vector2 cohese(List <IndividualParticle> neighbors)
    {
        Vector2 sum   = new Vector2(0, 0);
        int     count = 0;

        foreach (IndividualParticle particle in neighbors)
        {
            float distanceTo = Position.DistanceTo(particle.Position);
            if (distanceTo > 0 && distanceTo < neighborDistance)
            {
                sum += particle.Position;
                count++;
            }
        }
        if (count > 0)
        {
            sum /= count;
            Vector2 desiredDir = sum - Position;
            desiredDir  = desiredDir.Normalized();
            desiredDir *= maxSpeed;
            Vector2 steer = desiredDir - velocity;
            steer = steer.Clamped(maxForce);
            return(steer);
        }

        return(new Vector2(0, 0));
    }
Пример #9
0
    Vector2 align(List <IndividualParticle> neighbors)
    {
        Vector2 sum   = new Vector2(0, 0);
        int     count = 0;

        foreach (IndividualParticle particle in neighbors)
        {
            float distanceTo = Position.DistanceTo(particle.Position);
            if (distanceTo > 0 && distanceTo < neighborDistance)
            {
                sum += particle.velocity;
                count++;
            }
        }
        if (count > 0)
        {
            sum /= (float)count;
            sum  = sum.Normalized();
            sum *= maxSpeed;
            Vector2 steer = sum - velocity;
            steer = steer.Clamped(maxForce);
            return(steer);
        }
        return(new Vector2(0, 0));
    }
Пример #10
0
    Vector2 seperate(List <IndividualParticle> neighbors)
    {
        Vector2 steer = new Vector2();
        int     count = 0;

        foreach (IndividualParticle particle in neighbors)
        {
            float distanceTo = Position.DistanceTo(particle.Position);
            if (distanceTo > 0 && distanceTo < minimumSeperation)
            {
                Vector2 difference = Position - particle.Position;
                difference = difference.Normalized();
                difference = difference / distanceTo;
                steer     += difference;
                count++;
            }
        }
        if (count > 0)
        {
            steer = steer / (float)count;
        }
        if (steer.Length() > 0)
        {
            steer  = steer.Normalized();
            steer *= maxSpeed;
            steer -= velocity;
            steer  = steer.Clamped(maxForce);
        }
        return(steer);
    }
Пример #11
0
        private ITrackSegment SimplifySegment(ITrackSegment segment)
        {
            GeoFramework.Position prevPosition      = new Position();
            List <IWayPoint>      wayPointsToRemove = new List <IWayPoint>();

            foreach (IWayPoint wayPoint in segment.SegmentWaypoints)
            {
                Position position = new Position(wayPoint.Latitude.ToString(), wayPoint.Longitude.ToString());

                if (position.DistanceTo(prevPosition) < _SimplifyingDistance)
                {
                    wayPointsToRemove.Add(wayPoint);
                }
                else
                {
                    prevPosition = position;
                }
            }

            foreach (IWayPoint wayPoint in wayPointsToRemove)
            {
                segment.SegmentWaypoints.Remove(wayPoint);
            }
            return(segment);
        }
Пример #12
0
        public bool AdvancePath(TimeSpan time, bool faceRoute = true)
        {
            var modifier = time.TotalSeconds * Speed;

            if (CurrentPath != null)
            {
                // Advance along path
                var target = (Vector3)CurrentPath.Waypoints[CurrentPath.Index];
                target.Y = Position.Y; // TODO: Find better way of doing this
                if (faceRoute)
                {
                    Face(target);
                }
                var lookAt = Vector3.Forwards.Transform(Matrix.CreateRotationY(MathHelper.ToRadians(-(Yaw - 180) + 180)));
                lookAt  *= modifier;
                Velocity = new Vector3(lookAt.X, Velocity.Y, lookAt.Z);
                if (Position.DistanceTo(target) < 0.1)
                {
                    CurrentPath.Index++;
                    if (CurrentPath.Index >= CurrentPath.Waypoints.Count)
                    {
                        CurrentPath = null;
                        if (PathComplete != null)
                        {
                            PathComplete(this, null);
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }
Пример #13
0
        public FlightPlan GetFlightPlan()
        {
            var waypoints = _getFlightPlan().Where(IsValidWaypoint).ToList();

            if (waypoints.Count == 0)
            {
                return(null);
            }
            var envelope = GeometryFactory.Default.CreateMultiPoint(waypoints
                                                                    .Select(l => new Coordinate(l.lng, l.lat))).Envelope;
            var center         = envelope.Center();
            var minPos         = new Position(new Latitude(envelope.Minimum.Y), new Longitude(envelope.Minimum.X));
            var maxPos         = new Position(new Latitude(envelope.Maximum.Y), new Longitude(envelope.Maximum.X));
            var boundingRadius = (int)Math.Ceiling(minPos.DistanceTo(maxPos).ToMeters().Value);

            return(new FlightPlan(waypoints.Select(f => new FlightPlanWaypoint
            {
                Latitude = f.lat,
                Longitude = f.lng,
                Altitude = (int)f.alt,
            }))
            {
                CenterLongitude = center.X,
                CenterLatitude = center.Y,
                BoundingRadius = boundingRadius
            });
        }
Пример #14
0
    public void FindLurchTarget()
    {
        var playerList = GetTree().GetNodesInGroup("Players");

        findTargetSfx.Stop();
        if (playerList.Count > 0)
        {
            Player player    = playerList[0] as Player;
            var    lurchTime = 1f;
            if (Position.DistanceTo(player.GlobalPosition) > lurchTime * MaxVelocity)
            {
                lurchDirection = Position.DirectionTo(player.GlobalPosition);
                lurchTarget    = Position + lurchDirection * MaxVelocity;
            }
            else
            {
                lurchDirection = Position.DirectionTo(player.GlobalPosition);
                lurchTarget    = player.GlobalPosition;
                lurchTime     *= GlobalPosition.DistanceTo(lurchTarget) / MaxVelocity;
            }
            sprite.FlipH = lurchDirection.x < 0;
            tween.Remove(this, "global_position");
            tween.InterpolateProperty(this, "global_position", Position, lurchTarget, lurchTime, easeType: Tween.EaseType.Out);
            tween.Start();
        }
    }
Пример #15
0
            public void CalculateVertical()
            {
                Position start = new Position(0, 0);
                Position end   = new Position(0, 5);

                Assert.AreEqual(5, start.DistanceTo(end));
            }
Пример #16
0
            public void CalculateHorizontal()
            {
                Position start = new Position(0, 0);
                Position end   = new Position(5, 0);

                Assert.AreEqual(5, start.DistanceTo(end));
            }
Пример #17
0
            public void CalculateDiagonal()
            {
                Position start = new Position(0, 0);
                Position end   = new Position(3, 4);

                Assert.AreEqual(5, start.DistanceTo(end));
            }
Пример #18
0
    public override void _Process(float delta)
    {
        //If we detect a collision we snap the player back to his previous position and reset teh target position as well
        if (ray.IsColliding())
        {
            Position       = lastPosition;
            targetPosition = lastPosition;
        }
        else
        {
            GD.Print("Before Position = " + Position);
            //Move the position towards the move drection
            Position += moveDir * speed * delta;
            GD.Print("After Position = " + Position);

            if (Position.DistanceTo(lastPosition) >= tileSize - speed * delta)
            {
                Position = targetPosition;
            }
        }
        //If we are not in position
        if (Position == targetPosition)
        {
            Move();
            lastPosition    = Position;
            targetPosition += moveDir * tileSize;
        }
    }
Пример #19
0
        public Vector3d ComputeDesiredVelocity()
        {
            foreach (Circle attractor in FlockSystem.Attractors)
            {
                if (AttractToNearestPt)
                {
                    double   distanceToAttractor = Position.DistanceTo(ClosestPoint);
                    Vector3d attraction          = ClosestPoint - Position;
                    // Attraction gets stronger as the agent gets closer to the attractor
                    attraction *= (attraction.Length / distanceToAttractor);

                    attraction      *= Multiplier * attractor.Radius;
                    DesiredVelocity += attraction;
                }
                else
                {
                    double   distanceToAttractor = Position.DistanceTo(attractor.Center);
                    Vector3d attraction          = attractor.Center - Position;
                    // Attraction gets stronger as the agent gets closer to the attractor
                    attraction *= (attraction.Length / distanceToAttractor);
                    // Attraction strength is also proportional to the radius of the attractor circle/sphere
                    // This allows the user to tweak the Attraction strength by tweaking the radius
                    attraction      *= Multiplier * attractor.Radius;
                    DesiredVelocity += attraction;
                }
            }
            return(DesiredVelocity);
        }
Пример #20
0
    public Vector2 cohesion()
    {
        int     membersCount  = 0;
        Vector2 sumOfPosition = new Vector2();

        foreach (Tank member in members)
        {
            // It is possible that member got destroy before this API request
            if (!IsInstanceValid(member))
            {
                continue;
            }

            float distance = Position.DistanceTo(member.Position);

            if (distance > 0 && distance <= cohesionDistance)
            {
                sumOfPosition += member.Position;
                membersCount++;
            }
        }
        if (membersCount != 0)
        {
            return(seek(sumOfPosition / (float)membersCount));
        }

        return(sumOfPosition);
    }
Пример #21
0
        public override Event Act()
        {
            if (!CanShoot && Position.DistanceTo(Enemy.Position) < 2 && _remainingLandmines > 0)
            {
                _remainingLandmines--;
                return(new PlantLandmineEvent(this));
            }

            if (MainBody.Armor < 15 || !CanShoot) // go into defensive mode
            {
                var destination = GetNeighourVisitableTiles()
                                  .Where(p => !HasShootingLine(p))
                                  .OrderByDescending(p => p.DistanceTo(Enemy.Position))
                                  .FirstOrDefault();

                if (destination != null)
                {
                    return(new TankMoveEvent(this, destination - Position));
                }
            }

            if (CanShoot && HasShootingLine(Position)) // if it can shoot, shoot
            {
                return(new TankHitEvent(this, Enemy));
            }
            else // if there is nothing to do, move towards the enemy
            {
                var moveTo = DecideMovePosition();
                return(new TankMoveEvent(this, moveTo));
            }
        }
Пример #22
0
        public bool AdvancePath(TimeSpan time, bool faceRoute = true)
        {
            var modifier = time.TotalSeconds * Speed;

            if (CurrentPath != null)
            {
                // Advance along path
                var target = CurrentPath.Waypoints[CurrentPath.Index].AsVector3();
                target  += new Vector3((float)(Size.Width / 2), 0, (float)(Size.Depth / 2)); // Center it
                target.Y = Position.Y;                                                       // TODO: Find better way of doing this
                if (faceRoute)
                {
                    Face(target);
                }
                var lookAt = Directions.Forwards.Transform(Matrix.CreateRotationY((float)MathHelper.ToRadians(-(Yaw - 180) + 180)));
                lookAt  *= (float)modifier;
                Velocity = new Vector3(lookAt.X, Velocity.Y, lookAt.Z);
                if (Position.DistanceTo(target) < Velocity.Distance())
                {
                    Position = target;
                    Velocity = Vector3.Zero;
                    CurrentPath.Index++;
                    if (CurrentPath.Index >= CurrentPath.Waypoints.Count)
                    {
                        CurrentPath = null;
                        PathComplete?.Invoke(this, null);
                        return(true);
                    }
                }
            }

            return(false);
        }
Пример #23
0
    public Vector2 alignment()
    {
        int     membersCount  = 0;
        Vector2 sumOfVelocity = new Vector2();

        foreach (Tank member in members)
        {
            // It is possible that member got destroy before this API request
            if (!IsInstanceValid(member))
            {
                continue;
            }

            float distance = Position.DistanceTo(member.Position);

            if (distance > 0 && distance <= alignmentDistance)
            {
                sumOfVelocity += member.Velocity;
                membersCount++;
            }
        }
        if (membersCount != 0)
        {
            return(((sumOfVelocity / (float)membersCount).Normalized() * MaxSpeed - Velocity).Clamped(maxForces));
        }

        return(sumOfVelocity);
    }
Пример #24
0
        public bool InRange(Attackable attackable, int range = 2000)
        {
            if (attackable == null || attackable.Spacemap.Id != Spacemap.Id)
            {
                return(false);
            }
            if (attackable is Character character)
            {
                if (character == null || character.Destroyed)
                {
                    return(false);
                }

                if (this is Player player)
                {
                    if (Duel.InDuel(player) && player.Storage.Duel?.GetOpponent(player) != attackable)
                    {
                        return(false);
                    }
                }
            }
            if (range == -1 || attackable.Spacemap.Options.RangeDisabled)
            {
                return(true);
            }
            return(attackable.Id != Id && Position.DistanceTo(attackable.Position) <= range);
        }
Пример #25
0
    public Vector2 separation()
    {
        int     membersCount    = 0;
        Vector2 separationForce = new Vector2();

        foreach (Tank member in members)
        {
            // It is possible that member got destroy before this API request
            if (!IsInstanceValid(member))
            {
                continue;
            }

            float distance = Position.DistanceTo(member.Position);

            if (distance > 0 && distance <= separationDistance)
            {
                separationForce += (Position - member.Position).Normalized() / distance;
                membersCount++;
            }
        }
        if (membersCount != 0)
        {
            return(((separationForce / (float)membersCount).Normalized() * MaxSpeed - Velocity).Clamped(maxForces));
        }

        return(separationForce);
    }
Пример #26
0
        public override void _UnhandledInput(InputEvent @event)
        {
            if (Input.IsActionJustPressed("open_chest"))
            {
                var chests = _chestDetector.GetOverlappingBodies();
                if (chests.Count > 0)
                {
                    Node2D closestChest    = null;
                    var    closestDistance = float.MaxValue;
                    foreach (Node2D chest in chests)
                    {
                        var distance = Position.DistanceTo(chest.Position);
                        if (distance < closestDistance)
                        {
                            closestDistance = distance;
                            closestChest    = chest;
                        }
                    }

                    EmitSignal(nameof(OpenChestInputReceived), closestChest);
                }
            }

            if (Input.IsActionJustPressed("guild_hall_desk_interact"))
            {
                var desks = _deskDetector.GetOverlappingBodies();
                if (desks.Count == 0)
                {
                    return;
                }

                var desk = desks[0];
                EmitSignal(nameof(GuildHallDeskInputReceived), desk);
            }
        }
Пример #27
0
    Vector2 avoid(List <Obstacle> obstacles)
    {
        Vector2  viewAhead      = Position + velocity.Normalized() * seeAheadDistance;
        Vector2  viewAhead2     = viewAhead * 0.5f;
        Vector2  avoidanceForce = new Vector2(0, 0);
        Obstacle closest        = null;

        foreach (Obstacle o in obstacles)
        {
            if (viewAhead.DistanceTo(o.Position) <= o.radius || viewAhead2.DistanceTo(o.Position) <= o.radius)
            {
                if (closest == null)
                {
                    closest = o;
                }
                else if (Position.DistanceTo(o.Position) < Position.DistanceTo(closest.Position))
                {
                    closest = o;
                }
            }
        }
        if (closest != null)
        {
            avoidanceForce  = viewAhead - closest.Position;
            avoidanceForce  = avoidanceForce.Normalized();
            avoidanceForce *= maxAvoidForce;
        }
        return(avoidanceForce);
    }
Пример #28
0
        public bool AdvancePath(TimeSpan time, bool faceRoute = true)
        {
            var modifier = time.TotalSeconds * Speed;

            if (CurrentPath != null)
            {
                // Advance along path
                var target = (Vector3)CurrentPath.Waypoints[CurrentPath.Index];
                target.Y = Position.Y; // TODO: Find better way of doing this
                var diff = target - Position;
                diff *= modifier;
                if (faceRoute)
                {
                    Face(target);
                }
                Position += diff;
                if (Position.DistanceTo(target) < 0.1)
                {
                    CurrentPath.Index++;
                    if (CurrentPath.Index >= CurrentPath.Waypoints.Count)
                    {
                        CurrentPath = null; // TODO: Raise path complete event or something?
                        return(true);
                    }
                }
            }
            return(false);
        }
Пример #29
0
 public override void _PhysicsProcess(float delta)
 {
     base._PhysicsProcess(delta);
     if (_state == EEnemyState.STATE_IDLE || _state == EEnemyState.STATE_STUNNED || _state == EEnemyState.STATE_DYING)
     {
         return;
     }
     if (_moveTarget != _moveTargetNone)
     {
         velocity     = (velocity + Position.DirectionTo(_moveTarget) * Acceleration * delta).Clamped(MaxVelocity * delta);
         sprite.FlipH = velocity.x < 0;
         KinematicCollision2D result = MoveAndCollide(velocity);
         if (IsInstanceValid(result))
         {
             MoveAndCollide(result.Normal.Slide(velocity));
             _moveTarget = _moveTargetNone;
             ChangeState(EEnemyState.STATE_IDLE);
         }
         if (Position.DistanceTo(_moveTarget) < 0.2f)
         {
             _moveTarget = _moveTargetNone;
             ChangeState(EEnemyState.STATE_IDLE);
         }
     }
 }
Пример #30
0
    public void _OnHurtAreaEnter(Area2D hurtArea)
    {
        // Setup explosion
        if (++pierces >= maxPierces)
        {
            smokeTrailEmitter.Emitting       = false;
            smokeTrailEmitter.Scale          = new Vector2(0, 0);
            fireTrailEmitter.Emitting        = false;
            explosionEmitter.Emitting        = true;
            explosionEmitter.ProcessMaterial = explosionMaterial;
            explosionMaterial.Gravity        = new Vector3(velocity / 2, 0f, 0f);
            exploded       = true;
            sprite.Visible = false;
            hitArea.QueueFree();
            explosionCompleteTimer.Start();
            initialBoomVelocityTimer.Start();
        }

        // Affect target(s)
        foreach (Enemy target in GetTree().GetNodesInGroup("Enemies"))
        {
            if (Position.DistanceTo(target.Position) < effectRadius)
            {
                target.Damage(damage, GlobalPosition.DirectionTo(target.GlobalPosition) * knockbackStrength);
            }
        }
    }
Пример #31
0
    public override void _Process(float delta)
    {
        //  Move towards the target and stop when close.
        if (Position.DistanceTo(_target) > 10)
        {
            velocity = (_target - Position).Normalized() * Speed;
        }
        else
        {
            velocity = Vector2.Zero;
        }

        if (velocity.Length() > 0)
        {
            velocity = velocity.Normalized() * Speed;
            animated.Play();
        }
        else
        {
            animated.Stop();
        }
        Position += velocity * delta;

        if (velocity.x != 0)
        {
            animated.Animation = "right";
            animated.FlipV     = false;
            animated.FlipH     = velocity.x < 0;
        }
        if (velocity.y != 0)
        {
            animated.Animation = "up";
            animated.FlipV     = velocity.y > 0;
        }
    }
Пример #32
0
 /// <summary>
 /// Returns the distance from the segment to the specified position.
 /// </summary>
 /// <param name="position">The position.</param>
 /// <returns></returns>
 /// <remarks>This method analyzes the relative position of the segment to the line to determine the
 /// best mathematical approach.</remarks>
 public Distance DistanceTo(Position position)
 {
     if (_start.Equals(_end))
         return position.DistanceTo(_start);
     Position delta = _end.Subtract(_start);
     double ratio = ((position.Longitude.DecimalDegrees - _start.Longitude.DecimalDegrees)
         * delta.Longitude.DecimalDegrees + (position.Latitude.DecimalDegrees - _start.Latitude.DecimalDegrees)
         * delta.Latitude.DecimalDegrees) / (delta.Longitude.DecimalDegrees * delta.Longitude.DecimalDegrees + delta.Latitude.DecimalDegrees
         * delta.Latitude.DecimalDegrees);
     if (ratio < 0)
         return position.DistanceTo(_start);
     if (ratio > 1)
         return position.DistanceTo(_end);
     Position destination = new Position(
         new Latitude((1 - ratio) * _start.Latitude.DecimalDegrees + ratio * _end.Latitude.DecimalDegrees),
         new Longitude((1 - ratio) * _start.Longitude.DecimalDegrees + ratio * _end.Longitude.DecimalDegrees));
     return position.DistanceTo(destination);
 }
Пример #33
0
        private void CalculateGhostPoint(int index, Position otherPosition, Position currentPosition, GMapRoute route)
        {
            double x, y;
            var point = new GMap.NET.PointLatLng();

            if (currentPosition.DistanceTo(otherPosition) > _ghostMarkerThresholdDistance)
            {
                x = (currentPosition.Latitude.DecimalDegrees + otherPosition.Latitude.DecimalDegrees) / 2;
                y = (currentPosition.Longitude.DecimalDegrees + otherPosition.Longitude.DecimalDegrees) / 2;

                point.Lat = x;
                point.Lng = y;

                int ghostPointIndex = IsGhostPointExist(index, route);
                if (ghostPointIndex == -1)
                {
                    point = CreateGhostPoint(index, point, route);
                }
                else
                {
                    _ghostPointDictionary[route][ghostPointIndex].Marker.Position = point;
                }
            }
            else
            {
                RemoveGhostPointAt(index, route);
            }
        }
Пример #34
0
 public int DistanceTo_ExpectedValuesAreReturned(Position first, Position second)
 {
     return (int)first.DistanceTo(second);
 }