Need some help with Joints

Official forum for the Chipmunk2D Physics Library.
tatebn
Posts: 41
Joined: Tue May 12, 2009 1:03 pm
Contact:

Re: Need some help with Joints

Post by tatebn »

I changed the offset to be the center of the body. but I'm still having the same issue. here is my updated code for shape creation.

Code: Select all

-(id)initWithParams: (UIImageView *)img space: (cpSpace *)space col: (int) col mass: (float) mass{
	
	if( self = [super init] ){
		
		CGRect frame = img.frame;
		
		CGSize size = frame.size;
		CGFloat x = CGRectGetMidX(frame);
		CGFloat y = CGRectGetMidY(frame);
		
		CGFloat offsetX = size.width/2.0;
		CGFloat offsetY = size.height/2.0;
		
		cpVect verts[] = {
			cpv(x - offsetX, 480 - y - offsetY),
			cpv(x + offsetX, 480 - y - offsetY),
			cpv(x + offsetX, 480 - y + offsetY),
			cpv(x - offsetX, 480 - y + offsetY)
		};
		
		self.linkBody = cpBodyNew(mass, cpMomentForPoly(mass, 4, verts, cpvzero));
		self.linkBody->p = cpv(x, 480 - y);
		self.linkBody->a = 1.0f;
		self.linkBody->w = 5.0f; 
		
		cpSpaceAddBody(space, self.linkBody);
		
		self.linkShape = cpPolyShapeNew(linkBody, 4, verts, cpv(x, y));
		self.linkShape->e = 0.0;
		self.linkShape->u = 0.05;
		self.linkShape->collision_type = col;
		self.linkShape->data = img;
		
		cpSpaceAddShape(space, self.linkShape);
		
	}
	
	return self;
	
}
Thanks,
Brandon
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Need some help with Joints

Post by slembcke »

No, I'm pretty sure what you meant was this:

Code: Select all

      cpVect verts[] = {
         cpv(-offsetX, -offsetY),
         cpv( offsetX, -offsetY),
         cpv( offsetX,  offsetY),
         cpv(-offsetX,  offsetY)
      };
See how it is centered on the origin? The origin == the center of gravity.

Also, if the polys are not colliding, the winding may be backwards. Try reversing the vertexes.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
tatebn
Posts: 41
Joined: Tue May 12, 2009 1:03 pm
Contact:

Re: Need some help with Joints

Post by tatebn »

Ah.... I was under the impression that the vector of verts needed to be the outline of the shape based on the entire coordinate plane. As in point 1 is at (0, 10) which connects to point 2 at (0, 50) which connects to point 3, etc. And that they had to be defined in a clockwise direction. Don't remember where I read that, but is that not the case? The verts are simply offsets from where ever I centered the body that is passed in?

Just want to make sure I understand. I knew it was going to be something simple.

Thanks again for all your help.
tatebn
Posts: 41
Joined: Tue May 12, 2009 1:03 pm
Contact:

Re: Need some help with Joints

Post by tatebn »

That looks much better, but the linking still isn't right. I can't get the pieces to anchor and pull down. It almost looks like gravity is pulling them up.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Need some help with Joints

Post by slembcke »

Are they acting weird/jittery or is gravity just pointed the wrong direction?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
tatebn
Posts: 41
Joined: Tue May 12, 2009 1:03 pm
Contact:

Re: Need some help with Joints

Post by tatebn »

The animation is smooth, but the pieces don't stay joined. Most of them go right, some of them go up. Not sure what I'm doing wrong. Here is the joining code again. This is both link creation (well, this calls the class I posted previously), and joint creation

Code: Select all

//these top links are to anchor to the top, still trying to figure out gravity for those.
	lTop = cpBodyNew(INFINITY, INFINITY);
	lTop->p = cpv(15, 480);
	linkL1 = [[chainLink alloc] initWithParams:L1 space:space col:1 mass:1];
	linkL2 = [[chainLink alloc] initWithParams:L2 space:space col:2 mass:1];
	linkL3 = [[chainLink alloc] initWithParams:L3 space:space col:3 mass:1];
	rTop = cpBodyNew(INFINITY, INFINITY);
	rTop->p = cpv(225, 480);
	linkR1 = [[chainLink alloc] initWithParams:R1 space:space col:4 mass:1];
	linkR2 = [[chainLink alloc] initWithParams:R2 space:space col:5 mass:1];
	linkR3 = [[chainLink alloc] initWithParams:R3 space:space col:6 mass:1];
	linkBling = [[chainLink alloc] initWithParams:bling space:space col:7 mass:10];

	
	// Make joints
	jointL4 = cpPinJointNew(lTop, linkL3.linkBody, lTop->p, linkL3.linkBody->p);
	
	jointL3 = cpPinJointNew(linkL3.linkBody, linkL2.linkBody, linkL3.linkBody->p, linkL2.linkBody->p);
	
	jointL2 = cpPinJointNew(linkL2.linkBody, linkL1.linkBody, linkL2.linkBody->p, linkL1.linkBody->p);
	
	jointR4 = cpPinJointNew(rTop, linkR3.linkBody, rTop->p, linkR3.linkBody->p);
	
	jointR3 = cpPinJointNew(linkR3.linkBody, linkR2.linkBody, linkR3.linkBody->p, linkR2.linkBody->p);
	
	jointR2 = cpPinJointNew(linkR2.linkBody, linkR1.linkBody, linkR2.linkBody->p, linkR1.linkBody->p);
	
	jointL1 = cpPinJointNew(linkL1.linkBody, linkBling.linkBody, linkL1.linkBody->p, linkBling.linkBody->p);
	
	jointR1 = cpPinJointNew(linkR1.linkBody, linkBling.linkBody, linkR1.linkBody->p, linkBling.linkBody->p);
	
	cpSpaceAddJoint(space, jointL4);
	cpSpaceAddJoint(space, jointL3);
	cpSpaceAddJoint(space, jointL2);
	cpSpaceAddJoint(space, jointL1);
	
	cpSpaceAddJoint(space, jointR4);
	cpSpaceAddJoint(space, jointR3);
	cpSpaceAddJoint(space, jointR2);
	cpSpaceAddJoint(space, jointR1);

Thanks
tatebn
Posts: 41
Joined: Tue May 12, 2009 1:03 pm
Contact:

Re: Need some help with Joints

Post by tatebn »

I removed the accellerometer code that changed gravity based on rotation, and now they kind of bounce right, and do get jittery. I was hoping they would just hang as they initially were defined on screen without the gravity change. I don't know why this is so complicated, it seems like a simple concept.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Need some help with Joints

Post by slembcke »

Again, like the vertex coordinates, you are supplying the joint offsets in global coordinates. They are relative to the bodies' centers.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
tatebn
Posts: 41
Joined: Tue May 12, 2009 1:03 pm
Contact:

Re: Need some help with Joints

Post by tatebn »

I have the joints all in place as you suggested, the whole relative to the body thing was my biggest pitfall. Now things still kind of float. It's almost as if the pieces are joined together by rubber bands. And once they bounce up I can't get them to fall correctly again. I would like the 10x heavier piece at the bottom to pull everything else down so that overall object is almost in its original form. But instead it bounces upward and the other pieces kind of circle around each other. Any ideas?

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

Re: Need some help with Joints

Post by slembcke »

Last code sample you posted, you were still doing this:

Code: Select all

   jointR2 = cpPinJointNew(linkR2.linkBody, linkR1.linkBody, linkR2.linkBody->p, linkR1.linkBody->p);
You almost certainly meant:

Code: Select all

   jointR2 = cpPinJointNew(linkR2.linkBody, linkR1.linkBody, cpvzero, cpvzero);
That is placing the joint anchors relative to the body, relative to where the body is in absolute coordinates. Basically a good way to get some scrambled input data. Stretching a large number of joints tightly is going to make them bouncy/stretchy unless you use a lot of solver iterations. Like I've said before, long chains of rigid bodies jointed together is not an easy thing to solve, and it's going to either be inaccurate or CPU heavy. You are also creating zero length pin joints which are probably not going to have as nice of a solution as a pivot.
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: No registered users and 10 guests