Sign in to follow this  
irreversible

Chipmunk rope simulation

Recommended Posts

irreversible    2862
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[i]  = 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[i], 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this