Rotate around a point with a spring

Official forum for the Chipmunk2D Physics Library.
Post Reply
the source
Posts: 6
Joined: Sun Sep 12, 2010 3:49 pm
Contact:

Rotate around a point with a spring

Post by the source »

Hi folks,

I am looking for a little advice on how to get an image to move with a spring action around a point (coords inside the sprite) to simulate a swinging effect and show movement with the accelerometer.

This code here doesn't do what I expect. Can someone enlighten me on what is wrong? What are good values for stiffness and spring constants?

TIA.

anObject = [CCSprite spriteWithFile:@"anObject.png"];
anObject = ccp(511 + 502/2, 8 + 414/2);
[self addChild:anObject z: 5 tag:5];

int numVerts = 4;
CGPoint objectVerts[] = {
cpv(50.0f, 50.0f),
cpv(-50.0f, 50.0f),
cpv(-50.0f, -50.0f),
cpv(50.0f, -50.0f)
};

objectBody = cpBodyNew(1.0f, cpMomentForPoly(1.0f, numVerts, objectVerts, CGPointZero));
objectBody->p = anObject.position;
objectBody->data = anObject;
cpSpaceAddBody(space, objectBody);

anObjectShape = cpCircleShapeNew(objectBody, 100.0, CGPointZero);
anObjectShape->data = anObject;
cpSpaceAddShape(space, anObjectShape);

// place pivot point at the center of anObject
pivotBody = cpBodyNew(INFINITY, INFINITY);
CGPoint aPoint = anObject.position;
pivotBody->p = ccp(aPoint.x, aPoint.y);

pivotShape = cpCircleShapeNew(pivotBody, 1.0f, cpvzero);
pivotShape->e = 0.9f; // dummy value
pivotShape->u = 0.9f; // dummy value
cpSpaceAddStaticShape(space, pivotShape);

// join both at their center points
joint = cpPivotJointNew(bearHeadBody, pivotBody, aPoint);

cpSpaceAddConstraint(space, joint);

// create another point that is the center of anObject + 10 for each of the axes
springPointBody = cpBodyNew(INFINITY, INFINITY);
springPointBody->p = ccp(aPoint.x + 10, aPoint.y);

springPointShape = cpCircleShapeNew(springPointBody, 1.0f, cpvzero);
springPointShape->e = 0.9f;
springPointShape->u = 0.9f;
cpSpaceAddStaticShape(space, springPointShape);

cpVect objectSpringAttachPoint = cpv(aPoint.x+10, aPoint.y+10);
cpFloat restLength = 10.0f;
cpFloat stiffness = 5.0f; // no idea what this should be
cpFloat damping = 0.999f;

spring = cpDampedSpringNew(springPointBody, objectBody, springPointBody->p, objectSpringAttachPoint, restLength, stiffness, damping );
cpSpaceAddConstraint(space, spring);
jason_rogers
Posts: 81
Joined: Fri Aug 27, 2010 1:00 am
Contact:

Re: Rotate around a point with a spring

Post by jason_rogers »

Hi
not a big expert in chipmunk
however I don't really see anything fundamentally wrong in your code.
Can you be a bit more specific on the effect your looking to do and what is this not expected behavior your having
the source
Posts: 6
Joined: Sun Sep 12, 2010 3:49 pm
Contact:

Re: Rotate around a point with a spring

Post by the source »

Hi, I worked out - I was defining a shape for the pivot point. This is what was making it go crazy. Removing the shape and it worked fine.
the source
Posts: 6
Joined: Sun Sep 12, 2010 3:49 pm
Contact:

Re: Rotate around a point with a spring

Post by the source »

and the shape for the spring ... not necessary.
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests