Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Dark_Streak

Time based with frame based movement?

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

I have a quick question about time-based movement/animation. I understand that different systems will run games at different speeds, so it is advisable to stay away from frame-based movement - in other words, incrementing an amount every frame. Instead time-based movement should be used. So say I have calculated the frames per second my game is getting, therefore I have the frame interval. This frame interval should be used to set the speed of an objects movement. But I don''t understand - how will this make the object move at the same speed on any system? For example, if I want to rotate an object, I might use something like this: RotAmount = RotAmount + (Speed * frameInterval); // Then RotAmount can be used to rotate an object glRotatef(RotAmount,0,1,0); DrawObject(); As you can see above, when I calculate my rotation value (RotAmount), it is still really being incremented every frame, because ''RotAmount = RotAmount + other stuff''. So by multiplying my speed by the frameinterval, the object will rotate at the same speed regardless of the system the game is run on. Is this correct, or am I missing something here? WWW.SPANNERWORX.CO.UK - Coming soon.

Share this post


Link to post
Share on other sites
Advertisement
Don''t go about trying to calculate the fps of the computer you are running on. Instead, determine how far your object should move in 1 second ( 10 seconds, 1min, it doesn''t really matter ). Then for each loop through your game code determine how much time has passed since the last frame. This change in time interval is then used to calculate how far to move/rotate your object.

Ex: Object moves 20cm in 1sec.
0.1 seconds have passed since the previous frame.
Therefore, move the object (20)(0.1) = 2cm
If 0.3 seconds have passed move the object (20)(0.3) = 6cm

A more complicated example:
Object rotates 360degrees in 10 seconds (10000milliseconds)
In 1 second in rotates 36degrees
In 1 millisecond it rotates 0.036 degrees (0.036deg/msec)
Change in time since last frame: 20 milliseconds
Therefore move object: (0.036deg/msec)(20msec) = 0.72degrees


Your code could read something like this

// RotPerMilliSecond is determined by you, or some sort of physics equation

DeltaTime = time since last frame
RotAmount = RotPerMilliSecond * DeltaTime

glRotatef( RotAmount, 0, 1, 0 );
DrawObject();


Hope this helps,
Chris Z.

Share this post


Link to post
Share on other sites
RotAmount = RotAmount + (Speed * frameInterval);
// Then RotAmount can be used to rotate an object
glRotatef(RotAmount,0,1,0);
DrawObject();

well smaller the frameInterval, smaller (speed * frameInterval) would be .. larger it is ... larger the (speed * frameInterval) thing would be. so time controls the rotation here ... well amount of angle increment per frame.

think think and think!

do the [winman_thing]

Share this post


Link to post
Share on other sites
Ahhhhh, thanks guys.

Yes, theWindowsMan, you are right - it does make sense when you put it like that. I should have realised that but, hey, sometimes our brains aren''t all in the sparking phase.

Cheers again for the feedback

WWW.SPANNERWORX.CO.UK - Coming soon.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
In windows, Use GetTickCount() to return the number of ticks (milliseconds) since midnight. This can be used to calculate the time elapsed since the animation began.

assign a speed to the animations in terms of (glunits)ms^-1 (glunits per millisecond)

when drawing the object, you can determine the distance the object has moved by using the simple mechanics formula:

Displacement(s) = Velocity(v) * Time(t)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!