Smooth Movement for OpenGLContext
Last update on .
Finally got around to implementing smooth movement for OpenGLContext. Instead of just "jumping" a given distance on each press of the key, the default movement manager will now smoothly interpolate to the position (same for orientations). Code is pretty straightforward, the only tricky thing was getting holding down the key to make the motion faster.
Under the covers I'm using a pair of timers to handle the interpolation. Each time a key-press event is received the timer is restarted to interpolate between the current position and the target position. Problem was that the timers would start a frame behind, and by the time they were firing the next key-press event would be firing as well.
Result was that the cycle-fractions produced for the first frame of the animation would be about 1/2 of the time that was intended (since the key-press processing occurs between frames). Since a new keyboard event would come in that frame too, the timer would get reset all over again. Result was that pressing and holding the key would slow down the motion by about 1/2 compared with pressing discretely.
To get around that, I compress the natural fraction from the timer by 1/2 and shift it 1/2 forward. Result is that the viewer jumps forward 1/2 of the distance, then slowly adjusts into the final position if they press the key once. If they kept the key pressed, however, they tend to move forward at about 1/2 of the step distance per frame. Only real downside is that speed is once again affected by frame-rate, rather than being entirely simulation-driven. Still, it looks reasonably good and feels natural.
Pingbacks are closed.
Comments are closed.