Exemplo n.º 1
0
    private void DrawLineToVertices(ref BottomPolygon[] poly)
    {
        Vector3  firstVert = new Vector3();
        Vector2  endPos1   = new Vector2();
        Segement ray;
        Segement segement;
        Vector3  endPos;

        for (int ip = 0; ip < poly.Length; ip++)
        {               //polygon
            for (int iv = 0; iv < poly[ip].vertices.Length; iv++)
            {
                //vertices of the polygon
                Color color = new Color(0F, 0.5F, 1F, 1F);
                if (iv != 0)
                {
                    color = colors[iv % 4];
                }
                Debug.DrawLine(player.transform.position, poly[ip].vertices[iv], color, 0F, false);

                Debug.Log("verticezs " + poly[ip].vertices[iv] + " " + poly.Length + " " + poly[ip].vertices.Length);

                if (iv == poly[ip].vertices.Length - 1)
                {
                    firstVert = poly[ip].vertices[0];
                }
                else
                {
                    firstVert = poly[ip].vertices[iv];
                }


                ray      = new Segement(player.transform.position, source.position);
                segement = new Segement(poly[ip].vertices[iv], firstVert);

                endPos1 = myAlgoTest(ray, segement);
                endPos  = new Vector3(endPos1.x, 0, endPos1.y);
//				Debug.Log ("OUT endPos " + poly[ip].vertices[iv] + " _ "+ source.position+ " _ "+ endPos);

                if (endPos.x != int.MinValue && endPos.y != int.MaxValue)
                {
                    Debug.DrawLine(player.transform.position, endPos, Color.red, 0F, false);
                }
            }
        }
        Debug.DrawLine(player.transform.position, source.position, Color.yellow, 0F, false);
    }
Exemplo n.º 2
0
    private Vector2 myAlgoTest(Segement ray, Segement segment)
    {
        /*
         *
         *		Point + Direction * T
         *
         * ------------------------------------------------------------------------
         *
         *      Ray X = r_px+r_dx*T1
         *		Ray Y = r_py+r_dy*T1
         *		Segment X = s_px+s_dx*T2
         *		Segment Y = s_py+s_dy*T2
         *
         * ------------------------------------------------------------------------
         *
         *      If the ray & segment intersect, their X & Y components will be the same:
         *		r_px + r_dx * T1 = s_px+s_dx*T2
         *		r_py    + r_dy*T1 = s_py+s_dy*T2
         *
         * ------------------------------------------------------------------------
         *
         *		Isolate T1 for both equations, getting rid of T1
         *	T1 = (s_px+s_dx*T2-r_px)/r_dx = (s_py+s_dy*T2-r_py)/r_dy
         *
         *      Multiply both sides by r_dx * r_dy
         *	s_px*r_dy + s_dx*T2*r_dy - r_px*r_dy = s_py*r_dx + s_dy*T2*r_dx - r_py*r_dx
         *
         *      Solve for T2!
         *	T2 = (r_dx*(s_py-r_py) + r_dy*(r_px-s_px))/(s_dx*r_dy - s_dy*r_dx)
         *
         *		 Plug the value of T2 to get T1
         *	T1 = (s_px+s_dx*T2-r_px)/r_dx
         *
         *
         */

        Vector2 ret = new Vector2(int.MinValue, int.MaxValue);

        // RAY in parametric: Point + Delta*T1
        Vector2 rayPoint    = new Vector2(ray.b.x, ray.b.y);
        Vector2 rayDistance = new Vector2(ray.b.x - ray.a.x, ray.b.y - ray.a.y);

        // SEGMENT in parametric: Point + Delta*T2
        Vector2 segmentPoint    = new Vector2(segment.a.x, segment.a.y);
        Vector2 segmentDistance = new Vector2(segment.b.x - segment.a.x, segment.b.y - segment.a.y);


        Debug.Log("!!!!!!!!!!!!!!!!!!!! segmentDistance = " + segment.ToString());

        //	Debug.Log ("in my function " + segmentPoint + " _ "+ rayPoint);


        //Check if they're parallel
        float rayMag = Mathf.Sqrt(rayDistance.x * rayDistance.x + rayDistance.y * rayDistance.y);
        float segMag = Mathf.Sqrt(segmentDistance.x * segmentDistance.x + segmentDistance.y * segmentDistance.y);

        //float rayMag = rayDistance.x+ rayDistance.y;
        //float segMag = segmentDistance.x + segmentDistance.y;
        if (rayDistance.x / rayMag == segmentDistance.x / segMag && rayDistance.y / rayMag == segmentDistance.y / segMag)
        {
            Debug.Log("Parallel case");
            //they're parallel
            return(new Vector2(int.MinValue, int.MinValue));
        }


        float t1 = 0, t2 = 0;


        t2 = (rayDistance.x * (segmentPoint.y - rayPoint.x) + rayDistance.y * (rayPoint.x - segmentPoint.x)) / (segmentDistance.x * rayDistance.y - segmentDistance.y * rayDistance.x);         ///(s_dx*r_dy - s_dy*r_dx);
        t1 = (segmentPoint.x + segmentDistance.x * t2 - rayPoint.x) / rayDistance.x;


        if (t1 < 0 || t2 < 0 || t2 > 1)
        {
            return(ret);
        }


        Debug.Log("Intersect found");
        return(new Vector2(rayPoint.x + rayDistance.x * t1, rayPoint.y + rayDistance.y * t1));
        //Yeah we  found an intersect !
    }