Пример #1
0
    private void VisualizeSplines()
    {
        var sys = (target as TrafSystemData).system;

        foreach (var r in sys.entries)
        {
            var spline = r.GetSpline();

            float pc   = 0f;
            var   line = new List <Vector3>();
            int   i    = 0;
            while (true)
            {
                if (pc > 1.08f)
                {
                    i++;
                    pc -= 1f;
                }

                if (i >= spline.Count - 1)
                {
                    break;
                }

                line.Add(HermiteMath.HermiteVal(spline[i].position, spline[i + 1].position, spline[i].tangent, spline[i + 1].tangent, pc));


                pc += 0.1f;
            }

            Handles.color = Color.green;
            Handles.DrawPolyLine(line.ToArray());
        }
    }
Пример #2
0
    void OnSceneGUI()
    {
        var t = target as TrafIntersection;

        foreach (var p in t.paths)
        {
            Handles.color = Color.yellow;
            Handles.DrawPolyLine(p.start.transform.position,
                                 HermiteMath.HermiteVal(p.start.transform.position, p.end.transform.position, p.start.transform.forward, p.end.transform.forward, 0.2f),
                                 HermiteMath.HermiteVal(p.start.transform.position, p.end.transform.position, p.start.transform.forward, p.end.transform.forward, 0.4f),
                                 HermiteMath.HermiteVal(p.start.transform.position, p.end.transform.position, p.start.transform.forward, p.end.transform.forward, 0.6f),
                                 HermiteMath.HermiteVal(p.start.transform.position, p.end.transform.position, p.start.transform.forward, p.end.transform.forward, 0.8f),
                                 p.end.transform.position);
        }
    }
Пример #3
0
    private static TrafRoad GetRoad(TrafIntersectionPath p)
    {
        var r = ScriptableObject.CreateInstance(typeof(TrafRoad)) as TrafRoad;

        r.waypoints = new List <TrafRoadPoint>();
        r.waypoints.Add(new TrafRoadPoint()
        {
            position = HermiteMath.HermiteVal(p.start.transform.position, p.end.transform.position, p.start.transform.forward, p.end.transform.forward, 0f)
        });
        r.waypoints.Add(new TrafRoadPoint()
        {
            position = HermiteMath.HermiteVal(p.start.transform.position, p.end.transform.position, p.start.transform.forward, p.end.transform.forward, 0.33f)
        });
        r.waypoints.Add(new TrafRoadPoint()
        {
            position = HermiteMath.HermiteVal(p.start.transform.position, p.end.transform.position, p.start.transform.forward, p.end.transform.forward, 0.66f)
        });
        r.waypoints.Add(new TrafRoadPoint()
        {
            position = HermiteMath.HermiteVal(p.start.transform.position, p.end.transform.position, p.start.transform.forward, p.end.transform.forward, 1f)
        });
        return(r);
    }
Пример #4
0
    private void VisualizeIntersectionSplines()
    {
        var sys = (target as TrafSystemData).system;

        foreach (var intersection in sys.intersections)
        {
            var spline = intersection.GetSpline();

            float pc   = 0f;
            var   line = new List <Vector3>();
            while (true)
            {
                if (pc > 1.08f)
                {
                    break;
                }
                line.Add(HermiteMath.HermiteVal(spline[0].position, spline[1].position, spline[0].tangent, spline[1].tangent, pc));
                pc += 0.1f;
            }

            Handles.color = Color.green;
            Handles.DrawPolyLine(line.ToArray());
        }
    }
    void MoveCar()
    {
        float thisDist = Vector3.Distance(currentWaypoint, prevWaypoint);

        currentPc += currentSpeed / thisDist * Time.fixedDeltaTime;


        if (currentPc > 1f)
        {
            prevWaypoint = currentWaypoint;
            prevNode     = currentNode;
            prevTangent  = currentTangent;

            currentWaypoint      = nextWaypoint;
            currentNode          = nextNode;
            currentWaypointIndex = nextWaypointIndex;
            UpdateNextWaypoint();

            if (currentNode.tangent == Vector3.zero)
            {
                currentTangent = (nextWaypoint - currentWaypoint).normalized;
            }
            else
            {
                currentTangent = (currentNode.tangent - currentWaypoint).normalized;
            }


            currentPc -= 1f;

            currentPc = (currentPc * thisDist) / Vector3.Distance(currentWaypoint, prevWaypoint);
        }

        Vector3 currentSpot = HermiteMath.HermiteVal(prevWaypoint, currentWaypoint, prevTangent, currentTangent, currentPc);



        //transform.Rotate(0f, currentTurn * Time.deltaTime, 0f);
        RaycastHit hit;

        Physics.Raycast(transform.position + Vector3.up * 5, -transform.up, out hit, 100f, ~(1 << LayerMask.NameToLayer("Traffic")));

        Vector3 hTarget = new Vector3(currentSpot.x, hit.point.y, currentSpot.z);


        Vector3 tangent;

        if (currentPc < 0.95f)
        {
            tangent = HermiteMath.HermiteVal(prevWaypoint, currentWaypoint, prevTangent, currentTangent, currentPc + 0.05f) - currentSpot;
        }
        else
        {
            tangent = currentSpot - HermiteMath.HermiteVal(prevWaypoint, currentWaypoint, prevTangent, currentTangent, currentPc - 0.05f);
        }
        tangent.y = 0f;
        tangent   = tangent.normalized;

        GetComponent <Rigidbody>().MoveRotation(Quaternion.FromToRotation(Vector3.up, hit.normal) * Quaternion.LookRotation(tangent));
        GetComponent <Rigidbody>().MovePosition(hTarget);
        //transform.Translate(Vector3.forward * currentSpeed * Time.deltaTime);
    }