示例#1
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        ContactPoint2D contact = collision.contacts[0];
        //  Debug.Break();
        Triangulator trianSc = collision.gameObject.transform.GetComponent <Triangulator>();

        if (trianSc == null)
        {
            return;
        }

        IndexableCyclicalLinkedList <Vertex> lst = trianSc.GetVertLst();



        int prevIdx;
        int pointIdx = GetClosestPointIdx(lst, contact.point);
        LinkedListNode <Vertex> prev, nxt, ctr, closest;

        ctr     = GetClosestPointVert(lst, contact.point);
        closest = ctr;
        // prev = ctr.Previous;
        if (ctr.Next != null)
        {
            nxt = ctr.Next;
        }
        else
        {
            nxt = lst.First;
        }
        prevIdx = pointIdx - 1;

        //co gdy circledmg zakrewa wszystkie punkty?....

        //

        //to teraz maja idx bierzesz dwie intersekcje a potem dla tych intersekcji sprawdzasz dystans do segmentow
        //dzieki temu bedzieszi wiedzil jaki odcienek dzielic.

        if (prevIdx < 0)
        {
            prevIdx += pointsTab.Length;
        }

        Vector2 [] intersection;
        //form first line seg
        intersection = null;



        List <Vertex> VertToRemoveLst;

        VertToRemoveLst = new List <Vertex>();
        int someIdx = lst.Count;
        LinkedListNode <Vertex> prevT, nxtT, ctrT;

        ctrT = ctr;
        int a = 0;

        List <LineSegmentWithAngle>          lineSegsOuter;
        List <LineSegmentInnerWithIntersect> intersectionLineSeg;
        List <IntersectionWithVert>          intersctionVertLst;

        intersctionVertLst  = new List <IntersectionWithVert>();
        lineSegsOuter       = new List <LineSegmentWithAngle>();
        intersectionLineSeg = new List <LineSegmentInnerWithIntersect>();


        // return;
        bool areWeOnBeginning = true;

        while (1 == 1)//going counter clock wise by vertexes
        {
            string temp            = "we check now ctr:" + ctr.Value.Index.ToString() + "on " + ctr.Value.Position.ToString() + " and " + nxt.Value.Index.ToString() + "on " + nxt.Value.Position.ToString();
            string temp1           = "czy odleglosc od contact point :" + contact.point.ToString() + " do " + ctr.Value.Position.ToString() + " = " + Distance(contact.point, ctr.Value.Position).ToString();
            float  ctr_next_radius = (float)Distance(ctr.Value.Position, nxt.Value.Position) + dmgRadius;
            string temp2           = "jest wieksza od dystansy z ctr do nxt plus dmgRadius" + ctr_next_radius.ToString();

            if (ctr == closest && areWeOnBeginning)
            {
                areWeOnBeginning = false;
            }

            if (Distance(contact.point, ctr.Value.Position) > Distance(ctr.Value.Position, nxt.Value.Position) + dmgRadius)//if to far to calc
            {
            }
            else
            {
                intersection = Intersection(contact.point.x, contact.point.y, dmgRadius, ctr.Value.Position, nxt.Value.Position);
                if (Distance(ctr.Value.Position, contact.point) < dmgRadius)
                {
                    VertToRemoveLst.Add(ctr.Value);
                }
                if (intersection != null)
                {
                    if (intersection.Length == 1)
                    {
                        //  lst.AddBefore(nxt, new LinkedListNode<Vertex>(new Vertex(intersection[0], someIdx++)));
                        if (Distance(nxt.Value.Position, contact.point) < dmgRadius)
                        {
                            //odcinek wchodzacy w dmgRadius push.
                            intersectionLineSeg.Add(new LineSegmentInnerWithIntersect(ctr.Value, nxt.Value, ctr, nxt, intersection[0], true));
                            //zapamietac intersekcje  wychodzącę.
                            //pod koniec alg lecisz od wychodzacej dopuki nie masz intersekcji.
                            //powinno dzialac dobrze x171113
                            Vector2[] circleIntersection;
                            circleIntersection = new Vector2[2];
                            Vector2 tempRotatedVec = intersection[0];
                            bool    weHaveSecondCircleIntersect = false;
                            //   float tempAngle = AngleBetweenVectorsDe(nxt.Value.Position, ctr.Value.Position);
                            //  while(!weHaveSecondCircleIntersect)// check is have lenght already?
                            //   {
                            //      circleIntersection[0] = intersection[0];
                            //      RotateVecPFromI(contact.point, 20, tempRotatedVec);
                            //kuzwa nie da rady tak trzeba wyliczyc wszystkie intersekcje i zapameitywac gdzie byly z jakimi odcinkami i potem na podstawie
                            //tych info dodawac te vertexy.
                            //patrzysz jeszcze czy to jest intersekcja wejsciowa czy wyjsciowa.
                            //mozna dwie listy bo potem tylko szukasz intersekcji wyjsciowych.
                            //to lecisz to tych intesekcjach z odcinkami wedle clockwise i bierzesz pierwsza ktora jest pod wzgledem wielkosci konta.
                            //czyli wyjsciowa intersekcja odcninkowa z najmniejszym katem clockwise co do obecnej wejsciowej.
                            //  }
                            //addbefore nxt until in
                        }
                        else
                        {
                            lineSegsOuter.Add(new LineSegmentWithAngle(ctr.Value, nxt.Value));
                            // intersectionLineSeg.Add(new LineSegmentInnerWithIntersect(ctr.Value, nxt.Value,intersection[0], false));
                        }
                        //  if (Distance(ctr.Value.Position, contact.point) < dmgRadius)
                        //   {
                        //      VertToRemoveLst.Add(ctr.Value);
                        // kiedy remove bo jak mamy kilka vertow np 3 w kole to jak?
                        // rozkmin ten usuwa tylko dwoch.
                        //   }
                        // VertToRemoveLst.Add(ctr.Value);
                        //  break;
                    }
                    else
                    {
                        if (FirstIsCloserThenSecond(intersection[0], intersection[1], nxt.Value.Position))
                        {
                            //     lst.AddBefore(nxt, new LinkedListNode<Vertex>(new Vertex(intersection[0], someIdx++)));
                            intersectionLineSeg.Add(new LineSegmentInnerWithIntersect(ctr.Value, nxt.Value, ctr, nxt, intersection[1], true));
                            lineSegsOuter.Add(new LineSegmentWithAngle(ctr.Value, nxt.Value));    // previus ctr to nxt

                            //       lst.AddBefore(nxt.Previous, new LinkedListNode<Vertex>(new Vertex(intersection[1], someIdx++)));
                            //  areWeHaveTwoIntesect = true;
                            //  break;
                        }
                        else
                        {
                            //     lst.AddBefore(nxt, new LinkedListNode<Vertex>(new Vertex(intersection[1], someIdx++)));
                            //      lst.AddBefore(nxt.Previous, new LinkedListNode<Vertex>(new Vertex(intersection[0], someIdx++)));
                            intersectionLineSeg.Add(new LineSegmentInnerWithIntersect(ctr.Value, nxt.Value, ctr, nxt, intersection[0], true));
                            lineSegsOuter.Add(new LineSegmentWithAngle(ctr.Value, nxt.Value));
                            // areWeHaveTwoIntesect = true;
                            // break;
                        }
                    }
                }
                else
                {
                    // i tak trzbea leciec po wszystkich bo jak beda plaskie polygony to bedzie uj
                    // a jak lecisz po wszystkich to wtedy nie mozesz breakowac jak juz masz intersekcje z dmgRadius.
                }
            }

            ctr = nxt;

            if (nxt.Next == null)
            {
                nxt = lst.First;
            }
            else
            {
                nxt = nxt.Next;
            }

            if (ctr == closest && !areWeOnBeginning)
            {
                break;
            }


            //ctr = closest;
        }

        /*
         * while(1==1 && !areWeHaveTwoIntesect)
         * {
         *  intersection = Intersection(contact.point.x, contact.point.y, dmgRadius, ctr.Value.Position, prev.Value.Position);
         *
         *  if (intersection != null)
         *  {
         *      if (intersection.Length == 1)
         *      {
         *          lst.AddAfter(prev, new LinkedListNode<Vertex>(new Vertex(intersection[0], someIdx++)));
         *          VertToRemoveLst.Add(ctr.Value);
         *          break;
         *      }
         *      else
         *      {
         *          if (FirstIsCloserThenSecond(intersection[0], intersection[1], nxt.Value.Position))
         *          {
         *              lst.AddAfter(prev, new LinkedListNode<Vertex>(new Vertex(intersection[0], someIdx++)));
         *              lst.AddAfter(prev.Next, new LinkedListNode<Vertex>(new Vertex(intersection[1], someIdx++)));
         *              areWeHaveTwoIntesect = true;
         *              break;
         *          }
         *          else
         *          {
         *              lst.AddAfter(prev, new LinkedListNode<Vertex>(new Vertex(intersection[1], someIdx++)));
         *              lst.AddAfter(prev.Next, new LinkedListNode<Vertex>(new Vertex(intersection[0], someIdx++)));
         *              areWeHaveTwoIntesect = true;
         *              break;
         *          }
         *      }
         *
         *  }
         *  else
         *  {
         *      ctr = prev;
         *      prev = prev.Previous;
         *      VertToRemoveLst.Add(ctr.Value);
         *  }
         * }
         *
         */

        //we must do this i one direction
        //


        gizmosLineSegs = new List <LineSegment>();
        while (intersectionLineSeg.Count > 0)
        {
            LineSegmentInnerWithIntersect trg = intersectionLineSeg[0];
            float angleOftrg   = Angle(trg.A.Position - trg.B.Position);
            float angleOfOuter = Angle(lineSegsOuter[0].A.Position - lineSegsOuter[0].B.Position);
            LinkedListNode <Vertex> tmp;


            bool    weHaveIntersect = false;
            Vector2 intersecVector  = trg.intersection;
            Vector2 ctrV;
            ctrV = intersecVector;
            float basicAngle = Angle(trg.intersection - contact.point);
            foreach (LineSegmentWithAngle i  in lineSegsOuter)
            {
                i.angle -= basicAngle;
                if (i.angle < 0)
                {
                    i.angle += Mathf.PI * 2;
                }
            }
            //float tempAngle = AngleBetweenVectorsDe(nxt.Value.Position, ctr.Value.Position);
            //  objListOrder.Sort((x, y) => x.OrderDate.CompareTo(y.OrderDate));
            lineSegsOuter.OrderByDescending(x => x.angle);
            float crossingX, crossingY;
            // lineSegsOuter.Sort()
            tmp  = lst.AddAfter(trg.aLink, new Vertex(trg.intersection, someIdx++));
            ctrV = RotateVecPFromI(contact.point, -2, ctrV);
            while (!weHaveIntersect)// check is have lenght already?
            {
                Vector2 nxtV;

                LineSegmentWithAngle nearestLineSeg = lineSegsOuter[0];
                nxtV = RotateVecPFromI(contact.point, -30, ctrV);   // czy obraca w dobra strone?
                if (Get_line_intersection(ctrV.x, ctrV.y, nxtV.x, nxtV.y, nearestLineSeg.A.Position.x,
                                          nearestLineSeg.A.Position.y, nearestLineSeg.B.Position.x, nearestLineSeg.B.Position.y, out crossingX, out crossingY) == 0)
                {
                    ctrV = nxtV;
                    nxtV = RotateVecPFromI(contact.point, -30, ctrV);
                    tmp  = lst.AddAfter(tmp, new Vertex(ctrV, someIdx++));
                }
                else
                {
                    tmp             = lst.AddAfter(tmp, new Vertex(new Vector2(crossingX, crossingY), someIdx++));
                    weHaveIntersect = true;
                    intersectionLineSeg.RemoveAt(0);
                }

                gizmosLineSegs.Add(new LineSegment(new Vertex(ctrV, /*idx*/ 1000), new Vertex(nxtV, /*idx*/ 1000)));
                // lst.ad


                //kuzwa nie da rady tak trzeba wyliczyc wszystkie intersekcje i zapameitywac gdzie byly z jakimi odcinkami i potem na podstawie
                //tych info dodawac te vertexy.
                //patrzysz jeszcze czy to jest intersekcja wejsciowa czy wyjsciowa.
                //mozna dwie listy bo potem tylko szukasz intersekcji wyjsciowych.
                //to lecisz to tych intesekcjach z odcinkami wedle clockwise i bierzesz pierwsza ktora jest pod wzgledem wielkosci konta.
                //czyli wyjsciowa intersekcja odcninkowa z najmniejszym katem clockwise co do obecnej wejsciowej.
            }
        }



        for (int i = 0; i < VertToRemoveLst.Count; i++)
        {
            lst.Remove(VertToRemoveLst[i]);
        }



        /*
         * intersection = Intersection(contact.point.x, contact.point.y, dmgRadius, ctr.Value.Position, nxt.Value.Position);
         *
         * for(int i=0;i<intersection.Length;i++)
         * {
         *  circleIntesections.Add(intersection[i]);
         * }
         *
         *
         * intersection = Intersection(contact.point.x, contact.point.y, dmgRadius, ctr.Value.Position, nxt.Value.Position);
         *
         * for (int i = 0; i < intersection.Length; i++)
         * {
         *  circleIntesections.Add(intersection[i]);
         * }
         *
         */
        //lecisz po next od near i dozycasz do wyrzucenia.
        //
        //lst.
        for (int i = 0; i < lst.Count; i++)
        {
            Debug.Log(lst[i].Value);
        }



        trianSc.ApplyNewPointsFromLst(lst);

        trianSc.InitNeighboursWithCollision(collision.contacts[0].point, dmgRadius);

        ExplosionWaker explWk = this.GetComponent <ExplosionWaker>();

        if (explWk != null)
        {
            explWk.WakeExplo(collision.contacts[0].point, dmgRadius);
        }


        Destroy(gameObject);
    }
示例#2
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        ContactPoint2D contact = collision.contacts[0];
        //  Debug.Break();
        Triangulator trianSc = collision.gameObject.transform.GetComponent <Triangulator>();

        IndexableCyclicalLinkedList <Vertex> lst = trianSc.GetVertLst();



        int prevIdx;
        int pointIdx = GetClosestPointIdx(lst, contact.point);
        LinkedListNode <Vertex> prev, nxt, ctr, closest;

        ctr     = GetClosestPointVert(lst, contact.point);
        closest = ctr;
        // prev = ctr.Previous;
        if (ctr.Next != null)
        {
            nxt = ctr.Next;
        }
        else
        {
            nxt = lst.First;
        }
        prevIdx = pointIdx - 1;

        //co gdy circledmg zakrewa wszystkie punkty?....

        //

        //to teraz maja idx bierzesz dwie intersekcje a potem dla tych intersekcji sprawdzasz dystans do segmentow
        //dzieki temu bedzieszi wiedzil jaki odcienek dzielic.

        if (prevIdx < 0)
        {
            prevIdx += pointsTab.Length;
        }

        Vector2 [] intersection;
        //form first line seg
        intersection = null;



        List <Vertex> VertToRemoveLst;

        VertToRemoveLst = new List <Vertex>();
        int someIdx = lst.Count;
        LinkedListNode <Vertex> prevT, nxtT, ctrT;

        ctrT = ctr;
        int a = 0;


        // return;
        bool areWeOnBeginning = true;

        while (1 == 1)//going counter clock wise by vertexes
        {
            string temp            = "we check now ctr:" + ctr.Value.Index.ToString() + "on " + ctr.Value.Position.ToString() + " and " + nxt.Value.Index.ToString() + "on " + nxt.Value.Position.ToString();
            string temp1           = "czy odleglosc od contact point :" + contact.point.ToString() + " do " + ctr.Value.Position.ToString() + " = " + Distance(contact.point, ctr.Value.Position).ToString();
            float  ctr_next_radius = (float)Distance(ctr.Value.Position, nxt.Value.Position) + dmgRadius;
            string temp2           = "jest wieksza od dystansy z ctr do nxt plus dmgRadius" + ctr_next_radius.ToString();

            if (ctr == closest && areWeOnBeginning)
            {
                areWeOnBeginning = false;
            }

            if (Distance(contact.point, ctr.Value.Position) > Distance(ctr.Value.Position, nxt.Value.Position) + dmgRadius)//if to far to calc
            {
            }
            else
            {
                intersection = Intersection(contact.point.x, contact.point.y, dmgRadius, ctr.Value.Position, nxt.Value.Position);
                if (Distance(ctr.Value.Position, contact.point) < dmgRadius)
                {
                    VertToRemoveLst.Add(ctr.Value);
                }
                if (intersection != null)
                {
                    if (intersection.Length == 1)
                    {
                        lst.AddBefore(nxt, new LinkedListNode <Vertex>(new Vertex(intersection[0], someIdx++)));
                        if (Distance(nxt.Value.Position, contact.point) < dmgRadius)
                        {
                            Vector2[] circleIntersection;
                            circleIntersection = new Vector2[2];
                            Vector2 tempRotatedVec = intersection[0];
                            bool    weHaveSecondCircleIntersect = false;
                            //   float tempAngle = AngleBetweenVectorsDe(nxt.Value.Position, ctr.Value.Position);
                            while (!weHaveSecondCircleIntersect)// check is have lenght already?
                            {
                                circleIntersection[0] = intersection[0];
                                RotateVecPFromI(contact.point, 20, tempRotatedVec);
                                //kuzwa nie da rady tak trzeba wyliczyc wszystkie intersekcje i zapameitywac gdzie byly z jakimi odcinkami i potem na podstawie
                                //tych info dodawac te vertexy.
                                //patrzysz jeszcze czy to jest intersekcja wejsciowa czy wyjsciowa.
                                //mozna dwie listy bo potem tylko szukasz intersekcji wyjsciowych.
                                //to lecisz to tych intesekcjach z odcinkami wedle clockwise i bierzesz pierwsza ktora jest pod wzgledem wielkosci konta.
                                //czyli wyjsciowa intersekcja odcninkowa z najmniejszym katem clockwise co do obecnej wejsciowej.
                            }
                            //addbefore nxt until in
                        }
                        //  if (Distance(ctr.Value.Position, contact.point) < dmgRadius)
                        //   {
                        //      VertToRemoveLst.Add(ctr.Value);
                        // kiedy remove bo jak mamy kilka vertow np 3 w kole to jak?
                        // rozkmin ten usuwa tylko dwoch.
                        //   }
                        // VertToRemoveLst.Add(ctr.Value);
                        //  break;
                    }
                    else
                    {
                        if (FirstIsCloserThenSecond(intersection[0], intersection[1], nxt.Value.Position))
                        {
                            lst.AddBefore(nxt, new LinkedListNode <Vertex>(new Vertex(intersection[0], someIdx++)));
                            lst.AddBefore(nxt.Previous, new LinkedListNode <Vertex>(new Vertex(intersection[1], someIdx++)));
                            //  areWeHaveTwoIntesect = true;
                            //  break;
                        }
                        else
                        {
                            lst.AddBefore(nxt, new LinkedListNode <Vertex>(new Vertex(intersection[1], someIdx++)));
                            lst.AddBefore(nxt.Previous, new LinkedListNode <Vertex>(new Vertex(intersection[0], someIdx++)));
                            // areWeHaveTwoIntesect = true;
                            // break;
                        }
                    }
                }
                else
                {
                    // i tak trzbea leciec po wszystkich bo jak beda plaskie polygony to bedzie uj
                    // a jak lecisz po wszystkich to wtedy nie mozesz breakowac jak juz masz intersekcje z dmgRadius.
                }
            }

            ctr = nxt;

            if (nxt.Next == null)
            {
                nxt = lst.First;
            }
            else
            {
                nxt = nxt.Next;
            }

            if (ctr == closest && !areWeOnBeginning)
            {
                break;
            }


            //ctr = closest;
        }

        /*
         * while(1==1 && !areWeHaveTwoIntesect)
         * {
         *  intersection = Intersection(contact.point.x, contact.point.y, dmgRadius, ctr.Value.Position, prev.Value.Position);
         *
         *  if (intersection != null)
         *  {
         *      if (intersection.Length == 1)
         *      {
         *          lst.AddAfter(prev, new LinkedListNode<Vertex>(new Vertex(intersection[0], someIdx++)));
         *          VertToRemoveLst.Add(ctr.Value);
         *          break;
         *      }
         *      else
         *      {
         *          if (FirstIsCloserThenSecond(intersection[0], intersection[1], nxt.Value.Position))
         *          {
         *              lst.AddAfter(prev, new LinkedListNode<Vertex>(new Vertex(intersection[0], someIdx++)));
         *              lst.AddAfter(prev.Next, new LinkedListNode<Vertex>(new Vertex(intersection[1], someIdx++)));
         *              areWeHaveTwoIntesect = true;
         *              break;
         *          }
         *          else
         *          {
         *              lst.AddAfter(prev, new LinkedListNode<Vertex>(new Vertex(intersection[1], someIdx++)));
         *              lst.AddAfter(prev.Next, new LinkedListNode<Vertex>(new Vertex(intersection[0], someIdx++)));
         *              areWeHaveTwoIntesect = true;
         *              break;
         *          }
         *      }
         *
         *  }
         *  else
         *  {
         *      ctr = prev;
         *      prev = prev.Previous;
         *      VertToRemoveLst.Add(ctr.Value);
         *  }
         * }
         *
         */

        //we must do this i one direction
        //

        for (int i = 0; i < VertToRemoveLst.Count; i++)
        {
            lst.Remove(VertToRemoveLst[i]);
        }



        /*
         * intersection = Intersection(contact.point.x, contact.point.y, dmgRadius, ctr.Value.Position, nxt.Value.Position);
         *
         * for(int i=0;i<intersection.Length;i++)
         * {
         *  circleIntesections.Add(intersection[i]);
         * }
         *
         *
         * intersection = Intersection(contact.point.x, contact.point.y, dmgRadius, ctr.Value.Position, nxt.Value.Position);
         *
         * for (int i = 0; i < intersection.Length; i++)
         * {
         *  circleIntesections.Add(intersection[i]);
         * }
         *
         */
        //lecisz po next od near i dozycasz do wyrzucenia.
        //
        //lst.

        trianSc.ApplyNewPointsFromLst(lst);

        Destroy(gameObject);
    }