Polygon Problem

Official forum for the Chipmunk2D Physics Library.

Polygon Problem

Postby MrOz » Sat May 29, 2010 8:06 am

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?
MrOz
 
Posts: 36
Joined: Tue Mar 02, 2010 11:36 pm

Re: Polygon Problem

Postby slembcke » Sat May 29, 2010 11:37 am

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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Polygon Problem

Postby MrOz » Sat May 29, 2010 1:46 pm

should cpPolyValidate() return 1 or 0 for a valid polygon?
Thanks in advance,
-Oz
MrOz
 
Posts: 36
Joined: Tue Mar 02, 2010 11:36 pm

Re: Polygon Problem

Postby MrOz » Sat May 29, 2010 1:54 pm

For winding order, the google code page says clockwise, but the chipmunk demos put them counter-clockwise.
MrOz
 
Posts: 36
Joined: Tue Mar 02, 2010 11:36 pm

Re: Polygon Problem

Postby slembcke » Sat May 29, 2010 6:23 pm

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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Polygon Problem

Postby MrOz » Sun May 30, 2010 10:52 pm

Mine points upwards. Anywho, I figured it out.
Thanks, though, slembcke
MrOz
 
Posts: 36
Joined: Tue Mar 02, 2010 11:36 pm

Re: Polygon Problem

Postby abbasi » Tue Jun 08, 2010 8:10 am

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??
abbasi
 
Posts: 9
Joined: Thu Apr 16, 2009 7:55 am

Re: Polygon Problem

Postby slembcke » Tue Jun 08, 2010 9:05 am

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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Polygon Problem

Postby abbasi » Tue Jun 08, 2010 9:27 am

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".
abbasi
 
Posts: 9
Joined: Thu Apr 16, 2009 7:55 am

Re: Polygon Problem

Postby slembcke » Tue Jun 08, 2010 10:31 am

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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Next

Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 3 guests

cron