Problem with static poly

Official forum for the Chipmunk2D Physics Library.
Post Reply
Brain21
Posts: 3
Joined: Tue Dec 23, 2008 9:30 am
Contact:

Problem with static poly

Post by Brain21 »

Hi, I'm trying out Chipmunk and I have a problem. What I've done so far is to create a ground (static segment) and a ball. Then I added a rectangle (poly) and hope to make it static. It is static in the scene, but the ball goes right through it. I have the ground at the bottom of the screen and the ball at the top and the rectangle between the two and I make gravity down 10.0. The ball falls and stops at the ground, but goes through the rectangle. When I change the rectagle poly to a segment, it all works fine. Here's the code I'm using for the rectangle:

Code: Select all

void CreateRect( float x1, float y1, float x2, float y2 )
{
	int num = 4;
	float px, py, dx, dy;

	cpBody *bod;
	cpShape *shp;

	px = ( x1 + x2 ) / 2.0;
	py = ( y1 + y2 ) / 2.0;
	dx = ( x2 - x1 ) / 2.0f;
	dy = ( y2 - y1 ) / 2.0f;

	cpVect verts[] = {
		cpv(-dx,-dy),
		cpv(dx, -dy),
		cpv( dx, dy),
		cpv( -dx, dy),
	};

	bod = cpBodyNew(INFINITY, INFINITY);
	cpVect offset = cpv( px, py );
	shp = cpSegmentShapeNew(bod, cpv(x1, y1), cpv( x2, y1), 0 );  //This works
//	shp = cpPolyShapeNew(bod, num, verts, offset );                      //This doesn't
	shp->e = 1.0; shp->u = 1.0;
	cpSpaceAddStaticShape(space, shp);	
}
So the line that's commented out is the one that does not work (ball passes through).
Any ideas?

Denis.
sickman
Posts: 19
Joined: Mon Oct 06, 2008 7:50 am
Contact:

Re: Problem with static poly

Post by sickman »

I had the same problem.

The points of polygon must be given in counter-clockwise order.

Instead of this

Code: Select all

cpVect verts[] = {
      cpv(-dx,-dy),
      cpv(dx, -dy),
      cpv( dx, dy),
      cpv( -dx, dy)
   };
It should go

Code: Select all

cpVect verts[] = {
    cpv(-dx, -dy ),
    cpv(-dx, dy ),
    cpv(dx, dy ),
    cpv(dx, -dy)
   };
Brain21
Posts: 3
Joined: Tue Dec 23, 2008 9:30 am
Contact:

Re: Problem with static poly

Post by Brain21 »

Thanks! I had missed that part in the documents. It works now.

Brain21
sickman
Posts: 19
Joined: Mon Oct 06, 2008 7:50 am
Contact:

Re: Problem with static poly

Post by sickman »

That thing should be highlighted in red and underlined in manual because everybody seems to stumble on it, unless it hasn't already.
Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests