ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-10-13 02:29:03  阅读:340  来源: 互联网

标签:android opengl-es rubiks-cube


我一直在尝试为Android做魔方.我有一个关于轮换的问题.我想旋转一个外观正确的图形.这意味着如果用户触摸屏,并且在将手指向右移动之后,图形将从观察点的一侧向右旋转.但是当我旋转一些时,图形开始朝不正确的方向移动.我了解,这取决于轴是否改变了他们的状况.但是我尝试使用逆模型矩阵来获取必要的坐标,但尚未得到结果.有人可以在鼠标或触摸屏的帮助下给我示例或视觉上正确旋转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 rotation.您将在互联网上的Java周围找到大量资源.

标签:android,opengl-es,rubiks-cube
来源: https://codeday.me/bug/20191013/1904622.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有