Oscillating Gravity?

Discuss any Chipmunk bugs here.
Post Reply
fictionpuss
Posts: 5
Joined: Tue Oct 02, 2007 9:22 pm
Contact:

Oscillating Gravity?

Post by fictionpuss »

This is a bit of a long shot, but I'm putting it out there in case anyone else has run into something similar.

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
Any pointers much appreciated!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Oscillating Gravity?

Post by slembcke »

I would guess that you are moving the objects into position by setting the offset on the shape instead of moving the position of the rigid body. Doing so would cause the center of gravity to swing back and forth far below where your shapes attach and collide with the terrain.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
fictionpuss
Posts: 5
Joined: Tue Oct 02, 2007 9:22 pm
Contact:

Re: Oscillating Gravity?

Post by fictionpuss »

Hi - thanks for the quick response, took me a while to verify + fix, but you nailed it.. all my bodies had one shape with vertices's offset like this:

Code: Select all

  cpVect obj_verts[] = {
    cpv(-210,-490),
    cpv(-190,-490),
    cpv(-190,-510),
    cpv(-210,-510),
  };
Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests