# Frame rate independent movement one more time

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

## Recommended Posts

I understand that is frequent question, but... I have physics simulation system based on time step, but my object is shaking during simulation, i try different approaches to fix time step but there is almost same problem here. Can anyone help me? Code of MainLoop function: <div class="source">
WPARAM MainLoop()
{
MSG msg;

m_overlap_time =0;
m_frame_dt = 0;
m_frame_start_time = -1.0f;
m_frame_counter = 0;
m_timestep = 1.0f / 100.0f;

while(1)											// Do our infinate loop
{													// Check if there was a message
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)					// If the message wasnt to quit
break;
TranslateMessage(&msg);						// Find out what the message does
DispatchMessage(&msg);						// Execute the message
}
else											// if there wasn't a message
{
//==
//*
float last_frame_time = m_frame_start_time;

m_frame_start_time = 0.001f * timeGetTime();//mytime();
if (m_frame_start_time >= 0.0f)
m_frame_dt = m_frame_start_time - last_frame_time;
else
m_frame_dt = 0.0f;

if (floor(last_frame_time) != floor(m_frame_start_time))
m_frame_counter = 0;
else
++m_frame_counter;

float dt =  m_frame_dt;

float total_time = dt + m_overlap_time;

if (total_time > 0.25f)
{
total_time = 0.25f;
}

// split the timestep into fixed size chunks

int num_loops = (int) (total_time / m_timestep);
float timestep = m_timestep;

if (num_loops == 0)
num_loops = 1;
timestep = total_time / num_loops;

m_overlap_time = total_time - num_loops * timestep;

for (int i = 0 ; i < num_loops ; ++i)
{
Man.Calculate(timestep);
}

// Render the scene every frame

RenderScene();
}
}

return(msg.wParam);									// Return from the program
}



##### Share on other sites
I understand what you're trying to do, but cannot understand how your code attempts to do it.

I use:

for(;;) {  while (current_time() > next_update) {    Poll();    Update(next_update);    next_update += update_interval;  }  Render();}

##### Share on other sites
I'm put in simulation routine delta time(time between frames), that routine compute new velocity and position by delta time.

In your routine "Update(next_update)", what does mean next_update? Is it delta time or whole time from begining of simulation?

##### Share on other sites
Quote:
 Original post by CAHI4I'm put in simulation routine delta time(time between frames), that routine compute new velocity and position by delta time.In your routine "Update(next_update)", what does mean next_update? Is it delta time or whole time from begining of simulation?

It is the time since the beginning of the simulation. The delta time is always equal to update_interval, which is a constant, so I do not pass it to the function.

##### Share on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original post by CAHI4I'm put in simulation routine delta time(time between frames), that routine compute new velocity and position by delta time.In your routine "Update(next_update)", what does mean next_update? Is it delta time or whole time from begining of simulation?

It is the time since the beginning of the simulation. The delta time is always equal to update_interval, which is a constant, so I do not pass it to the function.

Ok I might be wrong here but I don't think that is exactly frame rate independent movement. You are basically saying that update_interval is a constant. Unless you are putting this code inside a separate thread, this is pretty much like the old frame rate dependent movement, since you are always advancing your simulation by a fixed amount of time. The longer the render time is the less this Update(next_update) function gets called and whenever it gets called it's only advancing the simulation by a fixed amount of time. You need to take time that has elapsed since the last loop iterations as the advancing time amount to get frame rate independent movement.

##### Share on other sites
Quote:
 Original post by hmmzOk I might be wrong here but I don't think that is exactly frame rate independent movement. You are basically saying that update_interval is a constant. Unless you are putting this code inside a separate thread, this is pretty much like the old frame rate dependent movement, since you are always advancing your simulation by a fixed amount of time. The longer the render time is the less this Update(next_update) function gets called and whenever it gets called it's only advancing the simulation by a fixed amount of time. You need to take time that has elapsed since the last loop iterations as the advancing time amount to get frame rate independent movement.

By "frame", I mean rendering and displaying one frame (as in, one screenful of pixels). My code can (and will) render several times in a row without updating, or update several times in a row without rendering. This is independence between rendering and updating. The only thing that rendering depends on is the passing of time, while the only thing rendering depends on is the amount of free time available.

##### Share on other sites
You will always have jerky movement unless you use the Vertical Sync for your time delay.

##### Share on other sites
Quote:
 Original post by OutAxDxYou will always have jerky movement unless you use the Vertical Sync for your time delay.

Lack of VSync causes tearing artifacts, not jerky movement.

##### Share on other sites
Im kinda new to this forum, but i am going to make the assumption that frame rate independant means not a fixed frame rate (amount of times between frames can differ and not affect the quality of calculations e.g. a space ship busy changing velocity and direction, his changing direction and position calculation is not dependent on number of frames per second and will always be exactly the same movement regardless of frame rate?)

In my limited experience any physics calculation of this nature usually requires extensive use of the cos mathematical function (or anything else that can create a desired arctangent for use in calculations).

##### Share on other sites
No you dont need it anymore than without framerate independent movement. All you do is add a multiplication to your velocity & acceleration with deltatime. In this case the delta time could be changing everyframe but it doesn't have to. Depending on how you have you main loop.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 21
• 12
• 9
• 11
• ### Forum Statistics

• Total Topics
631403
• Total Posts
2999878
×