Polygon Help - Offsets and Centers

Official forum for the Chipmunk2D Physics Library.
Post Reply
blogey85
Posts: 2
Joined: Mon Aug 03, 2009 2:43 pm
Contact:

Polygon Help - Offsets and Centers

Post by blogey85 »

Hi, I'm looking to attach a polygon to a circle (an arm to a body) at a single point and have the arm flop around when the body has forces applied to it. I think that a pin-joint would be best (correct me if I'm wrong), but I'm having significant troubles with the OFFSET params to a few of the chipmunk functions.

Can anyone explain in a little more detail where the centers of shapes are (are they at x/2, y/2 ?), what the offsets for cpMomentForPoly and cpPolyShapeNew actually do (do they move the center of the shape?), and anything else you think I might be missing?

Here's an image showing what I want done (pivoting at the obvious point), and the relevant code snippet that I'm trying.

Image

Code: Select all

	////// LEFT ARM //////
	cpVect laVerts[]={cpv(0,0),cpv(20,0),cpv(25,-5),cpv(20,-10),cpv(0,-10)};   // Clockwise
	moment = cpMomentForPoly(armMass, 5, laVerts, cpvzero);    
	// (Not sure what the offset does here, but I think this means MOI in center)

	golferLArmBody = cpBodyNew(armMass, moment);
	golferLArmBody->p = cpv(bodyX-bodyRadius,bodyY);
	cpSpaceAddBody(space, golferLArmBody);

	// I think the center of the shape is at (12.5, -5)?  Maybe?
	// The OFFSET moves the center to (25, -5), which is the point I want the joint at?
	shape = cpPolyShapeNew(golferLArmBody, 5, laVerts, cpv(12.5f,0);  
	cpSpaceAddShape(space, shape);
	
	////// LEFT SHOULDER //////
	leftShoulderJoint = cpPinJointNew(golferBody, golferLArmBody, cpv(-bodyRadius,0), cpvzero;
	// The point on the arm to connect to the body should be at (0,0) since I moved the offset to
	//    the point on want when creating the shape?  Is this wrong?

	cpSpaceAddJoint(space, leftShoulderJoint);
Any idea what might not be right about this? Any help is greatly appreciated! :)

Thanks!
blogey85
Posts: 2
Joined: Mon Aug 03, 2009 2:43 pm
Contact:

Re: Polygon Help - Offsets and Centers

Post by blogey85 »

Actually, in re-reading my post, I think I may have found something?

In this line:

Code: Select all

golferLArmBody->p = cpv(bodyX-bodyRadius,bodyY);
What point am I putting at (bodyX-bodyRadius, bodyY)? The center? The first point in laVects? The top-left point? This again goes back to my original question I suppose - where are the centers of polygons, and what does the offset param do??

Thanks again!
Jim
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Polygon Help - Offsets and Centers

Post by slembcke »

The polygon vertexes are relative to the center of gravity of the body you are attaching it to. The offset is just added to each vertex.
That means that the center of the polygon is at -offset basically. If you want to find the centroid of the polygon you'll have to calculate that yourself or guess it (you really don't have to be that close for it to look right) and set the offset accordingly.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 10 guests