x


iPhone - How to calculate a decent touch swipe speed?

I'm tracking the touch positions in an update function and am trying to take a measure of the speed of a swipe being performed. At the moment i'm only taking the last frame's touch position change and using this to extrapolate out using Time.deltaTime to get a rough speed of that motion.

I guess i should really be averaging it more than just taking the last frame but i figured it'd at least be ok.

What i'm seeing though seems to be some frames coming through with 0 movement even though the motion is relatively constant speed.

What's going on? Is it because the touches are updated at a certain frequency and the game (at the moment in the simulator and via unity remote) is getting its update frames at a different frequency causing some updates to have not received new touches?

Is there a better callback i can be using that is just updated on touch updates?

Thanks

more ▼

asked Mar 07 '10 at 03:30 AM

jimbobuk gravatar image

jimbobuk
1.1k 34 42 54

Can you post the code you are using for this?

Mar 24 '10 at 07:23 PM andeeee ♦

did u get the solution?

Jul 15 '10 at 07:15 AM Pria

Questions such as these deserve to receive some form of feedback.. in this case, from the person who posted it.

Sep 29 '10 at 07:49 AM zannghast

I'm sorry, but my development has frozen on this project for most of this year. When i come to start it up again i'll try to reactivate my questions with any solutions i've discovered.

For this particular problem i'm sure that just pushing the touch positions into a queue of say 10-20 frames (or more) will be sufficient to then let you take the delta position between the 20th frame ago and this current frame and use this to get the swipe speed. This should smooth out any of the strange problems i was seeing with motion having occasional 0 movement frames.

Sorry guys, day job too busy! :(

Sep 29 '10 at 08:24 PM jimbobuk
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The method you are using is reasonable up to a point. The problems tend to occur if the frame rate is TOO good. You won't see exact pixel movements of 1 or 2 pixels; they tend to get rounded to 0 or 5 pixels. So a tiny movement over a very small deltaTime can be rounded into a 5-pixel movement. When you divide to calculate the velocity, you end up with an absolutely enormous velocity!

A cheap solution is to ignore deltas of less than 10 pixels. That seems to work fine on all the devices I've tested it on, including retina displays. That would probably work for you as well, especially given that you seem to be interested in swipes, not small drags.

To solve your original problem, you could also ignore frames where the movement was 0. But that depends in part on what you are using the velocity for. In my case I wanted to calculate the velocity only when the drag ended. If you need the velocity recalculated throughout the drag you'll probably have to store the last N delta movements and times, and calculate a running average. You'll have to play with different values of N to see what works for your app.

more ▼

answered Oct 14 '10 at 04:05 PM

Bampf gravatar image

Bampf
5k 8 19 49

some of us - includingme - are really interested in some bits of code here... thanx!!!

Jan 20 '11 at 01:38 PM headkitgames

I have a post with code, and a downloadable project, on my blog: http://www.mindthecube.com/blog/2010/09/adding-iphone-touches-to-unitygui-scrollview In that example I'm dragging around a UnityGUI scrollable area, but the touch and drag detection can be adapted to other things.

Jan 20 '11 at 06:06 PM Bampf

thanx! i will have a look...

Apr 20 '11 at 09:07 AM headkitgames
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2000
x954
x580
x357
x68

asked: Mar 07 '10 at 03:30 AM

Seen: 7341 times

Last Updated: Mar 07 '10 at 03:30 AM