Polygon Orientation

Here's a code snippet for adding automatic orientation into Farseer Physics.



This is what I edited in Geometry.cs:

protected void SetVertices(Vertices vertices) {
            if (!VerticesCCW(vertices))
            {
                vertices.Reverse();
            }
            _localVertices = new Vertices(vertices);
            _worldVertices = new Vertices(vertices);
            _aabb.Update(_localVertices);
        }

        /// <summary>
        /// Determine if the passed in
        /// list of vertices is oriented
        /// CCW.
        /// </summary>
        /// <param name="vertices">Vertices list</param>
        /// <returns>true if oriented counter-clockwise</returns>
        protected bool VerticesCCW(Vertices vertices)
        {
            float sum = 0;
            Vector2[] verts = vertices.ToArray();
            // the sign of the 'area' of the polygon is all
            // we are interested in.
            for (int i = 0; i <verts.Length; i++)
            {
                int nextIndex = i + 1;
                if (i == verts.Length - 1)
                {
                    nextIndex = 0;
                }
                sum += (verts[i].X * verts[nextIndex].Y - verts[nextIndex].X * verts[i].Y);
            }
            return (sum <= 0)
        }