Take the 2-minute tour ×

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;
    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?

share|improve this question
are you getting you finger up and pressing somewhere else on the screen? –  SteveL Feb 24 '12 at 11:00
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
You can use gluUnproject function for converting your screen co-ordinates to object co-ordinates –  CV Enthusiast Feb 24 '12 at 11:47
@SatishKumar: Yes, I know about this. I didn't want to tell OP because I have a hunch, that OP might be abusing the matrix stack. First I wanted to see the code, to be able to make corrections on it, before coaxing gluUnproject into it. –  datenwolf Feb 24 '12 at 11:48
I am sure you do. I thought, it might be easy for him to learn himself if he looks into the specific function. :) –  CV Enthusiast Feb 24 '12 at 12:34
How to use gluUnproject ? –  Abhishek Feb 27 '12 at 11:09
@Abhishek: Please post your code at pastebin.com and I'll explain to you, where and how to integrate it. –  datenwolf Feb 27 '12 at 11:57
show 5 more comments

Your Answer


