• Create Account

## How to make Time Based Updates

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Cydriic  Members

174
Like
0Likes
Like

Posted 07 July 2014 - 08:56 AM

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, 07 July 2014 - 08:57 AM.

### #2belfegor  Members

2833
Like
2Likes
Like

Posted 07 July 2014 - 09:18 AM

If you got trouble implementing i can share you my code. Just ask.

### #3BeerNutts  Members

4288
Like
3Likes
Like

Posted 07 July 2014 - 12:37 PM

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;
}


My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #4Cydriic  Members

174
Like
1Likes
Like

Posted 08 July 2014 - 08:46 AM

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, 08 July 2014 - 08:46 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.