Game Development Reference
In-Depth Information
Recognizing gesture inputs
The arrival of the touch screen to mobile devices brought with it a new set of
terminology related to making inputs to our programs. For years we have been
using a mouse, clicking and dragging to interact with programs, and now with touch
screens we have quickly become comfortable with the idea of swiping and pinching.
These methods of interaction have become known as gestures and users have
become so used to them now that if your application doesn't respond as they expect,
they may get quickly frustrated with your application.
Unfortunately, Marmalade does not provide any support for detecting these
gestures, so instead we have to code for them ourselves. The following sections
aim to provide some guidance on how to easily detect both swipes and pinches.
Detecting a swipe gesture
A swipe occurs when the user touches the screen and then slides that touch point
quickly across the screen before releasing the screen.
To detect a swipe we must therefore first keep track of the screen coordinates where
the user touched the screen and the time at which this occurred. When this touch
event comes to an end due to the user releasing the screen, we first check the time it
lasted for. If the length of time is not too long (say less than a quarter of a second), we
check the distance between the start and end points. If this distance is large enough
(perhaps a hundred pixels in length, or a fraction of the screen display size), then we
have detected a swipe.
Often we only want to respond to a swipe if it is in a certain direction. We can
determine this by using the dot product, the formula for which is shown in the
following diagram:
The dot product is calculated by multiplying the x and y components of the two
vectors together and summing the results, or by multiplying the length of the two
vectors together and then multiplying by the cosine of the angle between the two
vectors.
To check if the user's swipe lies in a particular direction, we first make the direction
of the swipe into a unit vector, then dot product this with a unit vector in the desired
swipe direction. By using unit vectors we reduce the formula on the left-hand side of
the previous diagram to just the cosine of the angle between the vectors, so it is now
very simple to see if our swipe lies along the desired direction.
 
Search WWH ::




Custom Search