Animation
Posted: Tue Jun 18, 2024 6:09 am
Hello everybody
I decided to share thoughts about animation, which are not mentioned in KC documentation.
The traditional way of animating is to setup a timer and process timer events in a OnTimer() handler. We believe now that a better way to do it is to avoid the timer altogether and simply to run an animation loop at full speed:
while(keepAnimating) DoTheAnimationLoop();
DoTheAnimationLoop() here is supposed to have basically the same code as the OnTimer() handler.
The difference is that there is no timer or events involved. Let’s assume for simplicity that animation is spinning of an object. DoTheAnimationLoop() would do something like myIFrame2.Rotate(angleIncrement, ...) and call KC.UpdateView().
The direct loop for animation is good because it is running the process at full speed, which is what you want. No need for external code executed and no need for guessing what the interval should be. So it is the smoothest possible rendering. This is how games are run.
The only thing which remains to control is the rotation angle angleIncrement. If it is small, rotation smooth but slow. If you increase it everything is faster but jerkier.
A drawback is that the animation blocks all message processing etc on the thread. In the eMotion sample, for example, when the simulation is running it is still possible to change the view point and zoom in. I believe, though, it is not very important as animation is supposed to apply heavy load and the user normally focuses its full attention on it. Also modern multi core processors allow running other applications without much trouble.
With the timer animation on the other hand the tricky part is to select time interval as small as possible for animation to be smooth, but it should not be shorter than duration of a single OnTimer() call. If it is the events are queued, possibly indefinitely many, which slows things down.
Gerard
I decided to share thoughts about animation, which are not mentioned in KC documentation.
The traditional way of animating is to setup a timer and process timer events in a OnTimer() handler. We believe now that a better way to do it is to avoid the timer altogether and simply to run an animation loop at full speed:
while(keepAnimating) DoTheAnimationLoop();
DoTheAnimationLoop() here is supposed to have basically the same code as the OnTimer() handler.
The difference is that there is no timer or events involved. Let’s assume for simplicity that animation is spinning of an object. DoTheAnimationLoop() would do something like myIFrame2.Rotate(angleIncrement, ...) and call KC.UpdateView().
The direct loop for animation is good because it is running the process at full speed, which is what you want. No need for external code executed and no need for guessing what the interval should be. So it is the smoothest possible rendering. This is how games are run.
The only thing which remains to control is the rotation angle angleIncrement. If it is small, rotation smooth but slow. If you increase it everything is faster but jerkier.
A drawback is that the animation blocks all message processing etc on the thread. In the eMotion sample, for example, when the simulation is running it is still possible to change the view point and zoom in. I believe, though, it is not very important as animation is supposed to apply heavy load and the user normally focuses its full attention on it. Also modern multi core processors allow running other applications without much trouble.
With the timer animation on the other hand the tricky part is to select time interval as small as possible for animation to be smooth, but it should not be shorter than duration of a single OnTimer() call. If it is the events are queued, possibly indefinitely many, which slows things down.
Gerard