This project has moved and is read-only. For the latest updates, please go here.

Farseer newb question - CreatePolygonBody

Topics: Developer Forum
Apr 6, 2009 at 6:10 PM

I'm pretty much new to, Silverlight and Farseer (and game development in general, really)

I've setup a pretty basic project, and I'm just trying to get my head around things.

All I really wish to do, is have a few objects on the screen, fall & collide.

I've done this with a few rectangles, and it works fine - but i'm pulling my hair out regarding CreatePolygonBody. I just wish to add a triangle into the mix.

I've not been able to find much explaining how to do this properly in silverlight / farseer.

My question is, if i'm doing something like:


var verts = new Vector2[] { new Vector2(0, 0), new Vector2(100, 0), new Vector2(0, 100)};
var shape = new Vertices(verts);
triangleBdy =
BodyFactory.Instance.CreatePolygonBody(_physicsSimulator, shape, 1);
... etc...

What is this actually saying? is this saying "starting at 0,0 then move 100,0, them move 0,100"? Or is this just dropping fixed-points (i.e. the triangle described above) - also, which way round does the polygon get created? is it clockwise (the same as silverlight?) or counter-clockwise?

basically I can't seem to get my view & model aligned in silverlight, as the colisions are all off (only for the polygon) - here's the whole code:



var verts = new Vector2[] { new Vector2(0, 0), new Vector2(0, 100), new Vector2(100, 100), new Vector2(100, 100) };
var shape = new Vertices(verts);
triangleBdy =
BodyFactory.Instance.CreatePolygonBody(_physicsSimulator, shape, 1);
triangleBdy.Position =
new Vector2(400, 100);
var triangleGeom = GeomFactory.Instance.CreatePolygonGeom(_physicsSimulator, triangleBdy, shape, 0);



var points = new PointCollection();
new Point(0, 0));
new Point(100, 0));
new Point(100, 100));
triangle =
new Sprite(CreatePolygon(points, Colors.Red, Colors.Black, 1));
triangle.TranslateTransfrom =
new Vector(-triangle.Width / 2, -triangle.Height / 2); //This TranslateTransform was needed for the rectangles (as i'm assuming the're centre-driven, not top-left driven?)
triangle.RotateTransfrom.CenterX = triangle.Width / 2;
triangle.RotateTransfrom.CenterY = triangle.Height / 2;



public static Polygon CreatePolygon(PointCollection points......{
Polygon polygon = new Polygon();
     polygon.Points = points;

Any help would be most appreciated!


Apr 6, 2009 at 11:03 PM
The vertices should be centered around 0,0.
They are also made counter-clockwise, but the engine actually just reverse them if they are in clockwise order.

Take a look at our SimpleSamples for silverlight. They show you how Farseer Physics work together with silverlight.
Oh, you should also note that once you have created your vertices, they get centered around the centroid (center of mass), so you might want to put in an offset. This is all covered in the manual under "Known Issues".

Manual can be found online here: