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);
}
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
}
}
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?