c - Calculating scroll inertia/momentum? -


how calculate scrolling momentum scroll event?

i understand there have 2 timestamps @ beginning of scrolling @ end. there has "axis change" variable amount scroll without inertia.

this current code responsible ending of scrolling:

if ((type == kmxtend || type == kmxtmovedout) && _isscrolling)     {         long int finishtime = mxtimestamp();         printf("scend: ending scroll @ %ld\n",finishtime-_begintime);          /* scrollx change in x axis */         /* finishtime time touch down touch */          printf("  * time: %ld changex: %f\n",finishtime,scrollx);          _isscrolling = false;         _originalscrollpoint = _scrollpoint;     } 

is possible calculate "inertia addition" that? additional offset gained inertia can scroll in addition primary scroll value. or need additional variables?

i need because i'm writing own ui toolkit, isn't based on anything.

you simulate "recent axis changes" queue.

if store last half second of changes corresponding timestamps, can test if queue longer value n (ie if user dragged quicker usual towards end). know total distance traveled in last half second, time, can speed.

scale speed reasonable (say.. 15px/.5sec, map ~25px/sec) , apply negative acceleration (also appropiately scaled, example above, -20px/sec) every couple of milliseconds (or fast system can easily handle it, don't overstress this).

then run timer, updating speed @ each tick (speed+=accel*time_scale), position (position+=speed*time_scale). when speed reaches 0 (or goes below it) kill timer.


Comments