Need some help with Joints

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

Need some help with Joints

Post by tatebn »

I'm trying to have a chain of links, anchored at the top of the screen on the left and right side that would swing like a chain. I have each link defined as it's own body and space and if I don't use joints all the links move together as one. Which I would expect them to because they all have the same gravity.

I tried adding Pin Joints to connect each link to the link above it. The top links are connected to bodies I created to represent the top left and top right. The top bodies are not added to the space and should not be effected by gravity. The bottom center link is connected to the center of the left link above it and the right link above it.

Everything seems to be joined to something, but I don't know what. The pieces move frantically in all sorts of directions. Kind of swirling around a center point. Do I need to add a bounding box to the top? I just need some direction here.
tatebn
Posts: 41
Joined: Tue May 12, 2009 1:03 pm
Contact:

Re: Need some help with Joints

Post by tatebn »

I guess my biggest question here is, how do I have all the joined pieces weighted down to a center point at the bottom. Then the center point would kind of pull the other pieces when swinging. How does the mass work? Would that be my answer? Make the bottom piece much heavier than the rest?
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 »

tatebn wrote:I have each link defined as it's own body and space and if I don't use joints all the links move together as one.
You don't really make a space for every body do you?

Chains can be tricky because adding a lot of joints to a lot of bodies can be disastrous for performance and stability if you do it wrong. Some general tips:
  • More iterations means more stability and worse performance.
  • Fewer bodies/joints means more stability and performance.
  • Make a shorter chain first to make sure you have the joint parameters correct.
  • Applying large forces or attaching large masses to the chain will make stability worse.
Having realistic mass ratios (very light chain links compared to the mass hung from it) is cool and all, but disastrous for performance. Unless you use a lot of iterations, your chain will look stretchy and jittery. Watch what the big guys do in AAA games by keeping the mass ratios small. Big enough that you can see the effect, small enough that you don't have to use a lot of CPU to do it.
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 »

Sorry for being confusing. No I'm not giving each body it's own space. There is one space that is passed to each body init. I meant shape. Each link has it's own body and shape.

My chain is small right now. 3 links on each side with one central weight. I don't expect it to get bigger than 6 links on each side in the long run so performance shouldn't be a huge deal. As far as the mass ratios go, it's been a very long time since I've done any physics (very grateful for chipmunk by the way) and I'm not sure what a small mass ratio would be. As far as chipmunk is concerned what is the unit of mass when creating the bodies?

Also, a side question. As far as collision goes, does each shape have to have a different collision_type for them to bump into each other? I have them all as the same one right now and they slide through each other. Sorry for the simple questions, but I'm having trouble finding some good tutorials for this stuff, especially in objective-c, and I've kind of been trying to give myself a crash course and learn as I put something together.

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 »

By mass ratio, I mean hanging a weight that is 100x heavier than one of the chain links on a chain of 3 bodies is going to be problematic. Hanging the same on a chain of 6 will be worse. Having the weight be 10x heavier would be much better.
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 »

Maybe you can help me out here. I'm having weird gravity and joint issues. Here is the class where my bodies and shapes are declared.

Code: Select all

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, CGPointZero));
		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, cpvzero);
		self.linkShape->e = 0.0;
		self.linkShape->u = 0.05;
		self.linkShape->collision_type = col;
		self.linkShape->data = img;
		
		cpSpaceAddShape(space, self.linkShape);
And here are my initializations and joints.

Code: Select all

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);
What happens when this starts is all the pieces are sucked up and right, off of the screen, then they kind of orbit some off point center screen. What I would like is links in the chain to be connected to the links above it and weighted by the bottom piece.

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

Re: Need some help with Joints

Post by tatebn »

Also,

The pieces are sliding right through each other. How do I get them to recognize each other and collide?
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 »

Ok I see 3 potential and very nasty problems. First of all don't pass CGPointZero as a cpVect unless you are sure that the types are compatible. Use cpvzero. Second, polygon vertexes are relative to the center of gravity. It looks like you are defining the center of gravity to be far outside of the shape itself. Similar for the pin joints. The anchor vectors are in body relative coordinates. I think you meant to pass cpvzero for all of the joint anchor offsets.
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 »

How am I defining the center of gravity to be outside of the shape? Not sure what part of the code is actually defining the center of gravity. Also, any thoughts of lack of collisions. How does the collision system work?

I really appreciate the help.

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 »

Unless you specify an offset when creating the shape, the poly verts are attached to the body at (0, 0). This means that you want your box to be centered on (0,0). As far as collisions, you are setting a collision type. Are you also using a collision callback? If that callback is returning false, then it's telling Chipmunk to discard and ignore that collision.
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 23 guests