Esempio n. 1
0
    // returns the maximum value of AbstractCarPosition.offset so that the car is not rushing into the intersection
    public float stoppingDistanceForCurrentDrive(AbstractCarPosition carPosition)
    {
        /**
         *
         *  toIntersection                                     ref intersection
         *
         *  toLeftEdge
         *  \===================================================|   ^
         *   \|<          STOPPING DISTANCE                   > |  width(refIntersection)
         *    \ ------------------------------------------------|   x
         *     \                                                |  width(toIntersection)
         *      \===============================================|   v
         *     toRightEdge
         *
         *
         *
         */

        Intersection toIntersection = carPosition.referenceRoad.otherIntersection(carPosition.referenceIntersection);
        Vector3      toLeftEdge     = builder.coordinateForRoadAtIntersection(toIntersection, carPosition.referenceRoad,
                                                                              IntersectionBuilder.RightOrLeft.LEFT);
        Vector3 toRightEdge = builder.coordinateForRoadAtIntersection(toIntersection, carPosition.referenceRoad,
                                                                      IntersectionBuilder.RightOrLeft.RIGHT);

        // get the proportion
        int totalNumberOfLanes =
            carPosition.referenceRoad.GetNumberOfLanesInDirectionWithReferenceIntersection(toIntersection)
            + carPosition.referenceRoad.GetNumberOfLanesInDirectionWithReferenceIntersection(carPosition
                                                                                             .referenceIntersection);

        Vector3 stoppingLocation = Vector3.Lerp(toLeftEdge, toRightEdge,
                                                1f / 2 - (1f / 2 + carPosition.laneNumber) / totalNumberOfLanes) * (1f / 2);

        // project the stopping location onto the outgoing vector

        Vector3 stoppingCompensation = Vector3.Project(stoppingLocation,
                                                       IntersectionMath.getOutgoingVector(toIntersection, carPosition.referenceRoad));

        float stoppingDistance = carPosition.referenceRoad.GetRoadLength() - stoppingCompensation.magnitude;

        return(stoppingDistance);
    }
Esempio n. 2
0
    private GameObject buildAllWayStopSign(GameObject baseObj, Intersection intersection)
    {
        foreach (Road road in intersection.getConnectedRoads())
        {
            // get the coordinate of stop sign for that road

            // left and right are regarding the viewpoint from the intersection, although we want our stop sign on
            // the right, we need to pass in Left since stop sign is on the left of the road if you look at the road
            // from the intersection
            Vector3 position = intersectionBuilder.
                               coordinateForRoadAtIntersection(intersection, road, IntersectionBuilder.RightOrLeft.LEFT);

            position = position + intersection.position;

            // get the orientation
            Quaternion orientation = IntersectionMath.getAngle(intersection, road);

            GameObject stopSign = Instantiate(stopSignPrefab, position, orientation);

            stopSign.transform.parent = baseObj.transform;
        }
        return(baseObj);
    }
Esempio n. 3
0
 public Vector3 ccoordinateForRoadEnds(Road road, IntersectionBuilder.RightOrLeft ltrt)
 {
     return(builder.coordinateForRoadAtIntersection(this, road, ltrt));
 }
Esempio n. 4
0
    public IntersectionBuilder intersectionBuilder;    // we need to query intersection builder for coordinate information

    public GameObject BuildRoad(Road road)
    {
        Vector3 start = road.fromIntersection.position;
        Vector3 end   = road.toIntersection.position;

        /**
         * FROM LEFT 3 ------------------------- TO RIGHT 2
         *
         * start                                      end
         *
         * FROM RIGHT 0 -------------------------- TO LEFT 1
         *
         *
         * 0
         *
         */



        Mesh mesh = new Mesh();

        float width = road.GetRoadWidth();

        Vector3[] verticies =
        {
            intersectionBuilder.coordinateForRoadAtIntersection(road.fromIntersection, road, IntersectionBuilder.RightOrLeft.RIGHT),
            intersectionBuilder.coordinateForRoadAtIntersection(road.toIntersection,   road, IntersectionBuilder.RightOrLeft.LEFT) + end - start,
            intersectionBuilder.coordinateForRoadAtIntersection(road.toIntersection,   road, IntersectionBuilder.RightOrLeft.RIGHT) + end - start,
            intersectionBuilder.coordinateForRoadAtIntersection(road.fromIntersection, road, IntersectionBuilder.RightOrLeft.LEFT),
        };

        mesh.vertices = verticies;

        int[] triangles =
        {
            1, 0, 2,
            2, 0, 3
        };
        mesh.triangles = triangles;

        float distance = Vector3.Distance(verticies[0], verticies[1]);

        Vector2[] uv =
        {
            new Vector2(0,                                                                            0),
            new Vector2(distance,                                                                     0),
            new Vector2(Vector3.Dot(verticies[2] - verticies[0], verticies[1] - verticies[0]) / distance, 1),
            new Vector2(Vector3.Dot(verticies[3] - verticies[0], verticies[1] - verticies[0]) / distance, 1)
        };
        //Debug.LogFormat("UV = {0}{1}{2}{3}", uv[0], uv[1], uv[2], uv[3]);
        mesh.uv = uv;

        Vector3[] normals =
        {
            Vector3.up,
            Vector3.up,
            Vector3.up,
            Vector3.up
        };

        mesh.normals = normals;



        Vector3    roadPosition = start + new Vector3(0, 0.01f, 0);
        GameObject obj          = Instantiate(roadPrefab, roadPosition, Quaternion.identity);
//		obj.transform.parent = road.fromIntersection.GetGameObject ().transform;


        MeshFilter objMeshFilter = obj.GetComponent <MeshFilter> ();

        objMeshFilter.mesh = mesh;

        return(obj);
    }