Changed cpSpaceStep step time leads to explosive collisions

Official forum for the Chipmunk2D Physics Library.

Changed cpSpaceStep step time leads to explosive collisions

Postby Amral » Sat Sep 26, 2015 2:15 pm

Hello,

I recently changed the way I call cpSpaceStep (or cpHastySpaceStep, I've tested both) in my game update code, and since then I've been getting vey unusual, "explosive" collisions - about half the time, colliding objects will rebound with extremely high velocity, even though they collided with low velocity.

My old code, which works without problems, divided the update interval "dt" into 3, and called cpHastySpaceStep three times. In most cases, dt is approximately 1/60 second, so the chipmunk space was being stepped at approximately 1/180 seconds intervals.

Code: Select all
void updateChipmunkSpace(float dt) {
   float currentDt = dt / 3.0;
   cpHastySpaceStep(chipmunkSpace, currentDt);
   cpHastySpaceStep(chipmunkSpace, currentDt);
   currentDt = dt - (2.0*currentDt);
   cpHastySpaceStep(chipmunkSpace, currentDt);
}


I recently modified it so that cpHastySpaceStep is being called with a fixed time step (apart from one final call per update loop to consume the left-over time. I tried accumulating it instead for the next loop, going to a proper fixed time-step, but unfortunately I get noticable 'juddering' effects).

The new code is :

Code: Select all
#define CP_STEP_TIME (0.00555555)    // Approximately 1/180th second

void updateChipmunkSpace(float dt) {
    float dtToProcess = dt;
    while (dtToProcess >= CP_STEP_TIME) {
        cpHastySpaceStep(chipmunkSpace, CP_STEP_TIME);
        dtToProcess -= CP_STEP_TIME;
    }
    if (dtToProcess > 0) {
        cpHastySpaceStep(chipmunkSpace, dtToProcess); // PROBLEM HERE
    }
}


For some reason, the new code results in the very unphysical, 'explosive' collisions. If I remove the final call to cpHastySpaceStep (the one with the comment "PROBLEM HERE"), the collisions are normal, although of course the simulation becomes jerky as differing fractions of time are lost on each update loop.

I thought the problem might be that cpHastySpaceStep is sometimes being called with a very small step value, but I've tried pre-dividing dt by 10 or 20, causing small step values to be passed to cpHastySpaceStep, effectively "slowing down time" in the simulation , but everything works fine, so it's not that.

Does anyone have any experience of this, or any ideas what might be causing it?
Amral
 
Posts: 5
Joined: Fri Oct 24, 2014 11:11 am

Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 1 guest

cron