gpt4 book ai didi

android - 使用 OpenGl 和触摸屏功能视觉校正旋转

转载 作者:行者123 更新时间:2023-11-30 04:08:39 26 4
gpt4 key购买 nike

我一直在尝试为 android 做 Cube of Rubik。我有一个关于轮换的问题。我想在视觉上正确地旋转一个图形。这意味着如果用户触摸屏幕并将手指向右移动后,图形会从观察点的一侧向右旋转。但是当我进行一些旋转时,图形开始朝不正确的方向移动。我知道这取决于那个轴正在改变他们的情况。但是我尝试使用逆模型矩阵来获得必要的坐标,但我还没有结果。任何人都可以给我示例或在鼠标或触摸屏的帮助下视觉上正确旋转 3D 图形的链接吗?

   //get vector 3D of touch
Vector3f touchVector = getRubikSystemCoordinates(mTouchX,mTouchY,square.rubikRotationMatrix);
//Get vector 3D of move
Vector3f moveVector = getRubikSystemCoordinates(mMoveX,mMoveY,square.rubikRotationMatrix);
//get direction of motion
float direction = touchVector.substractFrom(moveVector);
//get axis for rotation
Vector3f axis = touchVector.vectorProductTo(moveVector);
//normalize axis
axis.normalize();
//get angle of rotation
float angle = direction.length;
//make identity Quad
Quaternion quad = new Quaternion();
//make rotation quad
quad.makeRotationKvaternion(angle,axis);
//from quad recieve matrix
Matrix4f matrix = quad.toMatrix();
//multiply to current modelview matrix
gl.glMultMatrixf(matrix.returnArray(),0);
//save rotation matrix
square.rotationMatrix = square.rotationMatrix.multiply(matrix);
//save modelView matrix
square.saveModelView(square.initMatrix.returnArray());


// touch coords to current modelView coords
private Vector3f getRubikSystemCoordinates(float x, float y, Matrix4f matrix){
// touch coords to normal coords of screen
Vector2f normalCoords = (new Vector2f(x,y)).toNormalScreenCoordinates(Settings.viewPort[2],Settings.viewPort[3]);
// to sphere coords in 3D
Vector3f sphereVector = new Vector3f(normalCoords.x,normalCoords.y, FloatMath.sqrt(2-normalCoords.x*normalCoords.x-normalCoords.y*normalCoords.y));
//Get inverse matrix from ModelView Matrix
Matrix4f m = matrix.inverseMatrix();
//Get vector for current modelView 3D coords
Vector3f vector = m.multiplyToVector(vector);
// make normalize vector
vector.normalize();
return vector;
}

最佳答案

您正在寻找的是名为 arcball 的旋转。您会在互联网上找到大量有关 Java 的资源。

关于android - 使用 OpenGl 和触摸屏功能视觉校正旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11207805/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com