int compareVectorAngle(roadNetworkRoad a, roadNetworkRoad b)
 {
     if (!a.getOtherIntersection(this) || !b.getOtherIntersection(this))
     {
         return(0);
     }
     return(compareVectorAngle(
                a.getOtherIntersection(this).gameObject.transform.position - this.transform.position,
                b.getOtherIntersection(this).gameObject.transform.position - this.transform.position));
 }
 public bool isConnected(roadNetworkRoad input)
 {
     foreach (roadNetworkRoad road in connectedRoads)
     {
         if (road == input)
         {
             return(true);
         }
     }
     return(false);
 }
Пример #3
0
    void MakeRoads(Way way)
    {
        for (int i = 0; i < way.nodeIndices.Count - 1; i++)
        {
            roadNetworkIntersection tempNode = nodeList[way.nodeIndices[i]].intersection;
            roadNetworkIntersection nextNode = nodeList[way.nodeIndices[i + 1]].intersection;
            GameObject newRoad = (GameObject)Instantiate(roadObject, tempNode.gameObject.transform.position, Quaternion.identity);
            if (way.tags.ContainsKey("name"))
            {
                newRoad.name = way.tags["name"];
            }
            else
            {
                newRoad.name = "way:" + way.id.ToString();
            }
            roadNetworkRoad tempRoad = newRoad.GetComponent <roadNetworkRoad>();
            tempRoad.origin      = tempNode;
            tempRoad.destination = nextNode;

            double width;
            string widthString;
            string highwayString;
            if (way.tags.TryGetValue("width", out widthString))
            {
                width = ConvertToMeters(widthString);
            }
            else if (way.tags.TryGetValue("lanes", out widthString) && Double.TryParse(widthString, out width))
            {
                width *= 3;
            }
            else if (way.tags.TryGetValue("highway", out highwayString))
            {
                width = GetDefaultWidth(highwayString);
            }
            else
            {
                width = 1;
            }

            tempRoad.widthLeft  = (float)(width / 2);
            tempRoad.widthRight = (float)(width / 2);
            tempRoad.EnsureConnections();
            way.roadList.Add(tempRoad);
        }
    }
    void GenerateEndPoint()
    {
        roadNetworkRoad road = connectedRoads[0];

        if (road.getOtherIntersection(this))
        {
            roadDirection direction = new roadDirection();
            direction.connectedRoad = road;
            direction.totalDistance = road.getOtherIntersection(this).gameObject.transform.position - this.transform.position;

            direction.totalDistanceFlat = new Vector3(direction.totalDistance.x, 0, direction.totalDistance.z);

            direction.absoluteAngle = AngleClamp(Math.Atan2(direction.totalDistanceFlat.z, direction.totalDistanceFlat.x));
            Vector3 leftPoint  = new Vector3(direction.connectedRoad.widthLeft * (float)Math.Cos(direction.absoluteAngle + (Math.PI / 2.0f)), 0, direction.connectedRoad.widthLeft * (float)Math.Sin(direction.absoluteAngle + (Math.PI / 2.0f)));
            Vector3 rightPoint = new Vector3(direction.connectedRoad.widthRight * (float)Math.Cos(direction.absoluteAngle - (Math.PI / 2.0f)), 0, direction.connectedRoad.widthRight * (float)Math.Sin(direction.absoluteAngle - (Math.PI / 2.0f)));
            direction.connectedRoad.setEndPoints(leftPoint, new Vector3(), rightPoint, this);
        }
    }
    void GenerateCorner()
    {
        for (int i = 0; i < connectedRoads.Count; i++)
        {
            roadNetworkRoad road = connectedRoads[i];

            if (road.getOtherIntersection(this))
            {
                roadDirection direction = new roadDirection();
                direction.connectedRoad = road;
                direction.totalDistance = road.getOtherIntersection(this).gameObject.transform.position - this.transform.position;

                direction.totalDistanceFlat = new Vector3(direction.totalDistance.x, 0, direction.totalDistance.z);

                direction.absoluteAngle = AngleClamp(Math.Atan2(direction.totalDistanceFlat.z, direction.totalDistanceFlat.x));
                directionList.Add(direction);
            }
        }
        for (int i = 0; i < directionList.Count; i++)
        {
            int j = i + 1;
            if (j >= directionList.Count)
            {
                j = 0;
            }
            roadDirection currentDirection = directionList[i];
            roadDirection nextDirection    = directionList[j];

            float cornerDistance = (currentDirection.connectedRoad.getWidthLeft(this) + nextDirection.connectedRoad.getWidthRight(this)) / 2.0f;

            //find the vectors to the elbows between the streets.
            double innerAngle = AngleClamp(nextDirection.absoluteAngle - currentDirection.absoluteAngle);
            currentDirection.nextCornerAngle = AngleClamp(innerAngle / 2 + currentDirection.absoluteAngle);
            if (innerAngle > Math.PI)
            {
                innerAngle = (Math.PI * 2) - innerAngle;
            }
            if (innerAngle > Math.PI / 4)
            {
                currentDirection.nextCornerDistance = cornerDistance / (float)(Math.Sin(innerAngle / 2.0));
            }
            else
            {
                currentDirection.nextCornerDistance = cornerDistance;
            }
        }
        for (int i = 0; i < directionList.Count; i++)
        {
            int k = i - 1;
            if (k < 0)
            {
                k = directionList.Count - 1;
            }
            roadDirection prevDirection    = directionList[k];
            roadDirection currentDirection = directionList[i];

            currentDirection.nextStreetDistance = (float)(Math.Sqrt((currentDirection.nextCornerDistance * currentDirection.nextCornerDistance) - (currentDirection.connectedRoad.getWidthLeft(this) * currentDirection.connectedRoad.getWidthLeft(this))));
            currentDirection.prevStreetDistance = (float)(Math.Sqrt((prevDirection.nextCornerDistance * prevDirection.nextCornerDistance) - (currentDirection.connectedRoad.getWidthRight(this) * currentDirection.connectedRoad.getWidthRight(this))));
            double forwardAngle   = AngleClamp(currentDirection.nextCornerAngle - currentDirection.absoluteAngle);
            double backwardsAngle = AngleClamp(currentDirection.absoluteAngle - prevDirection.nextCornerAngle);

            if (backwardsAngle > Math.PI)
            {
                backwardsAngle -= Math.PI;
            }
            if (forwardAngle > Math.PI)
            {
                forwardAngle -= Math.PI;
            }

            if (forwardAngle > (Math.PI / 2.0))
            {
                currentDirection.nextStreetDistance = -currentDirection.nextStreetDistance;
            }
            if (backwardsAngle > (Math.PI / 2.0))
            {
                currentDirection.prevStreetDistance = -currentDirection.prevStreetDistance;
            }

            Vector3 nextMidpoint = new Vector3(currentDirection.nextCornerDistance * (float)Math.Cos(currentDirection.nextCornerAngle), 0, currentDirection.nextCornerDistance * (float)Math.Sin(currentDirection.nextCornerAngle));

            int tempIndex = vertices.Count;
            vertices.Add(nextMidpoint);

            currentDirection.nextAngleVertex = tempIndex;
        }
        for (int i = 0; i < directionList.Count; i++)
        {
            int k = i - 1;
            if (k < 0)
            {
                k = directionList.Count - 1;
            }
            int j = i + 1;
            if (j >= directionList.Count)
            {
                j = 0;
            }
            roadDirection prevDirection    = directionList[k];
            roadDirection currentDirection = directionList[i];
            currentDirection.prevAngleVertex = prevDirection.nextAngleVertex;
            currentDirection.connectedRoad.setEndPoints(vertices[currentDirection.nextAngleVertex], new Vector3(), vertices[currentDirection.prevAngleVertex], this);
        }
        vertices.Clear();
    }
    void GenerateBasicIntersection()
    {
        Vector3 centerpoint = new Vector3(0, 0, 0);

        vertices.Add(centerpoint);
        directionList.Clear();
        for (int i = 0; i < connectedRoads.Count; i++)
        {
            roadNetworkRoad road = connectedRoads[i];

            if (road.getOtherIntersection(this))
            {
                roadDirection direction = new roadDirection();
                direction.connectedRoad = road;
                direction.totalDistance = road.getOtherIntersection(this).gameObject.transform.position - this.transform.position;

                direction.totalDistanceFlat = new Vector3(direction.totalDistance.x, 0, direction.totalDistance.z);

                direction.absoluteAngle = AngleClamp(Math.Atan2(direction.totalDistanceFlat.z, direction.totalDistanceFlat.x));
                directionList.Add(direction);
            }
        }
        for (int i = 0; i < directionList.Count; i++)
        {
            int j = i + 1;
            if (j >= directionList.Count)
            {
                j = 0;
            }
            roadDirection currentDirection = directionList[i];
            roadDirection nextDirection    = directionList[j];

            //find the vectors to the elbows between the streets.
            double innerAngle = AngleClamp(nextDirection.absoluteAngle - currentDirection.absoluteAngle);
            if (innerAngle == Math.PI)
            {
                currentDirection.nextCornerAngle    = AngleClamp(Math.PI / 2 + currentDirection.absoluteAngle);
                currentDirection.nextCornerDistance = -1.0f;
            }
            else
            {
                double angleMid = AngleClamp(Math.Atan2(
                                                 ((currentDirection.connectedRoad.getWidthLeft(this) / nextDirection.connectedRoad.getWidthRight(this)) * Math.Sin(innerAngle)),
                                                 1 + ((currentDirection.connectedRoad.getWidthLeft(this) / nextDirection.connectedRoad.getWidthRight(this)) * Math.Cos(innerAngle))));
                currentDirection.nextCornerAngle    = AngleClamp(angleMid + currentDirection.absoluteAngle);
                currentDirection.nextCornerDistance = currentDirection.connectedRoad.getWidthLeft(this) / ((float)Math.Sin(angleMid));
            }
        }
        for (int i = 0; i < directionList.Count; i++)
        {
            int k = i - 1;
            if (k < 0)
            {
                k = directionList.Count - 1;
            }
            roadDirection prevDirection    = directionList[k];
            roadDirection currentDirection = directionList[i];

            currentDirection.nextStreetDistance = (float)(Math.Sqrt((currentDirection.nextCornerDistance * currentDirection.nextCornerDistance) - (currentDirection.connectedRoad.getWidthLeft(this) * currentDirection.connectedRoad.getWidthLeft(this))));
            currentDirection.prevStreetDistance = (float)(Math.Sqrt((prevDirection.nextCornerDistance * prevDirection.nextCornerDistance) - (currentDirection.connectedRoad.getWidthRight(this) * currentDirection.connectedRoad.getWidthRight(this))));
            double forwardAngle   = AngleClamp(currentDirection.nextCornerAngle - currentDirection.absoluteAngle);
            double backwardsAngle = AngleClamp(currentDirection.absoluteAngle - prevDirection.nextCornerAngle);

            if (backwardsAngle > Math.PI)
            {
                backwardsAngle -= Math.PI;
            }
            if (forwardAngle > Math.PI)
            {
                forwardAngle -= Math.PI;
            }

            if (forwardAngle > (Math.PI / 2.0))
            {
                currentDirection.nextStreetDistance = -currentDirection.nextStreetDistance;
            }
            if (backwardsAngle > (Math.PI / 2.0))
            {
                currentDirection.prevStreetDistance = -currentDirection.prevStreetDistance;
            }

            float distance = currentDirection.nextStreetDistance;
            if (distance < currentDirection.prevStreetDistance)
            {
                distance = currentDirection.prevStreetDistance;
            }

            Vector3 endpoint     = currentDirection.totalDistanceFlat.normalized * distance;
            Vector3 nextMidpoint = new Vector3(currentDirection.nextCornerDistance * (float)Math.Cos(currentDirection.nextCornerAngle), 0, currentDirection.nextCornerDistance * (float)Math.Sin(currentDirection.nextCornerAngle));

            int tempIndex = vertices.Count;
            vertices.Add(endpoint);
            vertices.Add(nextMidpoint);

            currentDirection.nextAngleVertex    = tempIndex + 1;
            currentDirection.centerStreetVertex = tempIndex;
        }
        //corner mirroring
        for (int i = 0; i < directionList.Count; i++)
        {
            int k = i - 1;
            if (k < 0)
            {
                k = directionList.Count - 1;
            }
            int j = i + 1;
            if (j >= directionList.Count)
            {
                j = 0;
            }
            roadDirection prevDirection    = directionList[k];
            roadDirection currentDirection = directionList[i];
            currentDirection.prevAngleVertex = prevDirection.nextAngleVertex;
            if (currentDirection.nextStreetDistance > currentDirection.prevStreetDistance)
            {
                int     tempIndex          = vertices.Count;
                float   distanceDifference = currentDirection.nextStreetDistance - currentDirection.prevStreetDistance;
                Vector3 newStreetVert      = (currentDirection.totalDistanceFlat.normalized * distanceDifference) + vertices[currentDirection.prevAngleVertex];
                currentDirection.nextStreetVertex = currentDirection.nextAngleVertex;
                currentDirection.prevStreetVertex = tempIndex;
                vertices.Add(newStreetVert);
                faces.Add(0);
                faces.Add(currentDirection.prevStreetVertex);
                faces.Add(currentDirection.prevAngleVertex);
            }
            else
            {
                int     tempIndex          = vertices.Count;
                float   distanceDifference = currentDirection.prevStreetDistance - currentDirection.nextStreetDistance;
                Vector3 newStreetVert      = (currentDirection.totalDistanceFlat.normalized * distanceDifference) + vertices[currentDirection.nextAngleVertex];
                currentDirection.prevStreetVertex = currentDirection.prevAngleVertex;
                currentDirection.nextStreetVertex = tempIndex;
                vertices.Add(newStreetVert);
                faces.Add(0);
                faces.Add(currentDirection.nextAngleVertex);
                faces.Add(currentDirection.nextStreetVertex);
            }
            currentDirection.connectedRoad.setEndPoints(vertices[currentDirection.nextStreetVertex], vertices[currentDirection.centerStreetVertex], vertices[currentDirection.prevStreetVertex], this);
        }
        //finally make all the tris now that we have the verts.
        for (int i = 0; i < directionList.Count; i++)
        {
            faces.Add(0);
            faces.Add(directionList[i].nextStreetVertex);
            faces.Add(directionList[i].centerStreetVertex);
            faces.Add(0);
            faces.Add(directionList[i].centerStreetVertex);
            faces.Add(directionList[i].prevStreetVertex);
        }

        //proper UVs will need to be made, this is just for testing
        foreach (Vector3 vertex in vertices)
        {
            Vector2 uvert = new Vector2();
            uvert.x = vertex.x / 3;
            uvert.y = vertex.z / 3;
            uvs.Add(uvert);
        }
    }