Sign in to follow this  

Frame rate independent movement one more time

This topic is 4173 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

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by CAHI4
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?


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 this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by CAHI4
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?


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 this post


Link to post
Share on other sites
Quote:
Original post by hmmz
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.


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 this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by OutAxDx
You 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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

This topic is 4173 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this