I, also, am having problems with this and I don't really understand the overreaching "correct" way to do this stuff.
I'm making an overhead space shooter type game. The player holds down a "boost" key to cause the rocket to boost forward, which is basically just applying forces regularly. This is essentially my main loop:
Code: Select all
time_gameclock = time_physics_prev = time_physics_curr = worldTimer.getMilliseconds();
while (!done) {
mTWVideo->renderOneFrame();
long long dt = worldTimer.getMilliseconds() - time_gameclock;
while(dt >= TickMs) {
dt -= TickMs;
time_gameclock += TickMs;
// ADD FORCES HERE
mPlayer->boost(mTWInputstate->mBoost);
}
/* Physics handling part of the loop
This, like the rendering, ticks every time around.*/
// Note that at super high framerates [850fps], the number being passed to this function is regularly "1" or "2"
// Whereas at lower framerates [60fps], the number being passed is "16" or "17"
time_physics_curr = worldTimer.getMilliseconds();
mWorld->stepSimulation((float)(time_physics_curr - time_physics_prev));
time_physics_prev = time_physics_curr;
}
I added a comment "ADD FORCES HERE" where I'm actually adding forces to the spaceship. So far, this whole thing seems reasonable to me. The problem is that my framerate can be as low as 60fps [with vsync enabled], or it runs at about 850fps without vsync. The difference in simulation between 60fps and 850fps is
drastic.
I've tried experimenting with the last two params to mWorld->stepSimulation(), but they don't appear to fix what's wrong here. [I've tried 0,1,500... for the numSubSteps, and for the step length, I've tried numbers from 1/850th of a second [for my top framerate] up to 1/32 of a second [to match my input loop]].
I've now tried previous suggestion in this thread, and my boost function calls spaceship->setGravity instead of spaceship->addCentralForce, but it still seems heavily dependant on framerate.
Strangely, I'm getting another effect where the spaceship doesn't strictly accellerate at the speed of gravity; there's a point, based on a bunch of factors I haven't been able to figure out [one of which is the framerate], where the accelleration of the spaceship suddenly goes from really slow [pixels per second, on-screen] to really high [multiple screenwidths every tick].
I'm sorry, I'm probably being really stupid, but I just can't see what's going on here.
I *used* to divide the number passed stepSimulation by a thousand, but then I always seemed to get weird twitching in the bodies. It seems like the number should be divided by a thousand [since I'm passing milliseconds now], but the twitching of the bodies was what I was trying to fix.
Thanks,
Gary (-;