Question

I am trying to take a touch event and move a shape to wherever the touch event moves.

public boolean onTouchEvent(MotionEvent e) {
    mRenderer.setPosition(e.getX(), e.getY());

    return true;
}

The problem is the coordinates I get from the MotionEvent are the screen location in pixels, not the normalized coordinates [-1, 1]. How do I translate screen coordinates to the normalized coordinates? Thanks in advance!

Was it helpful?

Solution

    float x = e.getX();
    float y = e.getY();
    float screenWidth;
    float screenHeight;

    float sceneX = (x/screenWidth)*2.0f - 1.0f;
    float sceneY = (y/screenHeight)*-2.0f + 1.0f; //if bottom is at -1. Otherwise same as X

To add a bit more general code:

 /*
 Source and target represent the 2 coordinate systems you want to translate points between.
 For this question the source is some UI view in which top left corner is at (0,0) and bottom right is at (screenWidth, screenHeight)
 and destination is an openGL buffer where the parameters are the same as put in "glOrtho", in common cases (-1,1) and (1,-1).
 */
float sourceTopLeftX;
float sourceTopLeftY;
float sourceBottomRightX;
float sourceBottomRightY;

float targetTopLeftX;
float targetTopLeftY;
float targetBottomRightX;
float targetBottomRightY;

//the point you want to translate to another system
    float inputX;
    float inputY;
//result
    float outputX;
    float outputY;

outputX = targetTopLeftX + ((inputX - sourceTopLeftX) / (sourceBottomRightX-sourceTopLeftX))*(targetBottomRightX-targetTopLeftX);
outputY = targetTopLeftY + ((inputY - sourceTopLeftY) / (sourceBottomRightY-sourceTopLeftY))*(targetBottomRightY-targetTopLeftY);

With this method you can translate any point between any N-dimensional orthogonal systems (for 3D just add the same for Z as is for X and Y). In this example I used the border coordinates of the view but you can use ANY 2 points in the scene, for instance this method will work all the same if using screen center and top-right corner. The only limitaions are sourceTopLeftX != sourceBottomRightX for every dimension

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top