I’ve created 3 Java classes.
- one that has a glsurfaceview object and this calls the renderer class.
- this is the renderer class and this calls the cube class.
- this is the cube class.
If I run the app then the screen shows a rotating cube (did rotation in the rendering class) which is fine. But I want to control the direction of rotation of the cube and for that I’ve set 2 buttons. This is where I need help because I don’t know to to make the buttons control the movement of the cube. I’m new to Android so if you could leave some code for me to examine then that would be just great.
Your Activity class (or your class that extends Activity) should look like this:
Then in your renderer:
Basically, you use
glRotatefto rotate the cube just before you draw it. Use -ve vales for either the angle parameter (the first) or the x,y,z amount parameters to rotate in the opposite direction. Use method calls to theRendererto communicate with it and update the scene. Use this approach with caution as the Renderer thread and Main/UI thread (from where the button call is made) can have synchronisation issuesTo make a button call the
changeRotationDirectionmethod, simply addandroid:onClick="changeRotationDirection"into the XML layout (of any view. Doesn’t have to be just a button view). Any button methods declared in the XML layout have to be of the formpublic void [methodname](View [paramname])and have to be in the Activity class from where the button is pressedFor more advanced touch control, check as Erik suggested and also check out
OnTouchListeners(Note: if you’re using openGL-ES 2.0 or above (android 2.2+) then use
GLES20.glRotatef())