PathPlanningKart.PathPlanNextSegment C# (CSharp) Method

PathPlanNextSegment() public method

Performs path planning for the next path segment by utilizing a DynamicPathThreadJob that calculates the path in the background
public PathPlanNextSegment ( ) : void
return void
    public void PathPlanNextSegment()
    {
        bool noItem = (GetComponent<PowerUp>().powerUp == "");
        //Check if the next path segment needs to be calculated in a thread
        if (jobInProgress == false && nextWayPoints == null && dynamicReplan == false) {
            //trigger thread job for this car to obtain the next set of waypoints
            Node pathStartNode;
            if (currentThreadJob.destinationNode == PathPlanningDataStructures.graph.endNode) {
                pathStartNode = startNode;
            } else {
                pathStartNode = currentThreadJob.destinationNode;
            }
            currentThreadJob = new DynamicPathThreadJob (pathStartNode, PathPlanningDataStructures.graph.endNode, closedNodes, 12.0f, noItem);
            currentThreadJob.Start ();
            jobInProgress = true;
        } else if (jobInProgress == false && dynamicReplan) {
            Node pathStartNode;
            if (currentThreadJob.destinationNode == PathPlanningDataStructures.graph.endNode) {
                pathStartNode = startNode;
            } else {
                pathStartNode = PathPlanningDataStructures.graph.getClosestNode(transform.position + 3 * transform.forward);
            }
            currentThreadJob = new DynamicPathThreadJob(pathStartNode, PathPlanningDataStructures.graph.endNode, closedNodes, 5.0f, noItem);
            currentThreadJob.Start ();
            jobInProgress = true;
        }
        //Check if in progress thread has completed the path calculation
        if (jobInProgress) {
            if (currentThreadJob.isFinished()) {
                if (dynamicReplan) {
                    dynamicWayPoints = currentThreadJob.getPathWayPoints ();
                    nextWayPoints = null;
                } else {
                    nextWayPoints = currentThreadJob.getPathWayPoints();
                    dynamicWayPoints = null;
                }
                closedNodes = currentThreadJob.getClosedNodes ();
                jobInProgress = false;
            }
        }
    }

Usage Example

Ejemplo n.º 1
0
    //uncomment for original movement code
    void FixedUpdate()
    {
        if (isAI)
        {
            PathPlanningKart k = GetComponent <PathPlanningKart>();
            k.PathPlanNextSegment();
            Tuple <float, float> t = carController.speedAndTurn(this.gameObject);
            turnInput    = (float)t.Second;
            forwardInput = (float)t.First;
            ItemsAI.updateItems();
            k.UseItem();
        }
        else if (Input.GetKeyUp(KeyCode.P))
        {
            Time.timeScale = paused ? 1 : 0;
            paused         = !paused;
        }
        else
        {
            turnInput    = input.getTurnInput();
            forwardInput = input.getForwardInput();
        }

        //how quickly do we want to turn
        currentDeltaTurn = DELTA_TURN * turnInput;
        //takes care of ideling to go back straight && also turning

        //Debug.Log ("(turnValue + currentDeltaTurn): "+(turnValue + currentDeltaTurn)+"  MAXTU: "+(MAX_TURN)+"  speed: "+speed);
        if (Mathf.Abs(turnInput) < .1 && turnValue > 0)
        {
            turnValue = 0;
            // Debug.Log("Idle Pos");
            if (turnValue < DELTA_TURN)
            {
                turnValue = 0;
            }
            else
            {
                turnValue -= DELTA_TURN;
            }
        }
        else if (Mathf.Abs(turnInput) < .1 && turnValue < 0)
        {
            turnValue = 0;
            // Debug.Log("Idle Neg");
            if (turnValue > -DELTA_TURN)
            {
                turnValue = 0;
            }
            else
            {
                turnValue += DELTA_TURN;
            }
        }
        else if ((turnValue + currentDeltaTurn) < (MAX_TURN) && (turnValue + currentDeltaTurn) > (-MAX_TURN))
        {
            // Debug.Log("Turn");
            turnValue += currentDeltaTurn;
        }
        //Debug.Log("DeltaTurn: " + currentDeltaTurn);
        //Debug.Log("turnValue: " + turnValue);

        //Debug.Log("Vert: "+Input.GetAxis(vertical));
        //which way do we want to go


        //takes care of ACCELeration
        speed += ACCEL * forwardInput;
        if (speed < -MAX_SPEED)
        {
            speed = -MAX_SPEED;
        }
        if (speed > MAX_SPEED)
        {
            speed = MAX_SPEED;
        }
        if (forwardInput == 1 && speed < MAX_SPEED)
        {
            speed += ACCEL;
        }
        else if (forwardInput == -1 && speed > -MAX_SPEED)
        {
            speed -= ACCEL;
        }
        else if (forwardInput == 0 && speed < 0)
        {
            if (speed > -IDLE_ACCEL)
            {
                speed = 0;
            }
            else
            {
                speed += IDLE_ACCEL;
            }
        }
        else if (forwardInput == 0 && speed > 0)
        {
            if (speed < IDLE_ACCEL)
            {
                speed = 0;
            }
            else
            {
                speed -= IDLE_ACCEL;
            }
        }
        else if (Mathf.Abs(speed) > MAX_SPEED)
        {
            speed = Mathf.Sign(speed) * MAX_SPEED;
        }

        //applies boost, if any
        speed = speed * boost;

        //calculates the direction displacement vector
        rotationVector.Set(0, turnValue * speed * Time.deltaTime, 0);
        direction = Quaternion.Euler(rotationVector) * direction;

        //Debug.Log ("Direction: "+ direction);
        //Debug.Log ("Speed: " +speed);

        transform.position += direction * (speed * Time.deltaTime);
        if (!goingBackwards)
        {
            transform.rotation = Quaternion.LookRotation(direction);
        }
        else
        {
            transform.rotation = Quaternion.LookRotation(-1 * direction);
        }
        //newEquations ();
        //equations();
        //updateLogVectors ();
        if (writePositionsToFile)
        {
            WriteToRepo();
        }
        //uncomment for original movement code
    }
All Usage Examples Of PathPlanningKart::PathPlanNextSegment