Help with Joints

Official forum for the Chipmunk2D Physics Library.
Post Reply
seancarmody
Posts: 3
Joined: Tue Jun 30, 2009 7:30 pm
Contact:

Help with Joints

Post by seancarmody »

I am trying to make it appear as though a sign is hanging on a rope. I can put the sign in the space, but I cannot seem to add the rope, which would be a Joint.

It seems like it is all good until the "cpSlideJointNew" declaration. Then I am getting an incompatible pointer type error. It seems like I have it right, but obviously I do not.

Can someone help and take a look at the code below?

BTW I am using Chipmunk with Cocos2d on the iPhone 3.0

Thanks

-(void)setupChipmunk{

cpInitChipmunk();
space = cpSpaceNew();
cpSpaceResizeStaticHash(space, 12, 10);
cpSpaceResizeActiveHash(space, 12, 10);
space->gravity = cpv(0,-1000); //initial gravity (not necessary)
space->elasticIterations = 1;
[self schedule: @selector(step:)];

//for logo
cpVect verts2[] = { cpv(70, 200), cpv(455, 200), cpv(455, 310), cpv(70, 310) };
cpBody *logoBody = cpBodyNew(INFINITY, INFINITY);
logoBody->p = cpv(250,200);
cpSpaceAddBody(space, logoBody);
cpShape *logoShape = cpPolyShapeNew(logoBody, 4, verts2, cpvzero);
logoShape->data = logo;
logoShape->collision_type = 1;
cpSpaceAddShape(space, logoShape);

//for top edge to attach rope to
cpVect verts1[] = { cpv(0, 320), cpv(480, 320), cpv(480, 330), cpv(0, 330) };
cpBody *roofBody = cpBodyNew(INFINITY, INFINITY);
roofBody->p = cpv(240, 320);
cpShape *roofShape = cpPolyShapeNew(roofBody, 4, verts1, cpvzero);
roofShape->e = 0.5;
roofShape->u = 0.1;
roofShape->collision_type = 0;
cpSpaceAddStaticShape(space, roofShape);

// add rope
cpSlideJoint *cpSlideJointAlloc(void);
cpSlideJoint *string1 = cpSlideJointNew(roofBody, logoBody, cpv(80, 250), cpv(100, 320), 20, 22);
cpSpaceAddJoint(space, string1);

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

Re: Help with Joints

Post by slembcke »

This:

Code: Select all

cpSlideJoint *cpSlideJointAlloc(void);
cpSlideJoint *string1 = cpSlideJointNew(roofBody, logoBody, cpv(80, 250), cpv(100, 320), 20, 22);
cpSpaceAddJoint(space, string1);
Should simply be this:

Code: Select all

cpJoint *string1 = cpSlideJointNew(roofBody, logoBody, cpv(80, 250), cpv(100, 320), 20, 22);
cpSpaceAddJoint(space, string1);
Read the section in the manual about how Chipmunk memory management works here: http://code.google.com/p/chipmunk-physi ... umentation
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
seancarmody
Posts: 3
Joined: Tue Jun 30, 2009 7:30 pm
Contact:

Re: Help with Joints

Post by seancarmody »

Thank you so much for the help.

One follow up question. What would be the best way to create a shape that follows the joint in size and position? In other words, the visual representation of a chain or a rope? It does not look like cpJoint supports a data reference.

Thanks again for the help, it is much appreciated.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Help with Joints

Post by slembcke »

I think it was added recently to trunk, but the older version did not as there was no place where you would be passed a joint in a callback without any context.

You don't really need to draw the joint, what you want is to draw something for each of the bodies attached to the joint.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
seancarmody
Posts: 3
Joined: Tue Jun 30, 2009 7:30 pm
Contact:

Re: Help with Joints

Post by seancarmody »

I have something for each of the bodies attached to the joint. But I do need something for the Joint, otherwise the joint appears to operate as magic.

If you imagine a swing tied to a tree...the bodies are the swing seat and the tree branch. But the ropes that move at each point of contact are the Joints. Without them the scene looks odd.

I think the answer may be to create additional bodies for the ropes and join them at each end with another joint. But the problem there is that the rope is then frozen or stiff - not very natural. I thought if I can have an image follow the SlideJoint it would look more realsitic. SlideJoints operate like ropes or chains by themselves.

Let me know if you have any thoughts on this, and thanks again for your time.
dieterweb
Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany
Contact:

Re: Help with Joints

Post by dieterweb »

If performance is important for your application, try using a springjoint for the rope and simulate the drawing of the rope with valet integration. Take a look at this thread:
http://www.slembcke.net/forums/viewtopic.php?f=1&t=62

Thomas
Visit our game Blog: [url]http://zombiesmash.gamedrs.com[/url] or follow us on twitter: [url]http://twitter.com/zombiesmash[/url]
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests