public void CenterPivot(JelloBody t)
    {
        Vector2 center = new Vector2();

        t.polyCollider.points = JelloShapeTools.RemoveDuplicatePoints(t.polyCollider.points);
        t.Shape.changeVertices(t.polyCollider.points, t.Shape.InternalVertices);


        center = JelloShapeTools.FindCenter(t.Shape.EdgeVertices);        //using vertices instead of collider.points because need of assigning entire array at once


        if (t.meshLink != null)
        {
            MonoBehaviour monoBehavior;
            if (t.meshLink.UpdatePivotPoint(center, out monoBehavior))
            {
                EditorUtility.SetDirty(monoBehavior);
            }
        }

        for (int i = 0; i < t.Shape.VertexCount; i++)
        {
            t.Shape.setVertex(i, t.Shape.getVertex(i) - center);
        }

        t.polyCollider.points = t.Shape.EdgeVertices;

        t.transform.position += (Vector3)JelloVectorTools.rotateVector(new Vector2(center.x * t.Scale.x, center.y * t.Scale.y), t.Angle);
        if (t.transform.childCount > 0)
        {
            for (int i = 0; i < t.transform.childCount; i++)
            {
                t.transform.GetChild(i).position -= (Vector3)center;
            }
        }

        if (t.JointCount > 0)
        {
            for (int i = 0; i < t.JointCount; i++)
            {
                t.GetJoint(i).localAnchorA -= center;
            }
        }

        if (t.AttachPointCount > 0)
        {
            for (int i = 0; i < t.AttachPointCount; i++)
            {
                t.GetAttachPoint(i).point -= center;
            }
        }

        t.updateGlobalShape(true);

        EditorUtility.SetDirty(t);

        pivot = Vector2.zero;
    }
Esempio n. 2
0
    /// <summary>
    /// Finish adding vertices to this JeloClosedShape, and choose whether to convert into local space.
    /// JelloClosedShape.winding and JelloClosedShape.Triangles will be set.
    /// Make sure there are no duplicate points before calling this. use JelloShapeTools.RemoveDuplicatePoints().
    /// </summary>
    /// <param name="recenter">whether to convert the positions of the JelloClosedShape into local space.</param>
    ///
    /// <dl class="example"><dt>Example</dt></dl>
    /// ~~~{.c}
    /// //Create a closed shape square
    /// JelloClosedShape shape = new JelloClosedShape();
    ///
    /// shape.begin();
    ///
    /// shape.addPoint(new Vector2(-1,1));	//top left
    /// shape.addPoint(new Vector2(-1,1));	//top right
    /// shape.addPoint(new Vector2(-1,1));	//bottom right
    /// shape.addPoint(new Vector2(-1,1));	//bottom left
    ///
    /// shape.finish();
    /// ~~~
    public void finish(bool recenter = true)
    {
        if (mInternalVertices != null && mInternalVertices.Length > 0)
        {
            //dont allow duplicate points
            mInternalVertices = JelloShapeTools.RemoveDuplicatePoints(mInternalVertices);

            //dont allow points outside of the perimiter
            for (int i = 0; i < mInternalVertices.Length; i++)
            {
                if (!JelloShapeTools.Contains(mEdgeVertices, mInternalVertices[i]))
                {
                    mInternalVertices[i] = Vector2.one * Mathf.Infinity;
                }
            }
            //dont allow points on the perimiter. (this will also remove any null points)
            mInternalVertices = JelloShapeTools.RemovePointsOnPerimeter(mEdgeVertices, mInternalVertices);
        }

        mCenter = JelloShapeTools.FindCenter(mEdgeVertices);

        if (recenter)
        {
            // now subtract this from each element, to get proper "local" coordinates.
            for (int i = 0; i < mEdgeVertices.Length; i++)
            {
                mEdgeVertices[i] -= mCenter;
            }
            if (mInternalVertices != null)
            {
                for (int i = 0; i < mInternalVertices.Length; i++)
                {
                    mInternalVertices[i] -= mCenter;
                }
            }
        }

        if (JelloShapeTools.HasClockwiseWinding(mEdgeVertices))
        {
            winding = Winding.Clockwise;
        }
        else
        {
            winding = Winding.CounterClockwise;
        }



        Triangulate();
    }