• Advertisement
Sign in to follow this  

Time based movement screw-ups

This topic is 4428 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there :) I've got this little problem. Been having it for a few months, but I always thought that I'll be able to fix it later :). Well, since I can't manage to do that I'm posting here. You see, I'm using time based movement in my game, I multiply the amount I want an object to move a second by the delta time between frames. This works fine when an object has to move when, for example, a key is pressed, but it fails horribly when it has to move constantly, every frame. Here's how I harvest the delta time: main loop { curtime= timeGetTime(); deltatime = (curtime - prevtime)/1000.0f; update stuff prevtime = curtime; } Now when doing if (key is pressed) { xpos += number*deltatime; } it's fine, but when I simply do xpos+=number*deltatime; The object dissapears. And I'm pretty sure it's something other than going offscreen too quickly as I've taken measures against it and tested extremely small values too. Any help is greatly appreciated :)

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Now when doing
if (key is pressed) { xpos += number*deltatime; }
it's fine, but when I simply do
xpos+=number*deltatime;
The object dissapears.
And I'm pretty sure it's something other than going offscreen too quickly as I've taken measures against it and tested extremely small values too.


Ok you're on the right track, but you are overlooking one little detail. When you have an event happen on a keypress, chances are it will happen so many times per second, depending on how your input is. Let's say though, for example, your input is updated once per second, which your game runs at 30 frames per second. That means when you press a key to move, the guy will move over a total of number*deltatime units. However, think about what happens if you just called it without the condition of the limiting input. It would then be called 30*number*deltatime times in that once second!

See the difference? You're best bet is to either manually apply another factor to slow down the movement, to make it smaller (multiply by let's say .25 to start out with and see if that helps, although you might need to get into the .0X range), or you could make number smaller to begin with, since it's moving at a time based rate, or finally, you can make timer logic so you can have:

if(timer >= 250ms)
xpos+=number*deltatime;

Or something along that line. Good luck!

Share this post


Link to post
Share on other sites
You said you've taken measures against it going offscreen too quickly. Specifically, did you set the deltatime to 0 for the first frame, when prevtime would be zero? If not, then the deltatime would be equal to the current time and it would be an enourmous number. That would be the only thing I can think of since I use the same method and I had a little securty drone patrol back and forth my screen and it worked fine. Hope that helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
<large post minimized :) >

The thing is that when the key is pressed it does get updated every frame, just like it would be if it moved constantly without keypresses.
And theres a way to make the object not go offscreen, divide deltatime by 10000 :), any less and it still dissapears, but when divided by 10000 the object doesn't move at all.

Quote:
Original post by K_A_Z_E
You said you've taken measures against it going offscreen too quickly. Specifically, did you set the deltatime to 0 for the first frame, when prevtime would be zero? If not, then the deltatime would be equal to the current time and it would be an enourmous number. That would be the only thing I can think of since I use the same method and I had a little securty drone patrol back and forth my screen and it worked fine. Hope that helps.

Now that looked like the solution I was looking for! But once I tried it, it surprisingly didn't help. I tried setting them all even to 0 and 1, still the same thing.

I've just tried out a few thing. If I set it to go back to the middle of the screen when it goes offscreen it flashes very fast, so the problem seems to be in deltatime being too big, but it still keeps happening once the first frame has long passed.
/me confused :S

EDIT: No, if I get it to teleport back in the center once it goes offscreen it starts working ok! So this has something to do with the first frame then..

Share this post


Link to post
Share on other sites
Quote:
Original post by Fred304
Have you tried setting prevtime = timeGetTime(); before the loop?

That worked just fine! Thanks! =D
So K A Z E was right then and I was doing it the wrong way.
Rate++ to all three of you for being helpful :)

Share this post


Link to post
Share on other sites
Mr Benton proposed a timer/stopwatch type method which is probably what you want. You can also do something like this, I believe. It depends on how you want to regulate speed...


int main(){
float lastCalled = getCurrentTime();
float speed = .1; //units per millisecond (assuming getCurrentTime returns milliseconds)
//that means we move 6 units in one second

//MAIN LOOP
while(!done){
...
lastCalled = updatePos(itemToMove, speed, lastCalled);
...
}
}

//update function
float updatePos(object itemToMove, float speed, float lastCalled){
float delta = getCurrentTime() - lastCalled;
float distance = delta * speed;
itemToMove.x+=distance;
return getCurrentTime();
}







*edit: oh poo. Sorry for the late attempt. Glad you got it working!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement