I copied directly from the Moonbuggy tutorial, so I have a static body with segment shapes for the landscape, a few poly shapes which fall onto the landscape - no joints or anything more complex.
Upon startup, all the objects are attracted to the bottom right wildly, then to the bottom left.. then it continues back and forth less wildly, but doesn't seem to completely stop. It's as if the entire space is a ship which passed over a big wave, and continues to rock back and forth. While this occurs object interactions/collisions are perfect.
I'm trying to integrate this into an existing game, so the code segment below is to demonstrate the steps taken, and is not directly compilable.
Code: Select all
// initialisation
cpInitChipmunk();
cpSpace *space = cpSpaceNew();
setSpace(space);
cpResetShapeIdCounter();
space->gravity = cpv(0.0f, -300.0f);
space->iterations=10;
cpSpaceResizeActiveHash(space, 50.0, 999);
cpSpaceResizeStaticHash(space, 50.0, 999);
//staticBody = cpBodyNew(INFINITY,INFINITY); // no difference
staticBody = cpBodyNew(1e1000,1e1000);
setStaticBody(staticBody);
// constructing landscape
//
cpVect a = cpv( (DynamicPosition().x + Vertices()[i]->Position().x) * 10.0f, (DynamicPosition().y + Vertices()[i]->Position().y) * 10.0f);
cpVect b = cpv( (DynamicPosition().x + Vertices()[inext]->Position().x) * 10.0f, (DynamicPosition().y + Vertices()[inext]->Position().y) * 10.0f);
cpShape *seg = cpSegmentShapeNew(ChipmunkHelper::Instance()->getStaticBody(), a, b, 0.0f);
seg->u = 0.8;
seg->e = 0.5;
cpSpaceAddStaticShape(ChipmunkHelper::Instance()->getSpace(), seg);
// adding poly shapes - convex simple
cpFloat mass = 3.0f; // compute from area? specify override
cpFloat bMoment = cpMomentForPoly(mass,Vertices().size(), myVerts, cpvzero);
myBody = cpBodyNew(mass, bMoment);
myBody->p = cpv(DynamicPosition().x * 10.0f, DynamicPosition().y * 10.0f);
cpSpaceAddBody(ChipmunkHelper::Instance()->getSpace(), myBody);
mBody = myBody;
cpShape *shape;
shape = cpPolyShapeNew(myBody, Vertices().size(), myVerts, cpvzero);
shape->u = 0.5;
shape->e = 0.0;
cpSpaceAddShape(ChipmunkHelper::Instance()->getSpace(), shape);
// runtime loop
cpFloat dt = (1.0f/100.0f) ;
cpSpaceStep(ChipmunkHelper::Instance()->getSpace(), dt);
- "ChipmunkHelper" is a singleton used to ease integration issues - contains the space and static body
- Because I'm integrating chipmunk into this game, I don't have precise control over how regularly cpSpaceStep is called
- Performing multiple iterations per frame with cpSpaceStep didn't seem to help