How to get a LoopShape into PhysicsControllerMain?

Nov 25, 2010 at 4:02 AM

I'm new to Farseer, but I've managed to get a LoopShape with a ball bouncing around inside of it, this is just with Farseer and not this PhysicsHelper (which looks great from the videos).  However, I want to use the helper.  So I did the following.  How do I get this LoopShape into the Canvas, also, the LoopShape doesn't need to be visible at all.  I need to do this progromatically because this shape will change vertices throughout the game.  LoopShape is nice becuase it joins up at all the edges and nothing gets out, so if there is a better method let me know.


            PhysicsControllerMain _physicsController = LayoutRoot.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;
            var shape = new FarseerPhysics.Collision.Shapes.LoopShape();          
            var vertsB = new Vertices();
            vertsB.Add(new Vector2(56.5f, 0.5f));
            vertsB.Add(new Vector2(155.5f, 0.5f));
            vertsB.Add(new Vector2(155.5f, 160.5f));
            vertsB.Add(new Vector2(224.5f, 160.5f));
            vertsB.Add(new Vector2(224.5f, 0.5f));
            vertsB.Add(new Vector2(343.5f, 0.5f));
            vertsB.Add(new Vector2(343.5f, 160.5f));
            vertsB.Add(new Vector2(391.5f, 160.5f));
            vertsB.Add(new Vector2(391.5f, 223.5f));
            vertsB.Add(new Vector2(343.5f, 223.5f));
            vertsB.Add(new Vector2(343.5f, 395.5f));
            vertsB.Add(new Vector2(224.5f, 395.5f));
            vertsB.Add(new Vector2(224.5f, 223.5f));
            vertsB.Add(new Vector2(155.5f, 223.5f));
            vertsB.Add(new Vector2(155.5f, 395.5f));
            vertsB.Add(new Vector2(56.5f, 395.5f));
            vertsB.Add(new Vector2(56.5f, 223.5f));
            vertsB.Add(new Vector2(0.5f, 223.5f));
            vertsB.Add(new Vector2(0.5f, 160.5f));
            vertsB.Add(new Vector2(56.5f, 160.5f));
            shape.Vertices = vertsB.ToArray();

Nov 25, 2010 at 1:38 PM

Looks like you just need to attach the Shape to a Body and then add it into the Simulation?

BodyObject = BodyFactory.CreateBody(_physicsController.Simulator);

BodyObject.BodyType = BodyType.Dynamic;

BodyObject.CreateFixture(shape, 0.1F);


Nov 25, 2010 at 7:31 PM
Edited Nov 25, 2010 at 11:01 PM

That makes sense, I guess Im still new to the API.  Now how do I take that to the next step to get that into the Canvas in the same way I get my ball in there, or at least get other things to collide with it?  I've added a ball to the canvas but it doesn't seem to bounce off of this LoopShape.  
 _physicsController.AddPhysicsBody(_ball.GetValue(PhysicsObjectMain.PhysicsObjectProperty) as PhysicsObjectMain);

FYI - I tried this with Lines to connect each point and have a ball bouncing around within those lines, it works except for some lines as noted here  With this issue I suppose I can create very small width Rectangles to get around this, but that just adds more complexity that shouldn't be needed.