Skip to main content
gamedev.net
🔒 Locked

Timed Movement

Started by BloodLust666 14 replies 1.5k views
Original Post
BloodLust666
BloodLust666
I have a timer checking the time that goes by every frame. then i move my sprites according to that time, BUT things are REALLY choppy... like it's not working correctly... here's my timer code: void cGraphics::UpdateTime() { m_lCurTime = timeGetTime(); sm_fTimeElapsed = (m_lCurTime - m_lLastTime) / 1000.0f; m_lLastTime = m_lCurTime; if (sm_fTimeElapsed < 0.001f) sm_fTimeElapsed = 0.001f; m_fFPS = 1.0f / sm_fTimeElapsed; } Then, when i move something, i have a velocity and move it the velocity TIMES the "sm_fTimeElasped". the faster velocity i move a sprite, the choppier it looks...
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
BloodLust666
BloodLust666
on a weird note... everythign is PERFECTLY smooth in fullscreen mode! it's just in windowed mode that things look really choppy... y's that?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Drew_Benton
Drew_Benton
What are your FPS values in Windowed vs FullScreen. What could be happening is that when you are in Windowed mode, you are getting either way faster FPS (not likely) or way slower FPS (most likely) so you are getting 'wild' deltas to move your sprites around. When you go to FS, then the framerate is more 'suitable', and the deltas are 'more logical' (smaller), so the movement is perfect.
BloodLust666
BloodLust666
yep, ur correct. here's my windowed mode output every frame:

Windowed:
FPS: 25.641026
FPS: 83.333336
FPS: 58.823524
FPS: 43.478260
FPS: 41.666668
FPS: 41.666668
FPS: 43.478260
FPS: 43.478260
FPS: 12.987013
FPS: 38.461536
FPS: 200.000000
FPS: 249.999985
FPS: 111.111115
FPS: 41.666668
FPS: 45.454544
FPS: 13.157895
FPS: 41.666668
FPS: 249.999985
FPS: 249.999985
FPS: 90.909088
FPS: 45.454544
FPS: 43.478260
FPS: 43.478260
FPS: 45.454544
FPS: 43.478260
FPS: 43.478260

Full Screen:
FPS: 100.000000
FPS: 100.000000
FPS: 100.000000
FPS: 100.000000
FPS: 111.111115
FPS: 100.000000
FPS: 100.000000
FPS: 100.000000
FPS: 111.111115
FPS: 100.000000
FPS: 100.000000
FPS: 100.000000
FPS: 111.111115
FPS: 100.000000
FPS: 100.000000
FPS: 100.000000
FPS: 111.111115
FPS: 111.111115
FPS: 100.000000
FPS: 100.000000

how can i fix that in windowed mode though?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Staffan E
Staffan E
Does it produce the same result if you remove
if (sm_fTimeElapsed < 0.001f)sm_fTimeElapsed = 0.001f;
?

Why do you cap the frame duration to a minimum value? For one thing the timeGetTime() function has a minimum precision of one millisecond so sm_fTimeElapsed can't be less than one ms. It can be 0.0f, 0.001f, 0.002f... and on.
Hack my projects! Oh Yeah! Use an SVN client to check them out.BlockStacker
Drew_Benton
Drew_Benton
Yea, your timer code seems to be off. After checking what Staaf has said, you could also take a look at this thread and this thread for complete timer systems to compare your code to.
BloodLust666
BloodLust666
i just thought that if time was too small then update it the smallest it can.

about the other threads, i tried using the Query timer but i can't seem to get it to work. and i don't really want to dip into SDL just yet.

On my timer, should i just let it equal 0.0f if no time went by?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Staffan E
Staffan E
Quote:
Original post by Anonymous Poster
Just allowing the delta-time to equal zero is bad. Time will always pass between iterations of calculating the delta-time. So if the delta-time is zero, it just means the precision of the timer cannot detect any change in time; it does not mean that no time passed. Imagine the consequences of pretending no time passes between a large sequence of frames.

I don't agree. I have my Δt at zero on lots of the frames for not-so-heavy scenes and when using timeGetTime().

Imagine a situation where you're rendering ten frames in one ms, using a 1 ms precision timing function like timeGetTime(). Nine of those ten frames would produce a Δt of 0 ms and one of them would produce a Δt of 1 ms. If you allow Δt to be zero the movement of your translatees will be v * 0 ms for the nine frames and v * 1 ms for the one frame, resulting in the final movement v * 1 ms over the ten frames, and since the ten frames occured within 1 ms the movement is correct.

If you don't allow Δt to be zero and cap it to some minimum value, for example 1 ms, then the movement of the translatees will be v * 1 ms for every frame. This would broduce the final movoment of v * 10 ms which is ten times the movement that you intended when setting the velocity v.

EvilKnuckles666: Could it be that you have v-sync enabled in fullscreen mode?
Hack my projects! Oh Yeah! Use an SVN client to check them out.BlockStacker
BloodLust666
BloodLust666
no, i don't have vsync enabled and i don't want it to be. i want to be able to kick out the maximum amount of frames every computer running it can while keeping a good program and looking good.
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
WhatEver
WhatEver
You're problem lies in this code:

if (sm_fTimeElapsed < 0.001f)
sm_fTimeElapsed = 0.001f;

The choppiness happens when you forcefuly round the time elapsed. By doing this you're changing the actual amount of time being passed, hence the choppiness.

You'll need to either forcefuly delay your program for the time you want a frame to take (pause until 0.0001 seconds has passed), or remove that line of code.
WhatEver
WhatEver
Quote:
Original post by EvilKnuckles666
on a weird note... everythign is PERFECTLY smooth in fullscreen mode! it's just in windowed mode that things look really choppy... y's that?


When you're in full screen mode, windows tends to stop processing certain events that would normally be passed to processes, giving your program more time to do its thing :).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.