Updating Game World correctly

Started by
6 comments, last by hoogie 19 years, 5 months ago
Hi all, I am currently having difficulties in updating my game world correctly. I am aiming to have the game running at 60 frames a second so my Render function should be called appropriately, and I am also trying to establish frame rate independence by using a deltaTime value to control object movement. Here is an example of my following current update code: static DWORD lastTime; DWORD timeNow = GetTickCount(); for( int count = 0; count < m_entityVector.size(); count++) { // sets deltaTime and updates world entities DWORD deltaTime = (timeNow - lastTime) * 0.001f; m_entityVector[count]->Update(deltaTime); } // if render update needed renders entities if( (timeNow - lastTime) > 60 ) { // renders world and sets lastTime CWorldModel::RenderWorld(); lastTime = timeNow; } Basically I seam unable to get my render refresh or gameWorld update running correctly (time wise). Anyone have any ideas where im going wrong? Cheers
Advertisement
You should use floats for the time delta's. I'm using a high performance counter and use floats for the time delta, and it works like a charm...

Toolmaker

I was mainly wandering if my if statement for the Render update seams correct? And is it necessary to have an if statement for the game World update or does the delta time make this unnecessary?

Cheers
The timedelta is supposed to be used to eliminate that if statement capping the framerate. It's also strange that you cap your framerate at ~17 fps (you render every 60 milliseconds, 1000/60 ~ 17). It should look more like this:
void UpdateAndRender(){	static DWORD lastTime;	DWORD timeNow = GetTickCount();	float deltaTime = (timeNow - lastTime) * 0.001f;	lastTime = timeNow;	for( int count = 0; count < m_entityVector.size(); count++)	{ 		// sets deltaTime and updates world entities		m_entityVector[count]->Update(deltaTime);	} 	// renders world and sets lastTime	CWorldModel::RenderWorld();}

Does that help?
Yeah that does help thanks. The reason I was trying to cap the framerate is that I wanted to cap it at a solid 60FPS in order to avoid jumpiness in the FPS.
Im still having trouble in the fact that my delta time values seam to ve abnormally high, any ideas?
One question though, if the program is just updated all the time like that wont it put extra strain on the system? I assumed the if statement was used to insure that updates didnt occur to often?

Cheers
DWORD deltaTime = timeNow - lastTime

That would get the time difference, you then * 0.001f - is that to convert it to milliseconds?

Roger that, lets run like hell!
LastTime - ThisTime gives it in milliseconds. Multiplying by 0.001f (equivelent to dividing by 1000) gives it to you in seconds. Make sure ALL your DeltaTime values used floats. DWORDs will NOT work. What kind of DeltaTime values are you getting? How "abnormally high"? Also, don't cap you frame rate. The whole point of all this delta time stuff is to eliminate that. Capping would defeat the purpose of everything you're hoping to achieve in this thread.
Right frame rate is no longer cappedn and all deltaTime data is now float.
When I first run the program my first deltaTime is 2379.3120 after is has been multiplied by 0.001f, that should convert it to seconds as the GetTickCount(); function returns milliseconds.
This is what my function currently looks like:

// retrieves elapsed timestatic DWORD lastTime;DWORD timeNow = GetTickCount();float deltaTime = (timeNow - lastTime) * 0.001f;// loops through world entities updating themfor( int count = 0; count < m_entityVector.size(); count++){      m_entityVector[count]->Update(deltaTime);}	// renders world and sets lastTimeCWorldModel::RenderWorld();	lastTime = timeNow;


It doesnt seam possible for the resultant timeDelta to be so high but it is, I also noticed that wothin my gameObject updates any conditional statements that involve the deltaTime dont seam to work as they should, for example if I have an

IF(deltaTime > 10)
{
dothis();
}

Even if deltaTime is over 10 it isnt getting to doThis();

Thanks again for the help

[Edited by - hoogie on November 12, 2004 10:46:12 AM]

This topic is closed to new replies.

Advertisement