Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find angle and speed for user touch event around a circle on Android

Math has come on my way again and has defeated me. I need your help to regroup and attack again.

What I've got:

enter image description here

I've got a surfaceview and a circle as a bitmap. I need to rotate the bitmap as the user moves the finger around the circle edge. The faster the user slides, the more I need to rotate the image. Seems simple but not really easy to implement.

What I need

I need to calculate the angle on which to rotate the image on onDraw event. From what I've thought so far I need two things: - the angle between new touched point and the old one. I've done a simple function that takes care of this:

     private int getAngleBetweenTwoTouchedPoints(double oldX, double oldY, double newX, double newY)
     {
         return (int) Math.abs(Math.atan2(newY - oldY, newX - oldX));
     }

the angle returned by this varies from 0 to 1 and I believe it is correct. By incrementing the image rotation matrix angle by this value I get a slow rotation, basically by 1 unit. So, there is a chance to work, but not ok yet. - so, the second thing I may need is the speed with which the user slides the finger around the screen. So basically something like this:

rotationAngle += angleBetweenTouches+speed

Speed, or velocity as I saw it named is the problem in my case, considering that I don't move only on X or Y but around the circle. I have no idea how to calculate it. I saw VelocityTracker on Android help but I don't know how it could help.

So to recap: I need to be able to rotate the image as the user moves the finger around the image border. Even simpler, when the user stops sliding, the pixel of the image that was below the finger on slide start should be the same when the slide stops.

Any help is greatly appreciated. Thank you

like image 203
Alin Avatar asked Apr 28 '11 07:04

Alin


2 Answers

I had the same task but for a different purpose with angles. i hope this link might give you some clue http://www.andengine.org/forums/post9226.html#p9226

For time difference did you try getting getTime() from Date class between two different clicks?

Hope this give you some idea.

like image 60
Jana Avatar answered Oct 18 '22 08:10

Jana


I suspect that given "Even simpler, when the user stops sliding, the pixel of the image that was below the finger on slide start should be the same when the slide stops. ", what you might be better off with is absolute rotations rather than relative ones.

Incrementing the rotation with each event may accumulate errors that lead to the bitmap not appearing to "stick" to the finger.

If you just keep track of the initial event and the last event, then always transform from the initial view to that appropriate for the last event your code will be simpler (avoiding speed), and behave better.

like image 30
Keith Avatar answered Oct 18 '22 07:10

Keith