Tracking finger move in order to rotate a triangle : tracking is not perfect

Posted by Laurent BERNABE on Stack Overflow See other posts from Stack Overflow or by Laurent BERNABE
Published on 2012-11-13T10:40:50Z Indexed on 2012/11/13 11:00 UTC
Read the original article Hit count: 388

Filed under:
|

I've written a custom view, with the OpenGL_1 technology, in order to let user rotate a red triangle just by dragging it along x axis. (Will give a rotation around Y axis). It works, but there is a bit of latency when dragging from one direction to the other (without releasing the mouse/finger). So it seems that my code is not yet "goal perfect". (I am convinced that no code is perfect in itself).

I thought of using a quaternion, but maybe it won't be so usefull : must I really use a Quaternion (or a kind of Matrix) ?

I've designed application for Android 4.0.3, but it could fit into Android api 3 (Android 1.5) as well (at least, I think it could).

So here is my main layout :

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.laurent_bernabe.android.triangletournant3d.MyOpenGLView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        /> 

</LinearLayout>

Here is my main activity :

MainActivity.java

package com.laurent_bernabe.android.triangletournant3d;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

And finally, my OpenGL view

MyOpenGLView.java

package com.laurent_bernabe.android.triangletournant3d;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Point;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class MyOpenGLView extends GLSurfaceView implements Renderer {

    public MyOpenGLView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setRenderer(this);
    }

    public MyOpenGLView(Context context) {
        this(context, null);
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int actionMasked = event.getActionMasked();
        switch(actionMasked){
        case MotionEvent.ACTION_DOWN:
            savedClickLocation = new Point((int) event.getX(), (int) event.getY()); 
            break;
        case MotionEvent.ACTION_UP:
            savedClickLocation = null;
            break;
        case MotionEvent.ACTION_MOVE:
            Point newClickLocation = new Point((int) event.getX(), (int) event.getY());
            int dx = newClickLocation.x - savedClickLocation.x;
            angle += Math.toRadians(dx);
            break;
        }
        return true;
    }



    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        GLU.gluLookAt(gl,
                0f, 0f, 5f,
                0f, 0f, 0f,
                0f, 1f, 0f
        );

        gl.glRotatef(angle, 0f, 1f, 0f);
        gl.glColor4f(1f, 0f, 0f, 0f);
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, triangleCoordsBuff);
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        GLU.gluPerspective(gl, 60f, (float) width / height, 0.1f, 10f);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glClearDepthf(1.0f);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        buildTriangleCoordsBuffer();
    }

    private void buildTriangleCoordsBuffer() {
        ByteBuffer buffer = ByteBuffer.allocateDirect(4*triangleCoords.length);
        buffer.order(ByteOrder.nativeOrder());
        triangleCoordsBuff = buffer.asFloatBuffer();
        triangleCoordsBuff.put(triangleCoords);
        triangleCoordsBuff.rewind();
    }

    private float [] triangleCoords = {-1f, -1f,
                                        +1f, -1f,
                                        +1f, +1f};

    private FloatBuffer triangleCoordsBuff;
    private float angle = 0f;

    private Point savedClickLocation;

}

I don't think I really have to give you my manifest file. But I can if you think it is necessary.

I've just tested on Emulator, not on real device.

So, how can improve the reactivity ?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about android

Related posts about opengl-es-1.1