I have an OpenGL ES View in Android thats controlled by a matrix for translation. Im trying to figure out a way to get a hint of momentum scrolling as seen in the google maps app or the iPhone. Thanks.
2 Answers
If your problem is in 2d, it is quite simple
- You need to get the elapsed time in each frame
Your onTouch function will find the acceleration of your finger. I forgot the formula on how to get the acceleration from a distance. It should be the second derivative of position with a time variable. But you should always convert your deltaX, deltaY in acceleration. To make it easy you don't really need to put something accurate there. Edit: I don't know why I didn't see it but the function was all there...
acceleration.x = 2(newposition.x - position.x - speed.x * elapsedTime) / (elapsedTime * elapsedTime);
Once you have your acceleration you can set your new position with that code. This is simple physic dynamics in 2d. With your acceleration you can find your speed and with your speed you can find your next position.
speed.x = (float) (mass * acceleration.x * elapsed + speed.x); speed.y = (float) (mass * acceleration.y * elapsed + speed.y); position.x += mass * acceleration.x / 2 * elapsed * elapsed + speed.x * elapsed; position.y += mass * acceleration.y / 2 * elapsed * elapsed + speed.y * elapsed; speed.x *= friction; speed.y *= friction;
Mass and friction will let you define how fast it goes and how fast it will slow down by itself. You probably will have to tweak the code because this dynamic isn't exactly nice if you have to have to scroll backward to slow down.
At the end of each frame, you will have to reset your acceleration to (0,0). And on each new frame after a touch even, the acceleration should be set to something. It should work very well :)

- 13,220
- 6
- 67
- 99
-
1Was looking at your answer and am wondering how you get speed for use in acceleration when your speed formula requires acceleratioin ... One depends and requires the other? – Dan Dec 05 '12 at 13:40
-
Instead of calculating speed directly, it may give better resultats to use acceleration. Initial speed and acceleration is 0. So you have to save speed somewhere to add it to the following frame. Acceleration is nothing else than the change between each frame. The acceleration could be the deltaX, deltaY. The actual speed will be that deltaX * elapsedTime + lastSpeed. This way, the speed is correctly calculated even if a frame takes a lot of time or a small amount of time. Other parameters fake a bit of physic to add inerty and friction. You don't really want it to scroll forever. – Loïc Faure-Lacroix Dec 09 '12 at 22:26
- Measure the speed that the view is scrolling at.
- Detect when the user stops scrolling.
- Gradually decrease the speed that the scroll view is scrolling at.
Something like this:
public void redraw() {
myScrollView.ySpeed = myScrollView.lastY-myScrollView.y;
myScrollView.xSpeed = myScrollView.lastX-myScrollView.x;
if (!userIsScrolling && ySpeed > 0) {
ySpeed--;
}
if (!userIsScrolling && xSpeed > 0) {
xSpeed--;
}
myScrollView.lastY = myScrollView.y;
myScrollView.y += ySpeed;
myScrollView.lastX = myScrollView.x;
myScrollView.x += xSpeed;
}
public void userStoppedScrolling() {
userIsScrolling = false;
}

- 6,687
- 9
- 48
- 76
-
thanks, do you know how exactly i could control the speed at which the user is scrolling though? Right now it just calculates the delta between the starting position and end position of the users finger and then puts it in the matrix. – jfisk Dec 30 '10 at 21:52
-
Sorry, I dont know Android well enough to help with specifics like touch location and such. – Jumhyn Dec 31 '10 at 00:52