Graphics Programs Reference
In-Depth Information
been used with UI and Renderer. Understanding its usage is necessary to understand
the concepts for using UI with OpenGL rendering.
Using Touch for Rotation
For the Tank Fence game, we implement rotation using the screen (or optionally us-
ing sensors). We won't use buttons for rotation. In this topic, you learn to use the
screen using screen touch to update the rotate matrix associated with an object.
The rotate matrix associated with an object requires the desired angle of rotation (in
degrees) to rotate the object by that angle about a particular axis.
If you want to use the screen to rotate an object about the axis perpendicular to it (the
screen) so that the angle of rotation is proportional to the horizontal distance moved
by a finger across the screen, take the ratio of the total horizontal distance moved to
the width of the screen. For example, we can create a class that implements the inter-
face OnTouchListener , and, inside the implemented method onTouch , we can
use the code shown in Listing 2-12 to find the horizontal distance moved.
Listing
2-12. TOUCH ROTATION/src/com/apress/android/touchrotation/
Main.java
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_touchedX = event.getX();
} else if (event.getAction() ==
MotionEvent.ACTION_MOVE) {
float touchedX = event.getX();
float dx = Math.abs(_touchedX - touchedX);
We get the device width by accessing the display metrics members, and we obtain
the ratio of dx to the device width. We then convert this ratio into degrees, which
can be used by the rotate matrix to rotate the object. This concept is utilized by the
TOUCH ROTATION application ( Figure 2-16 ), in the source code for this chapter.
This application (via class Main ) also takes into account the direction (left or right)
in which the finger moves across the screen so as to handle clockwise and anticlock-
wise rotations separately.
To create such an application, we need two classes:
 
 
 
Search WWH ::




Custom Search