コード例 #1
0
    private void LinearSlice(Pair2f slice)
    {
        List <Slice2D> results = Slicer2D.LinearSliceAll(slice, sliceLayer);

        if (addForce == true)
        {
            float sliceRotation = Vector2f.Atan2(slice.B, slice.A);

            foreach (Slice2D id in results)
            {
                foreach (GameObject gameObject in id.gameObjects)
                {
                    Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
                    if (rigidBody2D)
                    {
                        foreach (Vector2f p in id.collisions)
                        {
                            Vector2 force = new Vector2(Mathf.Cos(sliceRotation) * addForceAmount, Mathf.Sin(sliceRotation) * addForceAmount);
                            rigidBody2D.AddForceAtPosition(force, p.Get());
                        }
                    }
                }
            }
        }

        cutterCount--;
    }
コード例 #2
0
    private static void DrawLine_Smooth_Matrix(Pair2f pair, float z = 0f)
    {
        float size = lineWidth;
        float pi2  = Mathf.PI / 2;

        float rot = Vector2f.Atan2(pair.A, pair.B);

        Vector2f A1 = new Vector2f(pair.A);
        Vector2f A2 = new Vector2f(pair.A);
        Vector2f B1 = new Vector2f(pair.B);
        Vector2f B2 = new Vector2f(pair.B);

        A1.Push(rot + pi2, size);
        A2.Push(rot - pi2, size);
        B1.Push(rot + pi2, size);
        B2.Push(rot - pi2, size);

        GL.TexCoord2(0, 0);
        GL.Vertex3(B1.GetX(), B1.GetY(), z);
        GL.TexCoord2(1, 0);
        GL.Vertex3(A1.GetX(), A1.GetY(), z);
        GL.TexCoord2(1, 1);
        GL.Vertex3(A2.GetX(), A2.GetY(), z);
        GL.TexCoord2(0, 1);
        GL.Vertex3(B2.GetX(), B2.GetY(), z);
    }
コード例 #3
0
ファイル: Rope2D.cs プロジェクト: 1004019267/InciseAndMerge
    void Start()
    {
        Vector2f position = new Vector2f(anchorBody.transform.position);

        GameObject prev = anchorBody;

        int        ropeId  = 1;
        GameObject gObject = null;

        while (Vector2f.Distance(position, new Vector2f(connectedBody.transform.position)) > .5f)
        {
            float direction = Vector2f.Atan2(new Vector2f(connectedBody.transform.position), position);

            gObject = new GameObject();
            gObject.AddComponent <HingeJoint2D>().connectedBody = prev.GetComponent <Rigidbody2D>();

            gObject.transform.parent   = transform;
            gObject.transform.position = position.Get();
            gObject.name = "Rope " + ropeId;
            gObject.AddComponent <JointRenderer2D> ().color   = color;
            gObject.AddComponent <CircleCollider2D> ().radius = .25f;

            nodes.Add(gObject);

            position.Push(direction, .5f);

            prev = gObject;
            ropeId++;
        }

        if (gameObject != null)
        {
            gObject.AddComponent <HingeJoint2D>().connectedBody = connectedBody.GetComponent <Rigidbody2D>();
        }
    }
コード例 #4
0
    private void ComplexSlice(List <Vector2f> slice)
    {
        List <Slice2D> results = Slicer2D.ComplexSliceAll(slice, sliceLayer);

        if (addForce == true)
        {
            foreach (Slice2D id in results)
            {
                foreach (GameObject gameObject in id.gameObjects)
                {
                    Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
                    if (rigidBody2D)
                    {
                        List <Pair2f> list     = Pair2f.GetList(id.collisions);
                        float         forceVal = 1.0f / list.Count;
                        foreach (Pair2f p in list)
                        {
                            float   sliceRotation = -Vector2f.Atan2(p.B, p.A);
                            Vector2 force         = new Vector2(Mathf.Cos(sliceRotation) * addForceAmount, Mathf.Sin(sliceRotation) * addForceAmount);
                            rigidBody2D.AddForceAtPosition(forceVal * force, (p.A.Get() + p.B.Get()) / 2f);
                        }
                    }
                }
            }
        }
    }
コード例 #5
0
    private void UpdateComplex(Vector2f pos)
    {
        if (Input.GetMouseButtonDown(0))
        {
            complexPairs.Clear();
            complexPairs.Add(pos);
        }

        if (Input.GetMouseButton(0))
        {
            Vector2f posMove = new Vector2f(complexPairs.Last());
            while ((Vector2f.Distance(posMove, pos) > minVertsDistance))
            {
                float direction = Vector2f.Atan2(pos, posMove);
                posMove.Push(direction, minVertsDistance);
                complexPairs.Add(new Vector2f(posMove));
            }

            mouseDown = true;
        }

        if (mouseDown == true && Input.GetMouseButton(0) == false)
        {
            mouseDown = false;
            Slicer2D.complexSliceType = complexSliceType;
            ComplexSlice(complexPairs);
            complexEvents.Add(complexPairs);
        }
    }
コード例 #6
0
    public void LateUpdate()
    {
        complexEvents.Clear();

        // Checking mouse press and release events to get linear slices based on input
        Vector2f pos = new Vector2f(Camera.main.ScreenToWorldPoint(Input.mousePosition));

        if (Input.GetMouseButtonDown(0))
        {
            complexPairs.Clear();
            complexPairs.Add(pos);
        }

        if (Input.GetMouseButton(0))
        {
            Vector2f posMove = new Vector2f(complexPairs.Last());
            while ((Vector2f.Distance(posMove, pos) > minVertsDistance))
            {
                float direction = Vector2f.Atan2(pos, posMove);
                posMove.Push(direction, minVertsDistance);
                complexPairs.Add(new Vector2f(posMove));
            }
            mouseDown = true;
        }

        if (mouseDown == true && Input.GetMouseButton(0) == false)
        {
            mouseDown = false;
            Slicer2D.complexSliceType = complexSliceType;
            ComplexSlice(complexPairs);
            complexEvents.Add(complexPairs);
        }
    }
コード例 #7
0
 // Not finished - still has some artifacts
 public static void PreparePolygon(Polygon polygon)
 {
     foreach (Pair3f pA in Pair3f.GetList(polygon.pointsList))
     {
         foreach (Pair3f pB in  Pair3f.GetList(polygon.pointsList))
         {
             if (pA.B != pB.B && Vector2f.Distance(pA.B, pB.B) < precision)
             {
                 pA.B.Push(Vector2f.Atan2(new Vector2f(pA.A), new Vector2f(pA.B)), precision);
                 pA.B.Push(Vector2f.Atan2(new Vector2f(pA.B), new Vector2f(pA.C)), -precision);
                 pB.B.Push(Vector2f.Atan2(new Vector2f(pB.A), new Vector2f(pB.B)), precision);
                 pB.B.Push(Vector2f.Atan2(new Vector2f(pB.B), new Vector2f(pB.C)), -precision);
             }
         }
     }
 }
コード例 #8
0
    private void ExplodeAll()
    {
        List <Slice2D> results = Slicer2D.ExplodeAll(sliceLayer);

        if (addForce == true)
        {
            foreach (Slice2D id in results)
            {
                foreach (GameObject gameObject in id.gameObjects)
                {
                    Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
                    if (rigidBody2D)
                    {
                        float sliceRotation = Vector2f.Atan2(new Vector2f(0, 0), new Vector2f(gameObject.transform.position));
                        Rect  rect          = Polygon.CreateFromCollider(gameObject).GetBounds();
                        rigidBody2D.AddForceAtPosition(new Vector2(Mathf.Cos(sliceRotation) * addForceAmount / 10f, Mathf.Sin(sliceRotation) * addForceAmount / 10f), rect.center);
                    }
                }
            }
        }
    }
コード例 #9
0
    private void ComplexSlice(List <Vector2f> slice)
    {
        List <Slice2D> results = Slicer2D.ComplexSliceAll(slice, null);

        if (addForce == true)
        {
            foreach (Slice2D id in results)
            {
                foreach (GameObject gameObject in id.gameObjects)
                {
                    Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
                    if (rigidBody2D)
                    {
                        foreach (Pair2f p in Pair2f.GetList(id.collisions))
                        {
                            float sliceRotation = Vector2f.Atan2(p.B, p.A);
                            rigidBody2D.AddForceAtPosition(new Vector2(Mathf.Cos(sliceRotation) * addForceAmount, Mathf.Sin(sliceRotation) * addForceAmount), (p.A.Get() + p.B.Get()) / 2f);
                        }
                    }
                }
            }
        }
    }
コード例 #10
0
    static public void DrawStrippedLine(List <Vector2f> pointsList, float minVertsDistance, float z = 0f, bool full = false, Vector2f offset = null)
    {
        if (offset == null)
        {
            offset = new Vector2f(0, 0);
        }

        Vector2f vA = null, vB = null;

        if (setBorder == true)
        {
            Color tmcColor = setColor;
            float tmpWidth = lineWidth;

            GL.PushMatrix();
            SetColor(Color.black);
            lineMaterial.SetPass(0);
            GL.Begin(GL.QUADS);

            lineWidth = 2f * tmpWidth;

            foreach (Pair2f id in Pair2f.GetList(pointsList, full))
            {
                vA = new Vector2f(id.A.Get() + offset.Get());
                vB = new Vector2f(id.B.Get() + offset.Get());

                vA.Push(Vector2f.Atan2(id.A, id.B), -minVertsDistance / 4);
                vB.Push(Vector2f.Atan2(id.A, id.B), minVertsDistance / 4);

                DrawLine_Smooth_Matrix(new Pair2f(vA, vB), z);
            }

            GL.End();
            GL.PopMatrix();

            GL.PushMatrix();
            SetColor(Color.black);
            lineEndMaterial.SetPass(0);
            GL.Begin(GL.QUADS);

            lineWidth = 2f * tmpWidth;

            foreach (Pair2f id in Pair2f.GetList(pointsList, full))
            {
                vA = new Vector2f(id.A.Get() + offset.Get());
                vB = new Vector2f(id.B.Get() + offset.Get());

                vA.Push(Vector2f.Atan2(id.A, id.B), -minVertsDistance / 4);
                vB.Push(Vector2f.Atan2(id.A, id.B), minVertsDistance / 4);

                DrawLineEnd_Smooth_Matrix(new Pair2f(vA, vB), z);
            }

            GL.End();
            GL.PopMatrix();

            SetColor(tmcColor);
            lineWidth = tmpWidth;
        }

        GL.PushMatrix();
        lineMaterial.SetPass(0);
        GL.Begin(GL.QUADS);

        foreach (Pair2f id in Pair2f.GetList(pointsList, full))
        {
            vA = new Vector2f(id.A.Get() + offset.Get());
            vB = new Vector2f(id.B.Get() + offset.Get());

            vA.Push(Vector2f.Atan2(id.A, id.B), -minVertsDistance / 4);
            vB.Push(Vector2f.Atan2(id.A, id.B), minVertsDistance / 4);

            DrawLine_Smooth_Matrix(new Pair2f(vA, vB), z);
        }

        GL.End();
        GL.PopMatrix();
    }
コード例 #11
0
    // Linear Slice
    static public Slice2D Slice(Polygon polygon, Pair2f slice)
    {
        Slice2D result = Slice2D.Create();

        // Normalize into clockwise
        polygon.Normalize();

        // Getting the list of intersections
        List <Vector2f> intersections = polygon.GetListSliceIntersectPoly(slice);

        // Sorting intersections from one point
        intersections = VectorList2f.GetListSortedToPoint(intersections, slice.A);

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

        List <Pair2f> collisionList = new List <Pair2f>();

        // Dividing intersections into single slices - This method doesn't look like very reliable!!!
        // Optimize this (polygon.PointInPoly) line // Fix this nonsense!!!
        foreach (Pair2f p in Pair2f.GetList(intersections, false))
        {
            if (polygon.PointInPoly(new Vector2f((p.B.GetX() + p.A.GetX()) / 2, (p.B.GetY() + p.A.GetY()) / 2)) == true)
            {
                collisionList.Add(p);
                intersections.Remove(p.A);
                intersections.Remove(p.B);
            }
        }

        result.AddPolygon(polygon);

        // Slice line points generated from intersections list
        foreach (Pair2f id in collisionList)
        {
            result.AddCollision(id.A);
            result.AddCollision(id.B);

            Vector2f vec0 = new Vector2f(id.A);
            Vector2f vec1 = new Vector2f(id.B);

            float rot = Vector2f.Atan2(vec0, vec1);

            // Slightly pushing slice line so it intersect in all cases
            vec0.Push(rot, precision);
            vec1.Push(rot, -precision);

            // For each in polygons list attempt convex split
            foreach (Polygon poly in (new List <Polygon>(result.polygons)))
            {
                Slice2D resultList = SingleSlice(poly, new Pair2f(vec0, vec1));

                if (resultList.polygons.Count > 0)
                {
                    foreach (Polygon i in resultList.polygons)
                    {
                        result.AddPolygon(i);
                    }

                    // If it's possible to perform splice, remove currently sliced polygon from result list
                    result.RemovePolygon(poly);
                }
            }
        }

        result.RemovePolygon(polygon);

        return(result);
    }
コード例 #12
0
ファイル: ComplexSlicer.cs プロジェクト: crazymarks/ProjectTP
    static public Slice2D Slice(Polygon polygon, List <Vector2f> slice)
    {
        Slice2D result = Slice2D.Create();

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

        // Normalize into clockwise
        polygon.Normalize();

        if (Slicer2D.complexSliceType != Slicer2D.SliceType.Regular)
        {
            result = SlicePolygonInside(polygon, slice);
            if (result.polygons.Count > 0)
            {
                return(result);
            }
        }

        // Optimization (holes?)
        // if (polygon.SliceIntersectPoly (slice) == false)
        //	return(result);

        List <List <Vector2f> > slices = new List <List <Vector2f> >();

        bool entered = polygon.PointInPoly(slice.First());

        List <Vector2f> currentSlice = new List <Vector2f> ();

        foreach (Pair2f pair in Pair2f.GetList(slice, false))
        {
            List <Vector2f> stackList = polygon.GetListSliceIntersectPoly(pair);
            stackList = VectorList2f.GetListSortedToPoint(stackList, pair.A);

            foreach (Vector2f id in stackList)
            {
                if (entered == true)
                {
                    currentSlice.Add(id);
                    slices.Add(currentSlice);
                }
                else
                {
                    currentSlice = new List <Vector2f> ();
                    currentSlice.Add(id);
                }
                entered = !entered;
            }

            if (entered == true)
            {
                currentSlice.Add(pair.B);
            }
        }

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

        foreach (List <Vector2f> id in slices)
        {
            if (id.Count > 1)
            {
                foreach (Vector2f p in id)
                {
                    result.AddCollision(p);
                }

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

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

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

                    if (resultList.polygons.Count > 0)
                    {
                        foreach (Polygon i in resultList.polygons)
                        {
                            result.AddPolygon(i);
                        }

                        // If it's possible to perform convex split, remove parent polygon from result list
                        result.RemovePolygon(poly);
                    }
                }
            }
        }
        result.RemovePolygon(polygon);
        return(result);
    }
コード例 #13
0
    // Slice From Point
    static public Slice2D SliceFromPoint(Polygon polygon, Vector2f point, float rotation)
    {
        Slice2D result = Slice2D.Create();

        // Normalize into clockwise
        polygon.Normalize();

        Vector2f sliceA = new Vector2f(point);
        Vector2f sliceB = new Vector2f(point);

        sliceA.Push(rotation, 1e+10f / 2);
        sliceB.Push(rotation, -1e+10f / 2);

        if (polygon.PointInPoly(point) == false)
        {
            return(result);
        }

        // Getting the list of intersections
        List <Vector2f> intersectionsA = polygon.GetListSliceIntersectPoly(new Pair2f(point, sliceA));
        List <Vector2f> intersectionsB = polygon.GetListSliceIntersectPoly(new Pair2f(point, sliceB));

        // Sorting intersections from one point
        if (intersectionsA.Count > 0 && intersectionsB.Count > 0)
        {
            sliceA = VectorList2f.GetListSortedToPoint(intersectionsA, point) [0];
            sliceB = VectorList2f.GetListSortedToPoint(intersectionsB, point) [0];
        }
        else
        {
            return(result);
        }

        List <Pair2f> collisionList = new List <Pair2f>();

        collisionList.Add(new Pair2f(sliceA, sliceB));

        result.AddPolygon(polygon);

        foreach (Pair2f id in collisionList)
        {
            // Sclice line points generated from intersections list
            Vector2f vec0 = new Vector2f(id.A);
            Vector2f vec1 = new Vector2f(id.B);

            float rot = Vector2f.Atan2(vec0, vec1);

            // Slightly pushing slice line so it intersect in all cases
            vec0.Push(rot, LinearSlicer.precision);
            vec1.Push(rot, -LinearSlicer.precision);

            // For each in polygons list attempt convex split
            List <Polygon> temp = new List <Polygon>(result.polygons);           // necessary?
            foreach (Polygon poly in temp)
            {
                // NO, that's the problem
                Slice2D resultList = LinearSlicer.Slice(poly, new Pair2f(vec0, vec1));

                if (resultList.polygons.Count > 0)
                {
                    foreach (Polygon i in resultList.polygons)
                    {
                        result.AddPolygon(i);
                    }

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