Counting seconds and moving

This topic is 3857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi! I have a little problem moving my object in the scene. The idea is to move the object when the user presses 'S' (from 'start') on the keyboard. So, when 'S' is pressed, my object should move from the start to the end point in exactly 15 seconds. I tried some things, like using C's time.h library, but I didn't achieve much :( Does anyone know how to move an object a specific number of seconds? Thanks a lot! Darie

Share on other sites
What you probably want to do first is have your object have a velocity, which is distance / time. Your distance may be in pixels or a different kind of unit, it doesn't matter. You probably want your time to be set in seconds. So let's say you have the object moving 150 pixels in 15 seconds, then it has a velocity of 10 pixels/second.

The next step is to make it so that on every update call, you only move the object a tiny little bit. How much? This depends on how much time has past since the last update. Let's pretend your computer is really bad and you only get 1 frame per second. So your object is only updated every 1 second. All we would do would be moving the position of your object by velocity * time. So if your object starts at 0, then our position is 0 "plus" 10 pixels/second * 1 second, which then would now be at 10 pixels. If you give a smaller about of time, say 1/60th of a second, then the distance the object moves per frame is much less. This system of having objects move based on time will make it so that any computer will move your object to the end location in 15 seconds.

How do we get the time of the current frame? Well, it depends where you measure the time difference, but I tend to measure the time right before I update all my objects. I compare this time to the time I measured the previous frame and find the difference. The difference in time is what I pass to all my objects that need to be updated. Here is a small demonstration:

oldTime = GetTickCount( );Initialize( );while( running ){  newTime = GetTickCount( );  UpdateObjects( newTime - oldTime );  oldTime = newTime;}

One thing to note is that GetTickCount( ) is a Windows function call. It also gives the amount of miliseconds since Windows started. You probably don't want to update with miliseconds, so you'd have to divide the result ( or newTime - oldTime ) by 1000.0.

Side note: Does anyone know why I can't display a plus symbol?

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• Forum Statistics

• Total Topics
633740
• Total Posts
3013621
×