An easy question!

Started by
4 comments, last by PnP Bios 19 years, 7 months ago
Hi everybody! I've got a little problem I'm sure you will be able to solve! I'll use an example to explain it cuz it's not easy to tell whit words :P! We suppose to have, for example, a Camera that should move on the x axis from point 0 to point 200! In the update loop I write something like Camera.x+=1. Now, on my PC it takes 5 seconds from point 0 to point 200 while on my notebook it takes 10 seconds!!! I've tried to put at the end of the loop something like while(timePassed>30){doNothin} in order to fill the gap... but it doesn't work!! What shall I do!!! Thanks in advance! :)) Mkr
Advertisement
It sounds like you're depending on the speed of the paint refresh message to drive your animation. This is a bad idea, because it won't be the same across systems.

Instead, use GetTickCount() (on Windows) or some other means for finding out how much time has passed since you started moving and figure out how much your camera should have moved in that time, and use that to drive your animation. So for example:

int sStartTime;    //set to GetTickCount() when your animation startsint sTotalTime = 1000;     //the number of milliseconds you want the whole animation to play invoid Paint(){    float fPercent = float((GetTickCount() - sStartTime) / sTotalTime);    float newX = startX + ((destinationX - startX) * fPercent);    //set the objects x value to newX}


That's kind of a cheap hack, but hopefully it communicates the idea to you. You move your object a percentage along the total distance, based on the amount of time that's elapsed. You calculate the percentage to move it by comparing the amount of time that has elapsed from the start of the animation against how long you want the animation to take.
This would be the big issue of Time-Based Movement and Frame-Based Movement. What you're currently trying to do is the latter. You want to move a fixed amount every frame, and you're trying to fix the speed of each frame with your empty while(); loop. This is typically the easier way to do things. It's not as flexible, but it's simple. Time based movement would update the position of a movie object a variable amount each frame, depending on how long the frame lasted. For short frames, the object doesn't move as much. For longer frames, the object moves farther. This typically takes a bit more effort to implement (and can be hurt by poor-quality time functions), but is usually more flexible and forgiving when running on widely differing computers.

As far as your while loop goes, though, if that's literally what you have, then I'm pretty sure you need a less-than rather than a greater than. The basic idea is correct, though. Also, you might want to give time back to the OS from within the loop. Otherwise, such a loop (busy-loop) will eat up 100% processor usage for no reason. If it's Win32, just call Sleep(n) from within the loop, using a small number for n, anywhere from 0 to 15 or so. (0 will let something else run if there is something else that wants to run.)
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Thx you both man! I'll work on it! Thx again!

Mkr
Huston, we have a problem!
The GetTickCount() method sometimes gives me a gap of some milliseconds (like 1,2,3,4,7,8 etc...) so the camera sometimes advance with 'jumps'!! How can i solve this problem!!!!
Thx in advance!

Mkr
Use a high precision timer.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One

This topic is closed to new replies.

Advertisement