make a dynamic polygone in C

Official forum for the Chipmunk2D Physics Library.
Post Reply
yeassay
Posts: 2
Joined: Sun Aug 17, 2008 2:57 pm
Contact:

make a dynamic polygone in C

Post by yeassay »

Hi,
I'm writing a little game just for testing chipmunk and I have a little problem, i want to create a function who add a polygon, but my polygon don't bounce against walls, they just fall:

Code: Select all

void make_dynamic_polygon(pt *tabPT, int nbVect)
{
	int i;
	float minx = 10000.0, miny = 10000.0, maxx = 0.0, maxy = 0.0;
	cpVect *verts = (cpVect *) malloc(nbVect*sizeof(cpVect));
	
	for(i=0;i<nbVect;i++) {
		if(tabPT[i].x < minx && tabPT[i].y < miny) {
			minx = tabPT[i].x;
			miny = tabPT[i].y;
		}
		if(tabPT[i].x > maxx && tabPT[i].y > maxy) {
			maxx = tabPT[i].x;
			maxy = tabPT[i].y;
		}
	}
	
	for(i=0;i<nbVect;i++) {
		verts[i].x = tabPT[i].x - minx-((maxx-minx)/2);
		verts[i].y = tabPT[i].y - miny-((maxy-miny)/2);
		printf("x: %.2f, y: %.2f\n",verts[i].x,verts[i].y);
	}
	double heaviness = (maxx-minx) * (maxy-miny) * 0.001;

	cpBody *body = cpBodyNew(heaviness, cpMomentForPoly(heaviness, nbVect, verts, cpvzero));
	body->p = cpv(minx+((maxx-minx)/2.0), miny+((maxy-miny)/2.0));
	cpSpaceAddBody(space, body);
	
	cpShape *shape = cpPolyShapeNew(body, nbVect, verts, cpvzero);
	shape->e = 0.0; shape->u = 0.6;
	cpSpaceAddShape(space, shape);
}
"pt *tabPT" is an array of coordinates
and "int nbVect" the array size.

anyone see where i'm wrong?
maximile
Posts: 157
Joined: Mon Aug 20, 2007 12:53 pm
Location: London, UK
Contact:

Re: make a dynamic polygone in C

Post by maximile »

No, but are your polygons wound correctly? Have you tried reversing the order of the vertices?
yeassay
Posts: 2
Joined: Sun Aug 17, 2008 2:57 pm
Contact:

Re: make a dynamic polygone in C

Post by yeassay »

I have found the problem.... The coordinate for creating a polygon have to be order like this:

1 . . 4
+ - +
| . . |
+ - +
2 . . 3

I have no idea why but i have to make a function for restore order of my polygon...
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests