Run game at desired frames per seconds?

Started by
4 comments, last by JY 18 years, 4 months ago
I currently got help doing the following code, below! I was wondering if I wanted to get Processer Independent Animation, what would I do? do I just do like I have done below diveided my update time by my desired Frames Per Second? "timeSinceLastUpdate=(runTime-gotTime)/60; // 60 = desired FPS" What do I do? Any help would be greatly appreated! Thanks in advance! P.s code below:

  double newAngle=angle;
	if (Player_2_turn==true){
		newAngle=180-angle;
		glColor3d(1,1,0);
	}

	bananaX += bananaVelX * timeSinceLastUpdate;
	bananaY += bananaVelY * timeSinceLastUpdate;
	 
	bananaVelX += (wind) * timeSinceLastUpdate;
	bananaVelY -= (gravity * timeSinceLastUpdate);

	double angleDouble = newAngle/360.0f*6.283185f;
	if (GotThrowTime==false){
		gotTime=runTime;
		bananaVisible=true;
		bananaVelX = (cos(angleDouble) * (strength/4));
		bananaVelY = (sin(angleDouble) * (strength/4));
		if (Player_1_turn==true) {
			bananaX = CurrentMonkey1CoLeftX;
			bananaY = CurrentMonkey1CoTopY;
		} else if (Player_2_turn==true){
			bananaX = CurrentMonkey2CoRightX;
			bananaY = CurrentMonkey2CoTopY;
		}

		GotThrowTime=true;
	}

	if (bananaVisible==true){
		glBegin(GL_POLYGON);
		glColor3d(1,1,0);
		glVertex2f(bananaX,bananaY);
		glVertex2f(bananaX,bananaY+5);
		if (Player_1_turn==true) {
			glVertex2f(bananaX+5,bananaY+5);
			glVertex2f(bananaX+5,bananaY);
		} else {
			glVertex2f(bananaX-5,bananaY+5);
			glVertex2f(bananaX-5,bananaY);
		}
		glEnd(); 
	}
	timeSinceLastUpdate=(runTime-gotTime)/60; // 60 = desired FPS


Edit* I already have a timer below. I was just wondering what I had todo to make the banana move at the same rate on every computer! Basically the code before makes a square go up at a certain angle and fail at the same rate as gravity. but I want it to do this at the same frame rate on every computer there for been Processer Independent Animation! My timer works out the delta time(dt) which I used to get my frame rate! My secondsSinceRun gets turned in to runTime which is used to display how long the game has been running! The question is I could get the computers frame rate and if it is less then 50 then tought it will just have to be slow, but am not sure what I multiply or how I use this information to get it to run at this speed?

#include "timer.h"
#include <time.h>

static double current;
static double systemTime;
static double lastTime;
static double Start;
static double dt;
static double secondsSinceRun;

Timer::Timer(void){
	Start = clock();
	lastTime = 0;
}

Timer::~Timer(void)
{
}

double Timer::tick(){
	do {
	current = clock();
	systemTime = (double)(current - Start)/1000; 
	dt = systemTime - lastTime;
	lastTime = systemTime;
	}while (dt <= 0);

	return dt;
}

double Timer::SecondsSinceRun(){
	secondsSinceRun=systemTime;
	return secondsSinceRun;
}

*
Advertisement
while( gameIsRunning ) {  // Compute the number of frames that I should do, based on the time since the  // last time I did this. Here, frameDuration is 1.0/60.0, assuming time is  // in seconds.  numberOfFramesToDo = (currentTime - previousTime)/frameDuration;  previousTime += numberOfFramesToDo * frameDuration;  // Do that many frames.  for( ; numberOfFramesToDo > 0; --numberOfFramesToDo ) { DoFrame( ); }}
I'm a bit simple I do not quite understand where I run this and I was trying to find out how I make say a ball go accross the screen at the same rate on any computer?

E.g

ball -----> ball -----> ball -----> ball -----> ball -----> ball -----> ball

and it would do this on any computer?

edit*
this is what I have done! is it right?

	bananaX += (bananaVelX * timeSinceLastUpdate)*dt;	bananaY += (bananaVelY * timeSinceLastUpdate)*dt;	 	bananaVelX += ((wind) * timeSinceLastUpdate)*dt;	bananaVelY -= (gravity * timeSinceLastUpdate)*dt;timeSinceLastUpdate=(runTime-gotTime); // no longer divided by 60


I don't have enought computer so I can't check it? :(
*

[Edited by - CodeBox on November 28, 2005 4:27:15 PM]
What is dt? (hint: it's the same thing as timeSinceLastUpdate, so you should remove it)
dt is not same as timeSinceLaseUpdate!

Time since last update is the seconds since the banana was throw

runTime might = 33.3,
gotTime might = 25.3,
therefore timeSinceLastUpdate = 8 seconds

timeSinceLastUpdate=(runTime-gotTime)

dt is the delta time the time it takes to update a frame I think!
dt = systemTime - lastTime;
lastTime = systemTime;

systemTime may = 10.005 seconds,
and lastTime may = 10.002 seconds,

meaning it took 0.003 seconds or 3 miliseconds to update the frame??
so thats why I timesed it by this is this correct?
If you want an animation to run at the same rate on every computer, you have to make it time based rather than frame based. So, instead of moving something x pixels per frame you need to give it a time-based speed of x pixels per second.

Then when you come to update each frame; calculate the difference between the time now and the time the last frame was updated. This as a fraction of a second is then multiplied by your speed, acceleration etc. parameters to give the value for the current frame.

So, if your banana has a velocity of 100 pixels per second and it's 23ms since your last frame, move the banana 2.3 pixels.

This will give you an animation that runs at the same speed on all computers. It will be more jerky on slower computers because the time between updates is greater so the difference in position will be greater.
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan

This topic is closed to new replies.

Advertisement