Sign in to follow this  

An easy question!

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

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

Share this post


Link to post
Share on other sites
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 starts
int sTotalTime = 1000; //the number of milliseconds you want the whole animation to play in
void 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.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

This topic is 4856 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this