private void FollowPath(double elapsed) { // Move to the next node in our path if we are close enough to // our current target node. if ((Position - _pathEnum.Current.Position).Length < NavigationDistance) { // Set path to null if we have reached our destination. if (!_pathEnum.MoveNext()) { _path = null; } } if (_path != null) { _seekAtSteering.Location = _pathEnum.Current.Position; Vector2 steeringForce = _seekAtSteering.Steer(this, elapsed) * 10; Vector2 acceleration = steeringForce / Mass; Velocity += acceleration * elapsed; Velocity = Velocity.Truncate(MaxSpeed); Position += Velocity * elapsed; } }
private void CreatePath() { // Calculate shortest path from current position to // the current mouse position and store the IEnumerator // of that path for future navigation. GraphNode nearestCurrent = _world.Graph.NearestNode(Position); GraphNode nearestDestination = _world.Graph.NearestNode(Mouse.Position); try { _path = new AStarAlgorithm(_world.Graph) .GetShortestPath(nearestCurrent, nearestDestination); _pathEnum = _path.Path.GetEnumerator(); if (!_pathEnum.MoveNext()) { // Delete path if we're already at the destination. _path = null; } } catch (ArgumentException) { Debug.WriteLine("Cannot plan a path to this location"); } }