【发布时间】:2020-08-14 00:37:41
【问题描述】:
我正在尝试使用触摸(滚动)围绕场景旋转相机:
public class SurfaceView extends GLSurfaceView
implements GestureDetector.OnGestureListener {
SceneRenderer renderer;
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
renderer.setMotion(distanceX, distanceY); // pass movement to render
return true;
}
}
相机在渲染中移动如下:
public class SceneRenderer implements GLSurfaceView.Renderer {
private float[] viewMatrix = new float[16];
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Matrix.setLookAtM(viewMatrix, 0, 0f, 0f, 3f,
0f, 0f, 0f, 0f, 1.0f, 0.0f);
}
...
// movement of camera
public synchronized void setMotion(float xDistance, float yDistance) {
Matrix.rotateM(viewMatrix, 0, -xDistance * 0.1f, 0, 1, 0);
Matrix.rotateM(viewMatrix, 0, -yDistance * 0.1f, 1, 0, 0);
}
}
这在开始时效果很好。但随后相机开始不按预期旋转。如何使用滚动使相机相对于场景(对象)的中心正确移动?也许有人解决了类似的问题?
感谢您的任何回答/评论!
解决办法:
private float kx = 0f;
private float ky = 0f;
private float radius = 3.0f;
private float x, y, z = 0f;
...
public synchronized void setMotion(float xDistance, float yDistance) {
kx = kx + xDistance * 0.001f;
x = (float) (radius * Math.sin(kx));
z = (float) (radius * Math.cos(kx));
ky = ky + yDistance * 0.001f;
y = (float) (radius * Math.sin(ky));
Matrix.setLookAtM(viewMatrix, 0, x, -y, z, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
}
但是使用这种方法,相机在沿 Y 轴旋转时会远离场景。
【问题讨论】:
标签: android matrix opengl-es opengl-es-2.0 touch-event