Example #1
0
        public Slice2D PolygonSlice(Polygon2D slice, Polygon2D slicePolygonDestroy)
        {
            Slice2D slice2D = Slice2D.Create(gameObject, slice);

            if (isActiveAndEnabled == false)
            {
                return(slice2D);
            }

            Polygon2D colliderPolygon = GetPolygonToSlice();

            if (colliderPolygon != null)
            {
                Slice2D sliceResult = Slicer2D.API.PolygonSlice(colliderPolygon, slice);

                if (sliceResult.GetPolygons().Count > 0)
                {
                    if (slicePolygonDestroy != null)
                    {
                        foreach (Polygon2D p in new List <Polygon2D>(sliceResult.GetPolygons()))
                        {
                            if (slicePolygonDestroy.PolyInPoly(p) == true)
                            {
                                sliceResult.RemovePolygon(p);
                            }
                        }
                    }
                    // Check If Slice Result Is Correct
                    if (sliceResult.GetPolygons().Count > 0)
                    {
                        sliceResult.AddGameObjects(PerformResult(sliceResult.GetPolygons(), slice2D));
                    }
                    else if (slicePolygonDestroy != null)
                    {
                        Destroy(gameObject);
                    }

                    return(sliceResult);
                }
            }

            return(slice2D);
        }
Example #2
0
        // Triggered Before Every Slice
        // Should we perform slice? Is it succesful according our rules?
        bool OnSlice(Slice2D sliceResult)
        {
            Polygon2D CutObject = ThinSliceHelper.GetSmallestPolygon(sliceResult);

            // Forbidden double slices // Shouldn't be possible with linear slicer
            if (sliceResult.GetPolygons().Count > 2)
            {
                return(false);
            }

            // Add Particles if slice is succesful
            if (CutObject == null)
            {
                ThinSlicerParticles.Create();
                Slicer2DController.Get().complexControllerObject.pointsList[0].Clear();
                return(false);
            }

            return(true);
        }
Example #3
0
        static public List <Slice2D> ComplexCutSliceAll(ComplexCut complexCut, Layer layer = null)
        {
            List <Slice2D> result = new List <Slice2D> ();

            if (layer == null)
            {
                layer = Layer.Create();
            }

            foreach (Sliceable2D id in Sliceable2D.GetListLayer(layer))
            {
                Slice2D sliceResult = id.ComplexCutSlice(complexCut);
                if (sliceResult.GetGameObjects().Count > 0)
                {
                    result.Add(sliceResult);
                }
            }

            return(result);
        }
Example #4
0
        static public List <Slice2D> ExplodeAll(Layer layer = null, int explosionSlices = 0)
        {
            List <Slice2D> result = new List <Slice2D> ();

            if (layer == null)
            {
                layer = Layer.Create();
            }

            foreach (Sliceable2D id in Sliceable2D.GetListLayer(layer))
            {
                Slice2D sliceResult = id.Explode(explosionSlices);
                if (sliceResult.GetGameObjects().Count > 0)
                {
                    result.Add(sliceResult);
                }
            }

            return(result);
        }
Example #5
0
        static public List <Slice2D> ExplodeInPointAll(Vector2D point, Layer layer = null)
        {
            List <Slice2D> result = new List <Slice2D> ();

            if (layer == null)
            {
                layer = Layer.Create();
            }

            foreach (Sliceable2D id in Sliceable2D.GetListLayer(layer))
            {
                Slice2D sliceResult = id.ExplodeInPoint(point);
                if (sliceResult.GetGameObjects().Count > 0)
                {
                    result.Add(sliceResult);
                }
            }

            return(result);
        }
Example #6
0
        static public List <Slice2D> PointSliceAll(Vector2D slice, float rotation, Layer layer = null)
        {
            List <Slice2D> result = new List <Slice2D> ();

            if (layer == null)
            {
                layer = Layer.Create();
            }

            foreach (Sliceable2D id in Sliceable2D.GetListLayer(layer))
            {
                Slice2D sliceResult = id.PointSlice(slice, rotation);
                if (sliceResult.GetGameObjects().Count > 0)
                {
                    result.Add(sliceResult);
                }
            }

            return(result);
        }
Example #7
0
        static public List <Slice2D> ComplexSliceAll(List <Vector2D> slice, Slice2DLayer layer = null)
        {
            List <Slice2D> result = new List <Slice2D> ();

            if (layer == null)
            {
                layer = Slice2DLayer.Create();
            }

            foreach (Slicer2D id in GetListLayer(layer))
            {
                Slice2D sliceResult = id.ComplexSlice(slice);
                if (sliceResult.GetGameObjects().Count > 0)
                {
                    result.Add(sliceResult);
                }
            }

            return(result);
        }
Example #8
0
        static public List <Slice2D> LinearCutSliceAll(LinearCut linearCut, Slice2DLayer layer = null)
        {
            List <Slice2D> result = new List <Slice2D> ();

            if (layer == null)
            {
                layer = Slice2DLayer.Create();
            }

            foreach (Slicer2D id in GetListLayer(layer))
            {
                Slice2D sliceResult = id.LinearCutSlice(linearCut);
                if (sliceResult.GetGameObjects().Count > 0)
                {
                    result.Add(sliceResult);
                }
            }

            return(result);
        }
Example #9
0
        // Complex Slice
        static public Slice2D Slice(Polygon2D polygon, List <Vector2D> slice)
        {
            Slice2D result = Slice2D.Create(null, slice);

            if (slice == null)
            {
                return(result);
            }

            if (slice.Count < 2)
            {
                return(result);
            }

            // Normalize into clockwise
            polygon.Normalize();

            // Optimization
            // Skip slicing polygons that are not overlapping with slice
            Rect sliceRect   = Math2D.GetBounds(slice);
            Rect polygonRect = polygon.GetBounds();

            if (sliceRect.Overlaps(polygonRect) == false)
            {
                return(result);
            }

            // If possible - slice out area
            if (Slicer2D.complexSliceType != Slicer2D.SliceType.Regular)
            {
                result = ComplexSlicerExtended.SlicePolygonInside(polygon, slice);
                if (result.GetPolygons().Count > 0)
                {
                    return(result);
                }
            }

            result = MultipleSlice(polygon, slice);

            return(result);
        }
Example #10
0
        public Slice2D Explode(int explosionSlices = 0)
        {
            Slice2D slice2D = Slice2D.Create(gameObject, Slice2DType.Explode);

            if (isActiveAndEnabled == false)
            {
                return(slice2D);
            }

            Polygon2D colliderPolygon = GetPolygonToSlice();

            if (colliderPolygon != null)
            {
                Slice2D sliceResult = Slicer2D.API.Explode(colliderPolygon, explosionSlices);
                PerformResult(sliceResult.GetPolygons(), sliceResult);

                return(sliceResult);
            }

            return(slice2D);
        }
Example #11
0
        public Slice2D ComplexSlice(List <Vector2D> slice)
        {
            Slice2D slice2D = Slice2D.Create(gameObject, slice);

            if (isActiveAndEnabled == false)
            {
                return(slice2D);
            }

            Polygon2D colliderPolygon = GetPolygonToSlice();

            if (colliderPolygon != null)
            {
                Slice2D sliceResult = Slicer2D.API.ComplexSlice(colliderPolygon, slice);
                PerformResult(sliceResult.GetPolygons(), sliceResult);

                return(sliceResult);
            }

            return(slice2D);
        }
Example #12
0
        public Slice2D ComplexCutSlice(ComplexCut slice)
        {
            Slice2D slice2D = Slice2D.Create(gameObject, slice);

            if (isActiveAndEnabled == false)
            {
                return(slice2D);
            }

            Polygon2D colliderPolygon = GetPolygonToSlice();

            if (colliderPolygon != null)
            {
                Polygon2D slicePoly = new Polygon2D(slice.GetPointsList(1.01f));

                if (Math2D.PolyInPoly(slicePoly, colliderPolygon) == true)
                {
                    Destroy(gameObject);
                    return(slice2D);
                }
                else
                {
                    Slice2D sliceResult = Slicer2D.API.ComplexCutSlice(colliderPolygon, slice);

                    foreach (Polygon2D poly in new List <Polygon2D> (sliceResult.GetPolygons()))
                    {
                        if (Math2D.PolyInPoly(slicePoly, poly))
                        {
                            sliceResult.RemovePolygon(poly);
                        }
                    }

                    PerformResult(sliceResult.GetPolygons(), sliceResult);

                    return(sliceResult);
                }
            }

            return(Slice2D.Create(gameObject, slice));
        }
Example #13
0
        static public void ComplexTrail(Slice2D slice, float forceAmount)
        {
            Rigidbody2D rigidBody2D;
            Vector2D    vec;
            float       sliceRotation;
            float       forceVal;
            Vector2     vecSum = Vector2.zero;

            for (int i = 0; i < slice.GetGameObjects().Count; i++)
            {
                rigidBody2D = slice.GetGameObjects()[i].GetComponent <Rigidbody2D> ();
                if (rigidBody2D)
                {
                    List <Vector2D> collisions = slice.GetCollisions();

                    forceVal = 2.0f / collisions.Count;
                    for (int x = 0; x < collisions.Count; x++)
                    {
                        vec = collisions[x];

                        pair.A = vec;

                        if (pair.A != null && pair.B != null)
                        {
                            sliceRotation = (float)Vector2D.Atan2(pair.A, pair.B);

                            force.x = Mathf.Cos(sliceRotation) * forceAmount;
                            force.y = Mathf.Sin(sliceRotation) * forceAmount;

                            vecSum.x = (float)(pair.A.x + pair.B.x) / 2f;
                            vecSum.y = (float)(pair.A.y + pair.B.y) / 2f;

                            rigidBody2D.AddForceAtPosition(forceVal * force, vecSum);
                        }

                        pair.B = pair.A;
                    }
                }
            }
        }
        public void Result(Slice2D slice)
        {
            if (sliceResultEvent != null)
            {
                sliceResultEvent(slice);
            }

            if (globalSliceResultEvent != null)
            {
                globalSliceResultEvent(slice);
            }

            if (anchorSliceResultEvent != null)
            {
                anchorSliceResultEvent(slice);
            }

            if (anchorGlobalSliceResultEvent != null)
            {
                anchorGlobalSliceResultEvent(slice);
            }
        }
Example #15
0
        public Slice2D PointSlice(Vector2D point, float rotation)
        {
            Slice2D slice2D = Slice2D.Create(gameObject, point, rotation);

            if (isActiveAndEnabled == false)
            {
                return(slice2D);
            }

            Polygon2D colliderPolygon = GetPolygonToSlice();

            if (colliderPolygon != null)
            {
                Slice2D sliceResult = Slicer2D.API.PointSlice(colliderPolygon, point, rotation);

                PerformResult(sliceResult.GetPolygons(), sliceResult);

                return(sliceResult);
            }

            return(slice2D);
        }
Example #16
0
        static public void LinearSlice(Slice2D slice, float forceAmount)
        {
            float sliceRotationA = (float)Vector2D.Atan2(slice.slice[1], slice.slice[0]) - 90f * Mathf.Deg2Rad;
            float sliceRotationB = (float)Vector2D.Atan2(slice.slice[1], slice.slice[0]) + 90f * Mathf.Deg2Rad;
            float rot            = 0;

            Rigidbody2D rigidBody2D;

            List <Vector2D> collisions      = slice.GetCollisions();
            Vector2         middleCollision = ((collisions[0] + collisions[1]) / 2f).ToVector2();

            foreach (GameObject gameObject in slice.GetGameObjects())
            {
                rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
                if (rigidBody2D)
                {
                    Polygon2D poly   = slice.GetPolygons()[slice.GetGameObjects().IndexOf(gameObject)];
                    Rect      bounds = poly.GetBounds();

                    Vector2 cpA = middleCollision;
                    cpA = cpA.Push(sliceRotationA, 1);

                    Vector2 cpB = middleCollision;
                    cpB = cpB.Push(sliceRotationB, 1);

                    if (Vector2.Distance(cpA, bounds.center) < Vector2.Distance(cpB, bounds.center))
                    {
                        rot = sliceRotationA;
                    }
                    else
                    {
                        rot = sliceRotationB;
                    }

                    rigidBody2D.AddForce(new Vector2(Mathf.Cos(rot), Mathf.Sin(rot)) * forceAmount);
                }
            }
        }
Example #17
0
        //オブジェクトをスライスしたら
        void SliceEvent(Slice2D slice)
        {
            //親オブジェクトを取得
            GameObject OyaKame = transform.root.gameObject.transform.GetChild(0).gameObject;

            OyaKame.GetComponent <LettuceTatle>().SetCutted();

            //スライスされた全オブジェクトを読み込み
            foreach (GameObject parts in slice.GetGameObjects())
            {
                Rigidbody2D rb;
                if (rb = parts.GetComponent <Rigidbody2D>())
                {
                    parts.GetComponent <PolygonCollider2D>().isTrigger = false;
                    rb.freezeRotation = true;
                    rb.gravityScale   = 1.0f;
                    rb.AddForce(new Vector2(Random.Range(-50, 50), 100));
                }
                //スライスを不可にする
                Slicer2D slicer = parts.GetComponent <Slicer2D>();
                slicer.enabled = false;
            }
        }
        //オブジェクトをスライスしたら
        void SliceEvent(Slice2D slice)
        {
            //親稲を取得
            GameObject OyaIne = transform.root.gameObject;

            OyaIne.GetComponent <Ine>().SetCutted();

            //スライスされた全オブジェクトを読み込み
            foreach (GameObject parts in slice.GetGameObjects())
            {
                Rigidbody2D rb;
                if (rb = parts.GetComponent <Rigidbody2D>())
                {
                    rb.freezeRotation = true;
                    rb.gravityScale   = 1.0f;
                    rb.AddForce(new Vector2(Random.Range(-50, 50), 100));
                }

                //スライスを不可にする
                Slicer2D slicer = parts.GetComponent <Slicer2D>();
                slicer.enabled = false;
            }
        }
Example #19
0
        // Triggered On Every Successful Slice
        // Manage Game Objects
        void AfterSlice(Slice2D sliceResult)
        {
            GameObject cutObject = ThinSliceHelper.GetSmallestGameObject(sliceResult);

            if (cutObject != null)
            {
                ThinSliceHelper.ExplodeGameObject(cutObject, sliceResult.originGameObject);

                foreach (GameObject g in sliceResult.GetGameObjects())
                {
                    if (g != cutObject)
                    {
                        g.name = sliceResult.originGameObject.name;
                    }
                    else
                    {
                        g.name = "Falling Peace";
                    }
                }
            }

            ThinSliceGameManager.UpdateLevelProgress();
        }
        public void CopyTracker(Slice2D slice, Slicer2D slicer)
        {
            foreach (Slicer2DLinearTrackedController trackerComponent in Object.FindObjectsOfType <Slicer2DLinearTrackedController>())
            {
                if (trackerComponent.trackerObject == this)
                {
                    continue;
                }

                Dictionary <Slicer2D, SlicerTrackerObject> list = new Dictionary <Slicer2D, SlicerTrackerObject>(trackerComponent.trackerObject.trackerList);
                foreach (KeyValuePair <Slicer2D, SlicerTrackerObject> pair in list)
                {
                    if (pair.Key != slicer)
                    {
                        continue;
                    }
                    foreach (GameObject g in slice.GetGameObjects())
                    {
                        Slicer2D newSlicer = g.GetComponent <Slicer2D>();

                        SlicerTrackerObject t = null;
                        trackerList.TryGetValue(newSlicer, out t);

                        if (t == null)
                        {
                            t = new SlicerTrackerObject();

                            t.firstPosition = pair.Value.firstPosition;
                            t.lastPosition  = pair.Value.lastPosition;
                            t.tracking      = true;

                            trackerComponent.trackerObject.trackerList.Add(newSlicer, t);
                        }
                    }
                }
            }
        }
Example #21
0
        static public Slice2D LinearCutSlice(Polygon2D polygon, LinearCut linearCut)
        {
            List <Vector2D> slice  = linearCut.GetPointsList().ToVector2DList();
            Slice2D         result = Slice2D.Create(null, linearCut);

            if (slice.Count < 1)
            {
                return(result);
            }

            Vector2D startPoint = null;

            foreach (Vector2D id in slice)
            {
                if (polygon.PointInPoly(id) == false)
                {
                    startPoint = id;
                    break;
                }
            }

            Polygon2D newPolygon = new Polygon2D(slice);

            slice = Vector2DList.GetListStartingPoint(slice, startPoint);
            slice.Add(startPoint);

            if (polygon.PolyInPoly(newPolygon))
            {
                polygon.AddHole(newPolygon);
                result.AddPolygon(polygon);
                return(result);
            }

            result = ComplexSlicer.Slice(polygon, slice);

            return(result);
        }
        void SliceEvent(Slice2D slice)
        {
            Slicer2DParticlesManager.Instantiate();

            foreach (List <Vector2D> pointList in slice.slices)
            {
                foreach (Pair2D p in Pair2D.GetList(pointList))
                {
                    Particle2D firstParticle = Particle2D.Create(Random.Range(0, 360), new Vector3((float)p.A.x, (float)p.A.y, posZ));
                    Slicer2DParticlesManager.particlesList.Add(firstParticle);

                    Particle2D lastParticle = Particle2D.Create(Random.Range(0, 360), new Vector3((float)p.B.x, (float)p.B.y, posZ));
                    Slicer2DParticlesManager.particlesList.Add(lastParticle);

                    Vector2 pos = p.A.ToVector2();
                    while (Vector2.Distance(pos, p.B.ToVector2()) > 0.5f)
                    {
                        pos = Vector2.MoveTowards(pos, p.B.ToVector2(), 0.35f);
                        Particle2D particle = Particle2D.Create(Random.Range(0, 360), new Vector3(pos.x, pos.y, posZ));
                        Slicer2DParticlesManager.particlesList.Add(particle);
                    }
                }
            }
        }
        public List <Slice2D> Update(Vector2D position, float timer, Slice2DLayer layer)
        {
            List <Slice2D> result = new List <Slice2D>();

            foreach (Slicer2D slicer in Slicer2D.GetListCopy())
            {
                if (slicer.MatchLayers(layer) == false)
                {
                    continue;
                }

                SlicerTrailObject trail = null;
                trailList.TryGetValue(slicer, out trail);

                if (trail == null)
                {
                    trail = new SlicerTrailObject();
                    trailList.Add(slicer, trail);
                }

                if (trail.lastPosition != null)
                {
                    if (Vector2D.Distance(trail.lastPosition, position) > 0.05f)
                    {
                        trail.pointsList.Insert(0, new TrailPoint(position, timer));
                    }
                }
                else
                {
                    trail.pointsList.Insert(0, new TrailPoint(position, timer));
                }

                foreach (TrailPoint trailPoint in new List <TrailPoint>(trail.pointsList))
                {
                    if (trailPoint.Update() == false)
                    {
                        trail.pointsList.Remove(trailPoint);
                    }
                }

                if (trail.pointsList.Count > 1)
                {
                    Vector2D firstPoint  = null;
                    Vector2D lastPoint   = null;
                    bool     insideState = false;

                    foreach (TrailPoint trailPoint in trail.pointsList)
                    {
                        bool inside = false;
                        if (slicer.shape.GetLocal().PointInPoly(trailPoint.position.InverseTransformPoint(slicer.transform)))
                        {
                            inside = true;
                        }

                        switch (insideState)
                        {
                        case true:
                            // Slice!
                            if (inside == false)
                            {
                                lastPoint = trailPoint.position;

                                insideState = false;
                                break;
                            }
                            break;

                        case false:
                            if (inside == false)
                            {
                                // Searching For Start of Slice
                                firstPoint  = trailPoint.position;
                                insideState = true;
                            }
                            break;
                        }

                        if (lastPoint != null)
                        {
                            break;
                        }
                    }

                    if (firstPoint != null && lastPoint != null)
                    {
                        Slicer2D.complexSliceType = Slicer2D.SliceType.Regular;
                        Slice2D slice = slicer.LinearSlice(new Pair2D(firstPoint, lastPoint));
                        if (slice.GetGameObjects().Count > 0)
                        {
                            trailList.Remove(slicer);

                            result.Add(slice);
                        }
                        ;
                    }
                }

                trail.lastPosition = position;
            }
            return(result);
        }
        public void Update(Vector2 position, float minVertexDistance = 1f)
        {
            List <Slicer2D> slicer2DList = Slicer2D.GetListCopy();

            Vector2D trackedPos;


            foreach (Slicer2D slicer in slicer2DList)
            {
                SlicerTrackerObject tracker = null;
                trackerList.TryGetValue(slicer, out tracker);

                if (tracker == null)
                {
                    tracker = new SlicerTrackerObject();
                    trackerList.Add(slicer, tracker);
                }

                trackedPos = new Vector2D(slicer.transform.transform.InverseTransformPoint(position));

                if (tracker.lastPosition != null)
                {
                    if (slicer.shape.GetLocal().PointInPoly(trackedPos))
                    {
                        if (tracker.tracking == false)
                        {
                            tracker.firstPosition = tracker.lastPosition;
                        }

                        tracker.tracking = true;
                    }
                    else if (tracker.tracking == true)
                    {
                        tracker.tracking = false;

                        if (tracker.firstPosition != null)
                        {
                            tracker.lastPosition = trackedPos;

                            Pair2D slicePair = new Pair2D(new Vector2D(slicer.transform.TransformPoint(tracker.firstPosition.ToVector2())), new Vector2D(slicer.transform.TransformPoint(tracker.lastPosition.ToVector2())));

                            Slice2D slice = slicer.LinearSlice(slicePair);
                            if (slice.GetGameObjects().Count > 0)
                            {
                                CopyTracker(slice, slicer);
                            }
                            ;
                        }

                        trackerList.Remove(slicer);
                    }
                    else
                    {
                        if (tracker.tracking == false && tracker.lastPosition != null)
                        {
                            if (trackedPos.x != tracker.lastPosition.x && trackedPos.y != tracker.lastPosition.y)
                            {
                                bool collision = Math2D.LineIntersectPoly(new Pair2D(trackedPos, tracker.lastPosition), slicer.shape.GetLocal());

                                if (collision)
                                {
                                    tracker.firstPosition = tracker.lastPosition;

                                    tracker.tracking = true;
                                }
                            }
                        }
                    }
                }

                if (tracker != null)
                {
                    tracker.lastPosition = trackedPos;
                }
            }
        }
        public List <Slice2D> Update(Vector2D position, float timer, Slice2DLayer layer)
        {
            List <Slice2D> result = new List <Slice2D>();

            foreach (Slicer2D slicer in Slicer2D.GetListCopy())
            {
                if (slicer.MatchLayers(layer) == false)
                {
                    continue;
                }

                SlicerTrailObject trail = null;
                trailList.TryGetValue(slicer, out trail);

                if (trail == null)
                {
                    trail = new SlicerTrailObject();
                    trailList.Add(slicer, trail);
                }

                if (trail.lastPosition != null)
                {
                    if (Vector2D.Distance(trail.lastPosition, position) > 0.05f)
                    {
                        trail.pointsList.Add(new TrailPoint(position, timer));
                    }
                }
                else
                {
                    trail.pointsList.Add(new TrailPoint(position, timer));
                }

                if (trail.pointsList.Count > 1)
                {
                    foreach (TrailPoint trailPoint in new List <TrailPoint>(trail.pointsList))
                    {
                        if (trailPoint.Update() == false)
                        {
                            trail.pointsList.Remove(trailPoint);
                        }
                    }

                    List <Vector2D> points = new List <Vector2D>();
                    foreach (TrailPoint trailPoint in trail.pointsList)
                    {
                        points.Add(trailPoint.position);
                    }

                    Slicer2D.complexSliceType = Slicer2D.SliceType.Regular;
                    Slice2D slice = slicer.ComplexSlice(points);
                    if (slice.GetGameObjects().Count > 0)
                    {
                        trailList.Remove(slicer);

                        result.Add(slice);
                    }
                    ;
                }

                trail.lastPosition = position;
            }

            return(result);
        }
Example #26
0
        // Polygon Slice - TODO: Return No Polygon if it's eaten by polygon slice
        static public Slice2D PolygonSlice(Polygon2D polygon, Polygon2D slicePolygon)
        {
            List <Polygon2D> holes = polygon.holesList;

            polygon           = new Polygon2D(new List <Vector2D>(polygon.pointsList));
            polygon.holesList = holes;

            slicePolygon = new Polygon2D(new List <Vector2D>(slicePolygon.pointsList));

            Slice2D result = Slice2D.Create(null, polygon);

            Slicer2D.SliceType tempSliceType = Slicer2D.complexSliceType;
            Slicer2D.complexSliceType = Slicer2D.SliceType.SliceHole;

            slicePolygon.Normalize();
            polygon.Normalize();

            // Eat a polygon completely
            // Complex Slicer does not register slice in this case
            if (slicePolygon.PolyInPoly(polygon) == true)
            {
                result.AddPolygon(polygon);
                return(result);
            }

            // Cut a hole inside (how does this work if it collides with other hole?)
            if (polygon.PolyInPoly(slicePolygon) == true)
            {
                polygon.AddHole(slicePolygon);
                result.AddPolygon(polygon);

                Math2D.Distance distance = Math2D.Distance.PolygonToPolygon(polygon, slicePolygon);
                if (distance != null && distance.value < precision)
                {
                    return(SlicePolygonFromEdge(polygon, slicePolygon));
                }

                return(result);
            }

            // Act as Regular Slice
            Vector2D startPoint = null;

            foreach (Vector2D id in slicePolygon.pointsList)
            {
                if (polygon.PointInPoly(id) == false)
                {
                    startPoint = id;
                    break;
                }
            }

            if (startPoint == null)
            {
                if (Math2D.PolyIntersectPoly(polygon, slicePolygon))
                {
                    return(SlicePolygonFromEdge(polygon, slicePolygon));
                }

                if (Slicer2D.Debug.enabled)
                {
                    Debug.LogWarning("Starting Point Error In PolygonSlice");
                }
                return(result);
            }

            slicePolygon.pointsList = Vector2DList.GetListStartingPoint(slicePolygon.pointsList, startPoint);

            /*
             * List<Vector2D> s = new List<Vector2D> ();
             * foreach (Pair2D pair in Pair2D.GetList(polygonSlice.pointsList, false)) {
             *      List<Vector2D> stackList = polygon.GetListSliceIntersectPoly(pair);
             *      stackList = Vector2DList.GetListSortedToPoint (stackList, pair.A);
             *      Vector2D old = pair.A;
             *      s.Add (old);
             *
             *      foreach (Vector2D id in stackList) {
             *              s.Add (new Vector2D((old.GetX() + id.GetX()) / 2, (old.GetY() + id.GetY()) / 2));
             *              old = id;
             *      }
             * }
             *
             * polygonSlice.pointsList = s;
             */

            slicePolygon.AddPoint(startPoint);

            // Not Necessary
            if (polygon.SliceIntersectPoly(slicePolygon.pointsList) == false)
            {
                return(result);
            }

            // Slice More Times?
            result = ComplexSlicer.Slice(polygon, new List <Vector2D> (slicePolygon.pointsList));

            if (result.GetPolygons().Count < 1)
            {
                if (Slicer2D.Debug.enabled)
                {
                    Debug.LogWarning("Slicer2D: Returns Empty Polygon Slice");
                }
            }

            Slicer2D.complexSliceType = tempSliceType;

            return(result);
        }
Example #27
0
        // Create Polygon Inside? Extended Method?
        public static Slice2D SlicePolygonInside(Polygon2D polygon, List <Vector2D> slice)
        {
            Slice2D result = Slice2D.Create(null, slice);

            if (Slicer2D.complexSliceType != Slicer2D.SliceType.SliceHole)
            {
                return(result);
            }

            List <List <Vector2D> > checkSlices = new List <List <Vector2D> >();
            List <Vector2D>         curSlice    = null;

            foreach (Vector2D p in slice)
            {
                if (polygon.PointInPoly(p))
                {
                    if (curSlice == null)
                    {
                        curSlice = new List <Vector2D>();
                        checkSlices.Add(curSlice);
                    }
                    curSlice.Add(p);
                }
                else
                {
                    curSlice = null;
                }
            }

            //bool createPoly = false;
            List <Polygon2D> newPolygons = new List <Polygon2D>();
            Polygon2D        newPoly     = null;

            foreach (List <Vector2D> checkSlice in checkSlices)
            {
                foreach (Pair2D pairA in Pair2D.GetList(checkSlice, false))
                {
                    foreach (Pair2D pairB in Pair2D.GetList(checkSlice, false))
                    {
                        Vector2D intersection = Math2D.GetPointLineIntersectLine(pairA, pairB);
                        if (intersection != null && (pairA.A != pairB.A && pairA.B != pairB.B && pairA.A != pairB.B && pairA.B != pairB.A))
                        {
                            if (newPoly == null)
                            {
                                newPoly = new Polygon2D();
                                newPolygons.Add(newPoly);
                                newPoly.AddPoint(intersection);
                            }
                            else
                            {
                                //newPoly.AddPoint (intersection);
                                newPoly = null;
                            }
                        }
                    }
                    if (newPoly != null)
                    {
                        newPoly.AddPoint(pairA.B);
                    }
                }
            }

            foreach (Polygon2D poly in new List <Polygon2D>(newPolygons))
            {
                if (poly.pointsList.Count < 3)
                {
                    newPolygons.Remove(poly);
                    continue;
                }
            }

            if (newPolygons.Count > 0)
            {
                result.AddPolygon(polygon);

                foreach (Polygon2D poly in newPolygons)
                {
                    List <Polygon2D> polys = new List <Polygon2D> (polygon.holesList);
                    foreach (Polygon2D hole in polys)
                    {
                        if (poly.PolyInPoly(hole) == true)
                        {
                            polygon.holesList.Remove(hole);
                            poly.AddHole(hole);
                        }
                    }

                    polygon.AddHole(poly);
                }

                return(result);
            }

            return(result);
        }
Example #28
0
        static public Slice2D SlicePolygonFromEdge(Polygon2D polygon, Polygon2D slicePolygon)
        {
            Slice2D result = Slice2D.Create(null, polygon);

            Vector2D startA = null;
            Vector2D startB = null;

            Pair2D pairA = Pair2D.Zero();

            pairA.A = polygon.pointsList.Last();

            for (int i = 0; i < polygon.pointsList.Count; i++)
            {
                pairA.B = polygon.pointsList[i];

                Pair2D pairB = Pair2D.Zero();
                pairB.A = slicePolygon.pointsList.Last();

                float minDistance = float.PositiveInfinity;                 // inf

                for (int x = 0; x < slicePolygon.pointsList.Count; x++)
                {
                    pairB.B = slicePolygon.pointsList[x];

                    double dist = Math2D.Distance.PointToLine(pairB.A, pairA);

                    if (dist < precision)                       // precision
                    {
                        float distance = (float)Vector2D.Distance(pairA.A, pairB.A);

                        if (distance < minDistance)
                        {
                            minDistance = distance;

                            startA = pairA.B;
                            startB = pairB.B;
                        }
                    }

                    pairB.A = pairB.B;
                }

                pairA.A = pairA.B;
            }

            if (startB == null || startB == null)
            {
                Debug.LogWarning("Slicer2D: PolygonSlice From Edge Error");
                return(result);
            }

            polygon.pointsList      = Vector2DList.GetListStartingPoint(polygon.pointsList, startA);
            slicePolygon.pointsList = Vector2DList.GetListStartingPoint(slicePolygon.pointsList, startB);

            slicePolygon.pointsList.Reverse();

            Polygon2D poly = new Polygon2D();

            foreach (Vector2D vec in polygon.pointsList)
            {
                poly.AddPoint(vec);
            }

            foreach (Vector2D vec in slicePolygon.pointsList)
            {
                poly.AddPoint(vec);
            }

            result.AddPolygon(poly);

            return(result);
        }
Example #29
0
        static private Slice2D MultipleSlice(Polygon2D polygon, List <Vector2D> slice)
        {
            Slice2D result = Slice2D.Create(null, slice);

            List <ComplexSlicerSplit> slices = ComplexSlicerSplit.GetSplitSlices(polygon, slice);

            if (slices.Count < 1)
            {
                return(result);
            }

            // Adjusting split lines before performing convex split
            result.AddPolygon(polygon);

            foreach (ComplexSlicerSplit id in slices)
            {
                if (id.points.Count > 1)
                {
                    foreach (Vector2D p in id.points)
                    {
                        result.AddCollision(p);
                    }

                    // Sclice line points generated from intersections list
                    Vector2D vec0 = id.points.First();
                    vec0.Push(Vector2D.Atan2(vec0, id.points[1]), precision);

                    Vector2D vec1 = id.points.Last();
                    vec1.Push(Vector2D.Atan2(vec1, id.points[id.points.Count - 2]), precision);

                    // For each in polygons list attempt convex split
                    List <Polygon2D> resultPolygons = new List <Polygon2D>(result.GetPolygons());                   // necessary?
                    foreach (Polygon2D poly in resultPolygons)
                    {
                        Slice2D resultList = SingleSlice(poly, id);

                        if (resultList.GetPolygons().Count > 0)
                        {
                            if (resultList.slices.Count > 0)
                            {
                                foreach (List <Vector2D> resultSlice in resultList.slices)
                                {
                                    result.AddSlice(resultSlice);
                                }
                            }

                            foreach (Polygon2D resultPoly in resultList.GetPolygons())
                            {
                                result.AddPolygon(resultPoly);
                            }

                            // If it's possible to perform convex split, remove parent polygon from result list
                            result.RemovePolygon(poly);
                        }
                    }
                }
            }

            //Debug.Log("1 " + slices[0].Count + " " + slices[0][0].ToVector2() + " " + slices[0][1].ToVector2());
            //Debug.Log("2 " + slices[1].Count + " " + slices[1][0].ToVector2() + " " + slices[1][1].ToVector2());

            // if exist then remove slice was not performed
            result.RemovePolygon(polygon);
            return(result);
        }
Example #30
0
            static public Slice2D Slice(Polygon2D polygon, List <Vector2D> slice, ComplexCollision collisionSlice)
            {
                Slice2D result = Slice2D.Create(null, slice);

                if (Slicer2D.complexSliceType == Slicer2D.SliceType.Regular)
                {
                    return(result);
                }

                Polygon2D polyA = new Polygon2D();
                Polygon2D polyB = new Polygon2D(polygon.pointsList);

                Polygon2D holeA = polygon.PointInHole(slice.First());
                Polygon2D holeB = polygon.PointInHole(slice.Last());

                if (holeA == null || holeB == null)
                {
                    // Shouldn't really happen no more
                    if (Slicer2D.Debug.enabled)
                    {
                        Debug.LogWarning("Slicer: ERROR Split");
                    }
                    return(result);
                }

                List <Vector2D> slices = new List <Vector2D>(collisionSlice.GetPointsInsidePlus());

                List <Vector2D> pointsA = Vector2DList.GetListStartingIntersectSlice(holeA.pointsList, slice);
                List <Vector2D> pointsB = Vector2DList.GetListStartingIntersectSlice(holeB.pointsList, slice);

                polyA.AddPoints(pointsA);

                if (collisionSlice.GetPointsInside().Count > 0)
                {
                    if (Vector2D.Distance(pointsA.Last(), collisionSlice.Last()) < Vector2D.Distance(pointsA.Last(), collisionSlice.First()))
                    {
                        collisionSlice.Reverse();
                    }

                    polyA.AddPoints(collisionSlice.GetPointsInside());
                }

                polyA.AddPoints(pointsB);

                if (collisionSlice.GetPointsInside().Count > 0)
                {
                    collisionSlice.Reverse();

                    polyA.AddPoints(collisionSlice.GetPointsInside());
                }

                foreach (Polygon2D poly in polygon.holesList)
                {
                    if (poly != holeA && poly != holeB)
                    {
                        polyB.AddHole(poly);
                    }
                }

                polyB.AddHole(polyA);
                result.AddPolygon(polyB);

                result.AddSlice(slices);
                return(result);
            }