Jump to content
  • Advertisement
Sign in to follow this  
Cydriic

How to make Time Based Updates

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

Hi Everyone, 

 

I'd like to understand how to make time based updates in my game, instead of changing variables every time I work and run from a different computer.

 

 

 

So if I have these input controls, how would I go about it:

 

 

GLfloat MoveFactor = 0.01f; //0.05f //0.01f (As you can Tell I have different values for different computers)

 

if (keys[VK_UP])
{
 
MoveForward = true;
xpos -= (float)sin(heading*piover180) * MoveFactor;
zpos -= (float)cos(heading*piover180) * MoveFactor;
/*if (walkbiasangle >= 359.0f)
{
walkbiasangle = 0.0f;
}
else
{
walkbiasangle+= 10;
}
walkbias = (float)sin(walkbiasangle * piover180)/20.0f;*/
}
else
MoveForward = false;
 
if (keys[VK_DOWN])
{
 
MoveBackward = true;
xpos += (float)sin(heading*piover180) * MoveFactor;
zpos += (float)cos(heading*piover180) * MoveFactor;
/*if (walkbiasangle <= 1.0f)
{
walkbiasangle = 359.0f;
}
else
{
walkbiasangle-= 10;
}
walkbias = (float)sin(walkbiasangle * piover180)/20.0f;*/
}
else
MoveBackward = false;
 
 
How would I go about implementing it ? I use Windows.h, not glWindows.h
 
 
Thanks a lot. 
Edited by Cydriic

Share this post


Link to post
Share on other sites
Advertisement

Really, you want to give your movable objects velocity.  This velocity is just a distance/time.  I would advise starting with pixels/second, since it is the most basic method to work with (when you get more experienced, you can look at creating your own world coordinates and using a generic unit/seconds).

 

So, when you go to move an object that has, say, 10 pixels/second velocity, you'd take the elapsed time since the last update, say .033 seconds (30 frames per second, on a slow computer), and move the character 10 pixels/s * 0.33s = 0.33 pixels.  After 30 frame updates (which, at .033 seconds per update is 1 second), the character would have moved 10 pixels.

 

if your computer updates at 120 frames per second, time elapsed since last frame (deltaTime) would be .0083 seconds;  however, after 1 second (120 updates), the object would still have only moved 10 pixels.

 

void Object::Update(float deltaTime)
{
  // set the xPos and yPos based on xVelocityi and yVelocity and time elapsed)
  xPos += xVelocity * deltaTime;
  yPos += yVelocity * deltaTime;
}

Share this post


Link to post
Share on other sites

Nice !!!

 

Great stuff guys, I was able to implement a first little structure that I will enhance progressively\y.

 

The Article really got me to understand the basics.

 

BeerNutts, your explanation is great thanks a lot!

 

 

Cheers

Edited by Cydriic

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!