Skip to main content
GameDev.net gamedev.net
🔒 Locked

Chipmunk Physics - changing gravity

Started by Rhaal Sep 29, 2009 at 11:18 AM 0 replies 3.1k views
Original Post
Rhaal
Rhaal
I'd love to ask this on the official Chipmunk physics forums, but I can't get a verification email for my forum registration over there :( Does anyone have experience with the Chipmunk 2D physics engine? I am trying to create a level editor for my game that uses Chipmunk, but I can't for the life of me figure out how to "turn off" gravity and restart it. My goal is: - The editor starts with no gravity. Levels are edited this way. - "Play" is pressed, and gravity kicks in. Level is tested this way. - "Stop" button is pressed, and gravity goes back to zero and the level is reloaded, for editing again. The problem is every time I press play, the gravity seems to increase as shapes fall incredibly fast. Here is how I start gravity:
// Yes, cpInitChipmunk() is called before any of this.
void State_Playing::StartGravity()
{
  g_pSpace = cpSpaceNew();
  g_pSpace->gravity = cpv(0, 0);
  g_pSpace->elasticIterations = 5;

  SetTimer(NULL, 1, 1.0f / 60.0f, TimerProc);
}


Here's how I remove shapes and stop gravity:
void State_Playing::StopGravity()
{
  // all Block's deconstructors called here...

  cpSpaceDestroy(g_pSpaceNew);
  g_pSpace = cpSpaceNew();
  g_pSpace->gravity = cpv(0, 0);
  g_pSpace->elasticIterations = 5;
}

// Block deconstructor
Block::~Block()
{
  // Free shape
  if(fixed)
    cpSpaceRemoveStaticShape(space, shape);
  else
    cpSpaceRemoveShape(space, shape);

  // Free body
  cpSpaceRemoveBody(space, body);

  // Free sprite
  delete sprite;
}


It all appears to work, but every time I call the stop function, and then the start function everything falls faster. I can't narrow it down to gravity increasing or mass increasing (though I'm removing all the shapes!). Any clue? Is there some kind of cpShutdownChipmunk function? My only other option is to have two separate apps, a level editor and a level tester - which would be much less productive.
- A momentary maniac with casual delusions.
Rhaal
Rhaal
Typical.. I post and then solve it :)

For anyone else with the problem, calling cpSpaceFree instead of cpSpaceDestroy fixes it. (Though it seems like this should cause a crash/memory leak, not the problem I had.)
- A momentary maniac with casual delusions.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.