Sign in to follow this  

Help with timing movement

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

Hello, I have read countless of tutorials and threads about this subject but I don't seem to be able to get it to work. Need some help to localize the problem. I have a working game loop and I have a working fps counter which tells me the current FPS. I have no frame limit but what I want to get working is my movement of sprite/objects according to the time. Float variables initialized in my game constructor
myFrameCount = 0;
myLastUpdateTime = 0;
myTicksPerSecond = 1000;

Before my game loop I initialize my start time, SDL_GetTicks() will return me the ms since SDL was initialized
myStartTime = ( GLfloat ) SDL_GetTicks();

I choose to start doing FPS calculation at the beginning of the loop, since when I return, a frame has successfuly been swapped in to the front buffer.
GLvoid GameDirector::calculateFPS() {
	//Increase frame count
	myFrameCount++;

	//Get current time
	myCurrentTime = ( ( GLfloat ) SDL_GetTicks() - myStartTime ) / myTicksPerSecond;

	//Update FPS every half second
	if( myCurrentTime - myLastUpdateTime > 0.5f ) {

		//Calculate FPS
		myFPS = ( GLint ) ( myFrameCount / ( myCurrentTime - myLastUpdateTime ) );

		//Reset
		myLastUpdateTime = myCurrentTime;
		myFrameCount = 0;
	}
}

So lets talk about the player. While the player keeps pressing a key, the velocity (until the max limit is reached) will increase in the direction the player wants to go.
if( myKeyDown )
	player.accelerate( DIRECTION_DOWN );

The player will then move to the new position based on velocity, if the velocity is 0 the player will obviously not gain a new position.
player.move( ** insert correct float delta time here ** );

//Handle collision //Handle game logic //Clear screen //Draw //SwapBuffer //Restart game loop (going back to calculateFPS()) This is what my move method looks like ->
GLvoid GameCharacter::move( GLfloat deltaTime ) {
	myPositionX += ( myCurrentVelocityX * deltaTime );
	myPositionY += ( myCurrentVelocityY * deltaTime );
}

So first of all, before even talking about the delta time, does my calculation of the FPS look correct? I am getting perhaps 570fps without any limits. Setting a delay of 10 ms gives me 70fps which should prove it is actually working, right? So my question is, how do I get the delta time? I have tried some different ways but perhaps I am expecting results that I can't get. Setting a movement with time should move the sprite/object in same speed no matter what the fps is, right? I have access to all the time variables. Sorry for any spelling errors etc, going to sleep now. GN

Share this post


Link to post
Share on other sites
Quote:
Original post by energydrink
So my question is, how do I get the delta time?


Usually something like this:

unsigned previousTick = 0;
unsigned currentTick = previousTick;

while ( gameLoop ) {
previousTick = currentTick;
currentTick = SDL_GetTicks();
unsigned elapsedTicks = currentTick - previousTick;
float elapsedTime = (float)elapsedTicks / ticksPerSecond;

// Logic
// Rendering
}



Also, suggested reading.

Share this post


Link to post
Share on other sites
In the above example, before the loop where currentTick is initialize, I would recommend initializing it to like so:

unsigned currentTick = SDL_GetTicks();

Without this, then during the first elapsed time could be very large and potentially cause issues, though it is also possible it slips by unnoticed.

Share this post


Link to post
Share on other sites

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