我正在开发一个Android应用程序,在这个应用程序中我遇到了关于3D Cube旋转的问题。我想根据fingure touch旋转我的立方体。
直到现在我已经尝试过这个不能轮换的代码 -
//Rotate around the axis based on the rotation matrix (rotation, x, y, z)
gl.glRotatef(PhotoCube3DLWPActivity.xrot, 1.0f, 0.0f, 0.0f); //X
gl.glRotatef(PhotoCube3DLWPActivity.yrot, 0.0f, 1.0f, 0.0f); //Y
gl.glRotatef(PhotoCube3DLWPActivity.zrot, 0.0f, 0.0f, 1.0f); //Z
cube.draw(gl); //Draw the Cube
//Change rotation factors (nice rotation)
PhotoCube3DLWPActivity.xrot +=0.3f;;
PhotoCube3DLWPActivity.yrot +=0.2f;
PhotoCube3DLWPActivity.zrot +=0.4f;
其中xrot,yrot和zrot是PhotoCube3DLWPActivity类的静态变量,其值分别为0.3f,0.2f和0.4f。
我正在使用的Touch事件 -
@Override
public void onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN :
Log.i("check", "Hey action down");
downX = event.getX();
downY = event.getY();
downTime = System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE :
// handle in between rotation
break;
case MotionEvent.ACTION_UP :
Log.i("check", "Hey action up");
diffX = event.getX() - downX;
diffY = event.getY() - downY;
diffTime = System.currentTimeMillis() - downTime;
diffTouch = (float) Math.sqrt(diffX * diffX + diffY * diffY);
diffSpeed = diffTouch / diffTime * 1.0f;
diffAngle = (float) Math.atan(diffY / diffX);
xrot += diffSpeed * Math.sin(diffAngle);
yrot += diffSpeed * Math.cos(diffAngle);
break;
}
}//touch end