Sign in to follow this  
hoogie

Updating Game World correctly

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 time
static DWORD lastTime;
DWORD timeNow = GetTickCount();
float deltaTime = (timeNow - lastTime) * 0.001f;

// loops through world entities updating them
for( int count = 0; count < m_entityVector.size(); count++)
{
m_entityVector[count]->Update(deltaTime);
}

// renders world and sets lastTime
CWorldModel::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]

Share this post


Link to post
Share on other sites

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