Advertisement

heeeeeeeeelp meeeeee!

Started by July 15, 2000 11:49 AM
1 comment, last by youngo 24 years, 5 months ago
how would i make an object with a decreasing velocity move the same distance when the frame rate is not constant? this is something like what i do and it''s not working for me: QueryPerformanceCounter(&bb); velocity.x -= .1f*(bb.QuadPart - aa.QuadPart); if ( b.v.x <= 0) velocity.x = 0; QueryPerformanceCounter(&aa); object.x += velocity.x;
I think basically what you want to know is how to keep the game speed constant, based on normal time (real seconds), not on the speed of the game (frames per second), right? In my program, I have a small timer function to record the length of each frame in seconds (into a variable called FrameLen). What I do is base everything in the game off that, not off the number of frames that have gone by. So if you have an object moving 20 pixels per second (pps), each game frame you would move the object 20pps*FrameLen. So if your game is running at 50 frames per second, then each frame would be .02 seconds long, and therefore the object would move 20pps*.02sec, or .4 pixels per game frame. Or if you run it on a faster computer, say running at 100 frames per second, then the length of each frame would be .01 seconds, and the object would move 20pps*.01sec, or .2 pixels per game frame. So here is a basic equation:

float curSec=0, oldSec=0, frameLen=0;

void mainLoop()
{
object.x += velocityPerSecond*frameLen;
//
//the rest of your game code here
//

//these are the timers, run at the end of each game loop,
// recording the length of the frame and the number of seconds
// that have gone by since the beginning.
oldSec = curSec;
curSec = (GetTickCount()-tickZero)/1000;
frameLen = curSec - oldSec;
}

I hope this helps, I was pretty long winded.
Advertisement
I wouldn''t use windows timers (too complicated)
instead:
link w/ lib: winmm.lib

#include
.
.
.
DWORD lasttime,currenttime=timeGetTime();
float timeoffset,fps;
.
.
.
// beginning of the draw proc
lasttime=currenttime;
currenttime=timeGetTime();
timeoffset=currenttime-lasttime;
fps=1.0f/(timeoffset/1000.0f);

"Everything is relative." -- Even in the world of computers.
everything is relative. -- even in the world of computers... so PUSH BEYOND THE LIMITS OF SANITY!

This topic is closed to new replies.

Advertisement