Search code examples
androidopengl-esopengl-es-1.1

Moving an object along with mouse


i have drawn an square,now i want to move it along the plane following the mouse pointer.I am using open GL es 1.0.I tried to get difference in motion position with the below code:

@Override public boolean onTouchEvent(MotionEvent e) {
    float x = e.getX();
    float y = e.getY();
    switch (e.getAction()) {
    case MotionEvent.ACTION_MOVE:
         mRenderer.dx = x - mPreviousX;
         mRenderer.dy = y - mPreviousY;
        requestRender();
    }
    mPreviousX = x;
    mPreviousY = y;
    return true;
}

and then i am translating the object by using gtranslatef function with dx and dy values.

gl.glTranslatef(dx, dy,0);

but for a little movement in mouse pointer.there is large displacement in object position.

How can i move object along with mouse?


Solution

  • Pointer coordinates usually are in screen physical dimensions, i.e. pixels. Your OpenGL coordinates depend on the projection and modelview matrices you apply.

    What you must do is doing a backtransform of screen/viewport coordinates into your object or world space. This is done by reversing the transformation pipeline. Since matrix multiplication is a linear operation, you can not only put absolute values through it, it works as well for differentials.

    I'd tell you more, but I need to say your existing program structure to give you sensible advice how to extend or change it. Please post it on http://pastebin.com or similar.