Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are animations sometimes done using steps based on the amount of time that's passed?

I've noticed that some programmers animate objects based on the difference in time. I am not sure why or even if this is logical. Does anyone know the significance?

Below is a snippet of code that explains what I mean:

var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;
var newBallX = ball.x + ballDX*timePassed;
var newBallY = ball.y + ballDY*timePassed;
like image 644
numerical25 Avatar asked Jan 05 '10 22:01

numerical25


3 Answers

When you animate based on time, you make yourself independent of the framerate. No matter how many frames have passed, your ball will move the same distance in a given amount of time. Compare that to depending on the framerate, which is dependent on many variables, like how much processing power is available to do the animation.

This is a common game-physics issue -- check out Glenn Fiedler's excellent "Fix Your Timestep!" article for a more detailed take on this. (Doing it right is slightly more complicated than just multiplying your direction vectors by the timestep.)

like image 178
John Feminella Avatar answered Nov 15 '22 05:11

John Feminella


The logic is simple.

BallDX => Ball Delta X => The distance the ball can move on the x coordinate in one second

timepassed => amount of time passed

if OldBallX = 0
if BallDX = 10
if TimePassed = 1 sec
Then NewBallX = OldBallX + (BallDX*TimePassed)

Which means

NewBallX = 0 + (10 * 1) = 10 pixels

In that case

if TimePassed = 0.5 sec (half a second)

Then

NewBallX = 0 + (10 * 0.5) = 5 pixels 

Logical?

like image 45
HyperCas Avatar answered Nov 15 '22 07:11

HyperCas


Why NOT do it that way? As opposed to doing what? It is a simple linear motion right? Here is a thought: this allows for the ball to catch up with its intended position in the case other programs are slowing down the computer.

like image 31
Hamish Grubijan Avatar answered Nov 15 '22 05:11

Hamish Grubijan