Hi all,
Don't remember where, but somewhere in the old discussion group, I found this code to use as a Chipmunk stepper:
int steps = 2, i;
cpSpace *space = SPACE(rb_gv_get("space"));
cpFloat dt = delta/(cpFloat)steps;
for (i=0; i < steps; i++)
cpSpaceStep(space, dt);
cpSpaceHashEach(space->activeShapes, &eachShape, nil);
cpSpaceHashEach(space->staticShapes, &eachShape, nil);
The thing is that my simulation was running really jerky. So searching in the chipmunk forums, I found that you should supply a constant dt to cpSpaceStep in order to get a better simulation, so reading a post[1] referenced in the thread[2], I came up with the following code:
- (void)chipmunk_step:(ccTime)delta {
cpSpace *space = SPACE(rb_gv_get("space"));
cp_accumulator += delta;
while (cp_accumulator >= cp_dt) {
cpSpaceStep(space, cp_dt);
cp_accumulator -= cp_dt;
}
cpSpaceHashEach(space->activeShapes, &eachShape, nil);
cpSpaceHashEach(space->staticShapes, &eachShape, nil);
}
It's giving me a lot better results. Of course, you have to schedule chipmunk_step on each tick:
[myObj schedule:@selector(chipmunk_step:)];
What do you guys use as a stepper?
Oh, I forgot, cp_accumulator and cp_dt are defined as globals:
cpFloat cp_dt = 0.01f;
cpFloat cp_accumulator = 0.0f;
[1]: http://gafferongames.com/game-physics/fix-your-timestep/
[2]: http://www.slembcke.net/forums/viewtopic.php?f=1&t=356&p=1647&hilit=jerk#p1647
regards,
rolando./