Processer Independent Animation?

Started by
2 comments, last by VladR 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

Advertisement
If you don't mind hogging the CPU you can do something like this:

<code>
float lastTime = SDL_GetTicks()*0.001f;
float curTime;
float timeElapsed;

while (!finished) {
curTime = SDL_GetTicks()*0.001f;
timeElapsed = curTime-lastTime;
lastTime = curTime;
//do caluclations based on timeElapsed
}

</code>

timeElapsed will be a time in seconds (assuming you know that SDL_GetTicks returns milliseconds), so all game animation timing should be in seconds, so multiply something like (position+=speed*timeElapsed).
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;
Basically, you need following variables:

TimeStart - Time when you started the animation (you pressed a key to walk)
TimeEnd - Time when the animation should end (for example this is TimeStart + length of one walking cycle of your character)
TimeOfLastUpdate - Latest time when you were in your Animation Processing routine (APR). When the animation starts, set this to TimeStart
CurrentTime - Current time when you`re entering your APR - e.g. each rendering frame
ElapsedTime = CurrentTime-TimeStart

APR:
void APR (CurrentTime)
{
if (CurrentTime < TimeEnd)
{
ElapsedTime = CurrentTime-TimeStart;
// Handle the animation here based on the value of ElapsedTime
// E.g. in character animation you`re selecting appropriate animation frame based on this value

...

// After the animation is processed set the TimeOfLastUpdate
TimeOfLastUpdate = CurrentTime;
}
}


Some of above variables could be omitted, but believe me, that some time later during debugging you`ll be glad you have all of them in Watch window.

VladR My 3rd person action RPG on GreenLight: http://steamcommunity.com/sharedfiles/filedetails/?id=92951596

This topic is closed to new replies.

Advertisement