FP3 FixtureFactory.CreateEllipse

Topics: Developer Forum
Apr 27, 2010 at 1:12 PM

Hi,

Playing with FP3; seems like FixtureFactory.CreateEllipse creates an ellipse with vertices in CW order, rather than CCW as expected, causing an exception when Farseer attempts to create the shape.

The fix:

--- a/SourceFiles/FP3.0/Common/PolygonTools.cs
+++ b/SourceFiles/FP3.0/Common/PolygonTools.cs
@@ -150,7 +150,7 @@ namespace FarseerPhysics.Common
             vertices.Add(new Vector2(xRadius, 0));
             for (int i = 1; i < numberOfEdges; i++)
                 vertices.Add(new Vector2(xRadius * (float) Math.Cos(stepSize * i),
-                                         -yRadius * (float) Math.Sin(stepSize * i)));
+                                         yRadius * (float) Math.Sin(stepSize * i)));
 
             return vertices;
         }

Apr 27, 2010 at 3:08 PM
Edited Apr 27, 2010 at 3:14 PM

I created a method for checking if the vertices are clockwise or counterclockwise... well I ported it from C++ from some code shown in the gamedev forums:

public static float PerpDot(Vector2 a, Vector2 b)

{
    return -a.Y * b.X + a.X * b.Y;
}

Given localPoints an array of Vector2 for creating the shape:

if (localPoints.Count() > 3)
{
    float d = Globals.PerpDot(localPoints[1] - localPoints[0], localPoints[2] - localPoints[1]);
    if (d < 0) // Is clockwise, so we need to reverse them
    {
        Array.Reverse(localPoints);
    }
}
Hope this helps.

Developer
Apr 27, 2010 at 4:33 PM

All the PolygonTools except CreateRectangle and CreateEdge need to be modified to work with 3.x.

I'm pretty sure I posted that as an issue, but I'll double check.