Optimization of Rectangles

Feb 9, 2009 at 2:40 PM

Hi!
I created 4 rectangles in order to create a border around my application so the elements did not "fly away".
The time to startup the app was huge due to the calculation and initialization of the paths of the rectangles.

I did a little optimization in boundaryhelper so that Rectangles are treated specially:
In GetPointsForPath I added:

if (element is System.Windows.Shapes.Rectangle)
{
result.Add(new Point(left, top));
result.Add(new Point(left+width, top));
result.Add(new Point(left+width, top+height));
result.Add(new Point(left, top+height));
return result;
}

and in PhysicsSprite.xaml.cs I only allow the points optimization if it is not a Rectangle:

// let's clean up the number of points, for a little performance boost..
if (!(element is System.Windows.Shapes.Rectangle)) {...}

Another improvement could be made. In the rectangles case we could use a Rectangle instead of a Polygon:

                BodyObject = BodyFactory.Instance.CreateBody(physicsSim, 0.5f, vertices.GetMomentOfInertia());
                GeometryObject = GeomFactory.Instance.CreatePolygonGeom(physicsSim, BodyObject, vertices, 0);

could be replaced by somthing like:
                BodyObject = BodyFactory.Instance.CreateRectangleBody(physicsSim, (float)width, (float)height, (float)(width * height) * .0001F);
                GeometryObject = GeomFactory.Instance.CreateRectangleGeom(physicsSim, BodyObject, (float)width, (float)height);

bye,
Tiago Andrade e Silva

Coordinator
Feb 9, 2009 at 3:10 PM
Thanks Tiago, that is a good optimization and could probably carry over to other controls such as TextBlocks, etc - since their boundary is rectangular as well.

The only thing is, you may run into tunneling issues (object passing through or getting stuck in your bodies) with only those four points in a Polygon Geometry.

If you do see tunneling issues, you may want to either switch to the Rectangle geometry as you said, or you could call vertices.SubDivideEdges to add in some points between the edges (the Farseer manual mentions how to do this).
Feb 11, 2009 at 2:13 PM
Hi!
After applying this code I had some problems with the users control. Namely the Doll's head was placed on a different spot and strange things started to happen. Did not have time to look at the cause yet.