I'm trying to allow the user to pan a 2D scene by using the touch screen. I have tried several methods including translating the projection matrix, translating the view matrix and haven't found anything that will work. With these methods, the screen either pans extremely fast, or doesn't pan the amount that the user has moved with their finger. The closest I've got is the following:
public void onDrawFrame(GL10 glUnused) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
// Projection matrix setup
Matrix.frustumM(projectionMatrix, 0, -mSurfaceRatio * zoomFactor, // Left
mSurfaceRatio * zoomFactor, // Right
-1 * zoomFactor, // Bottom
zoomFactor, // Top
3, // Near
7); // Far
// panning
Matrix.translateM(projectionMatrix, 0, mPositionX, mPositionY, 0);
for (IDrawable drawable : mDrawables) {
drawable.Draw(maPositionHandle, maTextureHandle, mvpMatrixHandle, mvpMatrix, viewMatrix, projectionMatrix);
}
}
Gesture Detector:
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
boolean panned = false;
float currentZoomFactor = renderer.getZoomFactor();
if (distanceX != 0) {
renderer.mPositionX += -1f * (distanceX * (3.1f /renderer.mSurfaceWidth) * currentZoomFactor * renderer.mSurfaceRatio);
panned = true;
}
if (distanceY != 0) {
renderer.mPositionY += (distanceY * (3.8f /renderer.mSurfaceHeight) * currentZoomFactor * renderer.mSurfaceRatio);
panned = true;
}
if (panned)
{
surfaceView.requestRender();
}
return panned;
}
Code for calculating the surface:
public void onSurfaceChanged(GL10 glUnused, int width, int height)
{
GLES20.glViewport(0, 0, width, height);
mSurfaceRatio = (float)width / height;
mSurfaceWidth = (float)width;
mSurfaceHeight = (float)height;
}
This code will pan somewhat correctly, but I just guessed at the 3.1f and 3.8f to make it look right. By somewhat I mean that when the device (Xoom tablet) is held long-wise the area pans correctly in the x direction, but in the y direction, the scene will lag behind where the user's finger is moving.
Could someone help me with this? Thanks!