Game Development Reference
In-Depth Information
If the dot product value is very close to 1, then our two direction vectors are close to
being parallel, since cos(0°) = 1 , and we've detected a swipe in the required direction.
Similarly, if the dot product is close to -1, we've detected a swipe in the opposite
direction, as cos(180°) = -1 .
Detecting a pinch gesture
Pinch gestures can only be used on devices featuring multi-touch displays, since
they require two simultaneous touch points. A pinch gesture is often used to allow
zooming in and out to occur and is performed by placing two fingers on the screen
and then moving them together or apart. This is most easily achieved using the
thumb and index finger.
Detecting a pinch gesture in code is actually quite simple. As soon as we have
detected two touch points on the screen, we calculate a vector from one point to the
other and find the distance of this vector. This is stored as the initial distance and will
represent no zooming.
As the user moves their fingers around the screen, we just keep calculating the new
distance between the two touch points, and then divide this distance by the original
distance. The end result of this calculation is a zoom scale factor. If the user moves
their fingers together, the zoom factor will be less than one; if they move them apart,
the zoom value will be greater than one.
The pinch gesture is complete once the user removes at least one finger from
the display.
Detecting accelerometer input
The final input method we will be considering in this chapter is the accelerometer ,
which allows us to detect the orientation that the user is currently holding the
device at. An accelerometer is a sensor that can measure the forces applied to
a device, be they static forces such as gravity, or dynamic forces generated by
waving the device around.
Most devices will have three accelerometers aligned perpendicularly to each
other, as shown in the following diagram. This configuration allows us to discover
exactly how the user is holding the device at any time and so provides a method of
controlling our game.
 
Search WWH ::




Custom Search