Skip to main content
gamedev.net
🔒 Locked

Object movement problem

Started by spe Jun 26, 2004 at 7:07 AM 4 replies 1.2k views
Original Post
spe
spe
Hi! I'm writing a simple 2d game and I'm using dx9 (D3DXSprite) for the graphics. I have a prob with object movement. I'm using time based movement and get the fps like this : there are two functions :

float t0,t1;

void StartFps()
{
	t0=(float)GetTickCount();
}
void GetFps()
{
	t1=(float)GetTickCount();
	fps=1000.0f/(t1-t0);
}

so :
StartFps()
Rendering and stuff
GetFps()
the movement is done like this : if (key_right) xv+=300.0f/fps; ... xPos+=xv/fps; yPos+=yv/fps; The problem is the movement is realy jearky. There must be something wrong with the fps cause I've noticed that for about every third frame the fps drops to the half or so. I Saved the fps for every frame in a file and it looked something like this : 100 100 50 100 100 100 50 100 100 100 47 ... Have someone have an idea how this could be solved? Spe
JSoft@coder.hu
Anima
Anima
You should try using the QueryPerformanceCounter and QueryPerformanceFrequency functions - these are much higher resolution and more accurate. When I used the GetTickCount function in my own game it kept telling me that a cycle of the main loop took 0 milliseconds (infinite FPS) so it wasn't much good at all.
Sr_Guapo
Sr_Guapo
NEVER base movement off the FPS. Use real time based modeling. Calculate how much time has passed since the last update, then transform the object accordingly. If the object moves 500 pixels/second, and you have the current time and previous update time, then:

location += (t1-t0)*500

Also, it is definately a good idea to use QueryPerformanceCounter for this. You will need very accurate times to calculate the position with any accuracy. There are plenty of tutorials out there. Good Luck!
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
Sphet
Sphet
In the projects that I've been involved with we use a task manager which holds a list of callback 'Update' functions. The task manager is responsible for figuring out how long the timestep was between each task's previous update and the current one, and then invokes the Update function passing the argument of the time delta, in seconds. When dealing with object movement, you never really want to increment the position directly from a keystroke - you want to trap the keystroke and set a state in the object 'accelerating' or 'moving forward' or what have you, and then when you update your object, you can change it's position using the timedelta (in seconds) multiplied by the speed the object should be travelling, per second. It works well and the added bonus of the task manager lets you reorginize the calls to Update() really easily. Our task manager is extended to also only update tasks when the overall game state is matched to the bitmask the task was created with. This lets us go into the pause menu, set the game state to paused, and none of the AI update functions ever get called until the game is resumed again.
spe
spe
It works fine now! Thanks again!
JSoft@coder.hu

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.