Saturday, 25 May 2013

Android: Converting from pixel to OpenGL Location

Android: Converting from pixel to OpenGL Location

I have a simple OpenGL app that draws a circle, I want to move the circle based on where a person touches the screen. The problem is, OpenGL sees the height and width as -1 to 1, whereas Android sees it based on pixel (I.E. 725). I convert it using the following...
@Override
public boolean onTouch(View view, MotionEvent event) {
  float moveX = ((event.getX())-(size.x/2))/(size.x/2);
  Log.d("Debug", "Y "+ event.getY());
  Log.d("Debug", "Height "+ size.y);
  float moveY = ((size.y/2)-(event.getY()))/(size.y/2);
  Log.d("Debug", "MoveY "+ moveY);
  OpenGLNative.setMoveX(moveX);
  OpenGLNative.setMoveY(moveY);
  return true;
}
Problem is it doesn't quite line up in the emulator, for example if I click near the bottom the circle looks like follows...

The console output looks as follows....
05-25 13:58:48.859: D/Debug(14185): Y 776.0
05-25 13:58:48.859: D/Debug(14185): Height 856
05-25 13:58:48.859: D/Debug(14185): MoveY -0.8130841
Anyone know how I can improve this so the middle of the circle lines up more closely to where I select? Why does the screen seem to return a higher value than what is displayed in the emulator?

No comments:

Post a Comment