• Advertisement
Sign in to follow this  

Processer Independent Animation?

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

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){

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

	double angleDouble = newAngle/360.0f*6.283185f;
	if (GotThrowTime==false){
		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;


	if (bananaVisible==true){
		if (Player_1_turn==true) {
		} else {
	timeSinceLastUpdate=(runTime-gotTime)/60; // 60 = desired FPS

Share this post

Link to post
Share on other sites
If you don't mind hogging the CPU you can do something like this:

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


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).

Share this post

Link to post
Share on other sites
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;

Start = clock();
lastTime = 0;


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

return dt;

double Timer::SecondsSinceRun(){
return secondsSinceRun;

Share this post

Link to post
Share on other sites
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

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.

Share this post

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

  • Advertisement