Problem with PolygonBody

Topics: Developer Forum, User Forum
Feb 17, 2008 at 4:06 PM
Hi all.
At first, thanks for creating this engine, I like it very much. But I have a problem with polygon-bodies.
Collisiondetection for circles and rectangles works properly. But for polygon-bodies collisiondetection
with colliding objects works fine, as long as the IsStatic-property is true. But turning it off leads to other
objects sliding trough the polygon-object. Additionally, even if IsStatic is false gravity doesn't effect
polygon-objects. I tried it with simple polygons (triangle) and more complex ones (over 200 vertices),
it's always the same problem. Unfortunately I can't find a demo where polygon-bodies are used.

Has anybody an idea, or even the same problems?
Thanks for your help.
Coordinator
Feb 18, 2008 at 11:00 AM
Can you show the code you are using to create you polygons??
Feb 18, 2008 at 12:50 PM
Sure. This is the Code in the Initialize method:

//create triangle
Vertices verts1 = new Vertices();
verts1.Add(new Vector2(0, -50));
verts1.Add(new Vector2(-50, 50));
verts1.Add(new Vector2(50, 50));
verts1.Add(new Vector2(0, -50));
triBody = BodyFactory.Instance.CreatePolygonBody(verts1, 1);
triBody.Position = new Vector2(800, 600);
triBody.IsStatic = false; // <--- switching this to false causes the problems
triGeom = GeomFactory.Instance.CreatePolygonGeom(physicsSimulator, triBody, verts1, 0);
triGeom.CollisionResponseEnabled = true;

//create complex polygon
Vertices verts4 = readXml("shapeXml"); // <-- method to read vertices from an xml-file, works properly. it's tested
shapeBody = BodyFactory.Instance.CreatePolygonBody(verts4, 1);
shapeBody.Position = new Vector2(370, 575);
shapeBody.IsStatic = true; // <--- same problem with this body
shapeGeom = GeomFactory.Instance.CreatePolygonGeom(physicsSimulator, shapeBody, verts4, 0);
shapeGeom.CollisionResponseEnabled = true;

I used your QuickStartDemo for testing it.
Coordinator
Feb 18, 2008 at 7:59 PM
Edited Feb 18, 2008 at 8:00 PM
First, in the triangle code you don't need that 4th duplicate vert.

Second, Farseer prefers more verts, even on straight edges. Try using the new vertices.SubDivideEdges() method just before sending your verts to the factory object. This method will divide each side of your polygon into smaller segments by adding verts. This should improve you collision detection.

-Jeff
Feb 19, 2008 at 10:18 AM
Sadly this doesn't change anything. Tried different values for maxEdgeLength.
And it's very strange, that there is no physicseffect on those bodies. Even, if
I try to pick them with the mouse, they don't move. A friend and I are developing
a 2D-shooter and really would like to use your engine for the physicspart, would
be a pity, if we don't get it running. Do you have any working samples with
polygon-bodies? Just a simple one.

Greetings
Daniel
Coordinator
Feb 19, 2008 at 4:34 PM
Hmm, just looked at your code again..

BodyFactory.Instance.CreatePolygonBody(verts1, 1);


This needs to be:
BodyFactory.Instance.CreatePolygonBody(physicsSimulator, verts1, 1), doesn't it?

Another helpful thing is to create debug view of you bodies and geoms. This will very quickly show you what is going on. There is an example of a debug viewer in the XNA Demos project.
Feb 19, 2008 at 7:44 PM
Omg. What a noobish mistake. Now it works.
The debug view is a good thing. Helped me to solve another problem, where sprite and the geom-object
were not in the same place, because in the draw-method I set the origin-vector to (texture.width/2, texture.height/2),
but it must be the centroid.

Now we can concentrate on our game.
Tanks for your help.
Greetings Daniel.
Mar 5, 2008 at 3:10 AM
Hi,

Could you please point me to this debug view sample you're referring to? I'm at a point at which this sort of thing would really help me debug some issues I'm having. I looked through the XNA site and couldn't find it.

Thanks!
Coordinator
Mar 5, 2008 at 11:24 AM
Edited Mar 5, 2008 at 11:24 AM
It's in the XNA samples project. It's just a class.