Jump to content
  • Advertisement
Sign in to follow this  

Chipmunk rope simulation

This topic is 3050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to create an elastic cord - sort of like a rope under tension between two anchor points (eg a trampoline). However the below code doesn't really work - the rope jumps about wildly, the pivot joints start moving around after a short while and the length of the slide joints isn't constrained (I'd say at all, although the min/max limits are set to zero). What am I doing wrong/missing here? I've tried a number of variations of this code (eg different (relative) joint placements and such, but I just can't get it to behave in a stable manner)? ps - I was too lazy to rename 'spring' to 'rope' in the below code :)
//from the chipmunk springies demo
static cpBody *
add_bar(cpVect a, cpVect b, int group, int index)
	cpVect center = cpvmult(cpvadd(a, b), 1.0f/2.0f);
	cpFloat length = cpvlength(cpvsub(b, a));
	cpFloat mass = length/16000.0f;
	cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, mass*length*length/12.0f));
	body->p = center;
	springShape[index] = cpSpaceAddShape(space, cpSegmentShapeNew(body, cpvsub(a, center), cpvsub(b, center), 10.0f));
	springShape[index]->group = group;
	return body;

//the actual code that creates the rope

	cpVect from;
	cpVect to;

	from.x = -5;
	from.y = -2;
	to.x = 5;
	to.y = -2;

        int iNumSpringSegments = 10;

	anchor = cpBodyNew(INFINITY, INFINITY);

	TReal segx = (to.x - from.x) / (TReal)(iNumSpringSegments - 1);
	TReal segy = (to.y - from.y) / (TReal)(iNumSpringSegments - 1);

	for(int i = 0; i < iNumSpringSegments; i++)
		spring  = add_bar(
			cpv(segx * i + from.x,  segy * i + from.y),
			cpv(segx * (i + 1) + from.x,  segy * (i + 1) + from.y), 0, i);

        //attach the rope segments to each other using slide joints
	for(int i =0 ; i < iNumSpringSegments - 1; i++)
		cpSpaceAddConstraint(space, cpSlideJointNew(spring, spring[i + 1], cpv(segx*(i+1),0), cpv(segx*(i+1),0), 0, 0));		

        //attach the ends of the rope to anchors
	cpSpaceAddConstraint(space, cpPivotJointNew(anchor, spring[0], from));
	cpSpaceAddConstraint(space, cpPivotJointNew(anchor, spring[9], to));

I only just started messing around with Chipmunk yesterday and even though the demos are cool and fine and dandy and all that, I'm finding myself a bit confused as to how precisely to use the library (documentation doesn't seem to help much either in this case, although it's probably me rather than the documentation :) ). [Edited by - irreversible on February 17, 2010 6:17:41 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!