コード例 #1
0
        public override Goal Decompose(KinematicData character, Goal goal)
        {
            if ((Astar == null))
            {
                Astar = new NodeArrayAStarPathFinding(Graph, Heuristic);
                Astar.InitializePathfindingSearch(character.position, goal.position);
                CurrentParam = 0.0f;
            }


            GlobalPath currentSolution;

            if (Astar.InProgress)
            {
                var finished = Astar.Search(out currentSolution, true);

                if (finished && currentSolution != null)
                {
                    this.AStarSolution = currentSolution;
                    this.GlobalPath    = StringPullingPathSmoothing.SmoothPath(character, currentSolution);
                    this.GlobalPath.CalculateLocalPathsFromPathPositions(character.position);
                    // gets first node
                    goal.position = this.GlobalPath.LocalPaths[0].EndPosition;
                    return(goal);
                }

                /* else if(currentSolution != null && currentSolution.IsPartial)
                 * {
                 *   goal.position = currentSolution.PathPositions[0];
                 *   return goal;
                 * }*/
            }
            else
            {
                if (GlobalPath.PathEnd(CurrentParam))
                {
                    goal.position = GlobalPath.LocalPaths[GlobalPath.LocalPaths.Count - 1].GetPosition(1.0f);
                    return(goal);
                }

                CurrentParam = GlobalPath.GetParam(character.position, CurrentParam);


                goal.position = GlobalPath.GetPosition(CurrentParam + 0.2f);
                return(goal);
            }
            return(new Goal());
        }
コード例 #2
0
 public void SetPath(GlobalPath globalPath)
 {
     this.path = globalPath;
     base.Character.Position = globalPath.PathPositions[0];
     base.Target.Position    = globalPath.PathPositions[0] + globalPath.GetPosition(pathOffset);
 }
    private void Update()
    {
        foreach (var pedestrian in this.Pedestrians)
        {
            pedestrian.Update();
        }

        Vector3 testMouseClickPosition;

        if (TargeterComponent.GetGoalPosition(out testMouseClickPosition))
        {
            this.mouseClickPosition = testMouseClickPosition;
            Actuator.SetGoalPosition(this.mouseClickPosition);
            DecomposerComponent.Initialize(this.mouseClickPosition);
            this.startPosition      = this.character.KinematicData.position;
            currentSmoothedSolution = null;
        }

        if (this.character.KinematicData.Arrived)
        {
            //NOTE: stops the char from going forward before it flips back
            this.character.KinematicData.velocity = Vector3.zero;
            return;
        }

        // Path recalculation for the car actuator
        if (currentSmoothedSolution != null)
        {
            float distanceToPath = (character.KinematicData.position - currentSmoothedSolution.GetPosition((Actuator.GetMovement() as DynamicFollowPath).CurrentParam)).sqrMagnitude;

            if (distanceToPath > 400 && DecomposerComponent.Initialize(this.mouseClickPosition)) // 400 = 20^2
            {
                Debug.Log("searching");
                this.startPosition = this.character.KinematicData.position;
                Actuator.SetGoalPosition(this.mouseClickPosition);
                this.CalculatePath      = true;
                currentSmoothedSolution = null;
                currentSolution         = null;
            }
        }

        if (this.CalculatePath)
        {
            //Debug.Log("CALCULATE PATH");
            DecomposerComponent.CalculatePath(out this.currentSolution);
        }

        if (this.currentSolution != null &&
            (this.currentSmoothedSolution == null || DecomposerComponent.InNewLocalPath(this.currentSmoothedSolution) || this.violation))
        {
            //Debug.Log("NEW LINE");
            this.currentSmoothedSolution = DecomposerComponent.SmoothPath(this.currentSolution);
            Actuator.SetPath(currentSmoothedSolution);
        }

        if (this.currentSmoothedSolution == null)
        {
            return;
        }

        GlobalPath actuatorPath = null;
        GlobalPath pathToFollow = currentSmoothedSolution;

        violation = false;

        for (int i = 0; i < ITERATIONS_LIMIT; i++)
        {
            actuatorPath = Actuator.GetPath(pathToFollow);

            foreach (IConstraint constraint in PathConstraints)
            {
                Vector3 suggestedPosition;
                violation = constraint.WillViolate(actuatorPath, out suggestedPosition);
                if (violation)
                {
                    pathToFollow = new GlobalPath();
                    pathToFollow.LocalPaths.Add(new LineSegmentPath(character.KinematicData.position, suggestedPosition));
                    //Debug.Log("Path collision violation");
                    break;
                }
            }

            if (violation)
            {
                continue;
            }

            // restrições de output
            foreach (IConstraint constraint in MovementConstraints)
            {
                Vector3 suggestedPosition;
                violation = constraint.WillViolate(actuatorPath, out suggestedPosition);
                if (violation)
                {
                    pathToFollow = new GlobalPath();
                    pathToFollow.LocalPaths.Add(new LineSegmentPath(character.KinematicData.position, suggestedPosition));
                    //Debug.Log("Movement collision violation");
                    break;
                }
            }

            if (!violation)
            {
                break;
            }
        }

        Debug.DrawLine(this.character.KinematicData.position, actuatorPath.LocalPaths[0].EndPosition, Color.black);

        //NOTE: setting new legal path for the actuator and clearing auxiliary variable
        if (!violation)
        {
            Actuator.SetPath(actuatorPath);
            actuatorPath = null;
        }

        character.MovementOutput = Actuator.GetOutput(actuatorPath, character);

        this.character.Update();
    }