Page 1 of 2

Polygon Problem

Posted: Sat May 29, 2010 8:06 am
by MrOz
I have a program that reads a file for physics information, then creates a physics scene. The problem is, when I create polygons, regardless of mass, or points, group or layer, the polygon won't collide. I tried doing a "cpSpacePointQueryFirst" over it, during the simulation with my mouse, and it doesn't register. Here is the relevent code:

Code: Select all

	ps->space = cpSpaceNew( )
	cpSpaceResizeStaticHash( ps->space, size, count )
	cpSpaceResizeActiveHash( ps->space, size, count )
	ps->space->iterations = iterations
	
	ps->staticbody = cpBodyNew( INFINITY, INFINITY )

' ... Sometime later in the code ...
					var points = child->values/2
					dim pnt(0 to points-1) as cpVect
					for i = 0 to points-1
						pnt(i).x = cdbl( *child->value[i*2] ) : pnt(i).y = cdbl( *child->value[(i*2)+1] )
					next i
					var tbody = cpBodyNew( mass, cpMomentForPoly( mass, points, @pnt(0), cpvzero ) )
					tbody->p = center
					var tshape = cpPolyShapeNew( tbody, points, @pnt(0), cpvzero )
					tshape->e = e
					tshape->u = f
					tshape->group = group
					cpSpaceAddBody( ps->space, tbody )
					cpSpaceAddShape( ps->space, tshape )
I first checked to see if the pnt() and points variables were correct, which they were, and the shape displays correctly in the simulation. What's odd, at least to me, is that "cpSpaceHashEach()" will find the shape is draw it perfectly, but the "cpSpacePointQueryFirst" won't.

When I put segments and circles into the simulation, there is absolutely no problem.

Any help would be nice. If you need to see more code, I can post more.
Thanks,
-Oz

:: EDIT ::
I should mention the size and count are 30 and 1000, respectively

::EDIT ::
I just ran a few more tests, and it seems that the polygon's collision detection is really random/not reliable. seems to collide when the shape/collision is on certain angles, but not always. Is this a chipmunk bug?

Re: Polygon Problem

Posted: Sat May 29, 2010 11:37 am
by slembcke
I'm guessing that either your polygons are not convex or that the winding is backwards. If you compile Chipmunk in debug mode, it enables a number of assertions that check for common errors like this. If you want to check that the polygon is valid yourself you can call the cpPolyValidate() function. It's not in the docs, but it's in the cpPolyShape.h header.

Re: Polygon Problem

Posted: Sat May 29, 2010 1:46 pm
by MrOz
should cpPolyValidate() return 1 or 0 for a valid polygon?
Thanks in advance,
-Oz

Re: Polygon Problem

Posted: Sat May 29, 2010 1:54 pm
by MrOz
For winding order, the google code page says clockwise, but the chipmunk demos put them counter-clockwise.

Re: Polygon Problem

Posted: Sat May 29, 2010 6:23 pm
by slembcke
Does the y axis in your coordinate system point up or down? I think the Chipmunk docs assumes that you are using the math/OpenGL like coordinate system where the y axis points upwards.

Re: Polygon Problem

Posted: Sun May 30, 2010 10:52 pm
by MrOz
Mine points upwards. Anywho, I figured it out.
Thanks, though, slembcke

Re: Polygon Problem

Posted: Tue Jun 08, 2010 8:10 am
by abbasi
hello i am having a similar kind of problem in using polygons.

Now this chunk of code is working alright
int num = 4;
float temp = 50;
cpVect verts[] = {
cpv(-self.busSprite.texture.contentSize.width/2,-self.busSprite.texture.contentSize.height/2+temp),
cpv(-self.busSprite.texture.contentSize.width/2, self.busSprite.texture.contentSize.height/2),
cpv( self.busSprite.texture.contentSize.width/2, self.busSprite.texture.contentSize.height/2),
cpv( self.busSprite.texture.contentSize.width/2,-self.busSprite.texture.contentSize.height/2+temp),
};

but when i try to create a polygon with 8 vertices(code is listed below) it gives "Polygon is concave or has a reversed winding." error

int num = 8;
float deltaWidth = 50;
float deltaHeight = 50;
cpVect verts[] = {
cpv(-self.busSprite.texture.contentSize.width/2,-self.busSprite.texture.contentSize.height/2+deltaHeight),//1
cpv(-self.busSprite.texture.contentSize.width/2, self.busSprite.texture.contentSize.height/2),//2
cpv(-self.busSprite.texture.contentSize.width/2+deltaWidth, self.busSprite.texture.contentSize.height/2),//3
cpv(-self.busSprite.texture.contentSize.width/2+deltaWidth, self.busSprite.texture.contentSize.height/2-deltaHeight),//4
cpv(self.busSprite.texture.contentSize.width/2-deltaWidth, self.busSprite.texture.contentSize.height/2-deltaHeight),//5
cpv(self.busSprite.texture.contentSize.width/2-deltaWidth, self.busSprite.texture.contentSize.height/2),//6
cpv(self.busSprite.texture.contentSize.width/2, self.busSprite.texture.contentSize.height/2),//7
cpv(self.busSprite.texture.contentSize.width/2, -self.busSprite.texture.contentSize.height/2+deltaHeight),//8
};

can anybody help??

Re: Polygon Problem

Posted: Tue Jun 08, 2010 9:05 am
by slembcke
Well, as the error says, you are either going the wrong way around the polygon's edge. Make sure it's the same direction (clockwise or counterclockwise) as your first polygon, and make sure the vertexes don't have any "dents" in the surface.

One last thing to keep in mind is that colliding two octagons is 4x more expensive than two quadrilaterals. Don't expect the greatest performance if you have 20 of those onscreen at the same time. Not a big deal if there are only a few though.

Re: Polygon Problem

Posted: Tue Jun 08, 2010 9:27 am
by abbasi
I have used the vertices in both clock and counter clock wise but it gives the same error. the body is created alright but the shape gives this error. what do u mean by "dents".

Re: Polygon Problem

Posted: Tue Jun 08, 2010 10:31 am
by slembcke
The polygon must be convex. Concave polygons are much more expensive to do collision detection with and Chipmunk (and most other physics engines as well) doesn't support them. I'm fairly certain that the validation function is correct.