繁体   English   中英

Opengl ES 2.0 Android-根据旋转来平移相机

[英]Opengl ES 2.0 Android - Translate camera according to rotation

我有一个使用Opengl ES 2.0开发的Android孤岛随机生成应用程序,我正在尝试实现类似FPS的相机。 主要问题是根据相机旋转来平移视图。这就是我旋转和平移视图的方式:

    Matrix.setIdentityM(mCurrentRotation, 0);
    Matrix.rotateM(mCurrentRotation, 0, mAngleX, 0.0f,1.0f, 0.0f);
    Matrix.setIdentityM(mCurrentTranslation, 0);
    Matrix.translateM(mCurrentTranslation,0,xrot,-7.0f,yrot);
    mAngleX = 0.0f;

    Matrix.multiplyMM(mTemporaryMatrix, 0, mCurrentRotation, 0, mAccumulatedRotation, 0);

    System.arraycopy(mTemporaryMatrix, 0, mAccumulatedRotation, 0, 16);
          .
          .
          .
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
    Matrix.multiplyMM(mMVPMatrix, 0, mCurrentTranslation, 0, mMVPMatrix, 0);
    Matrix.multiplyMM(mMVPMatrix, 0, mTemporaryMatrix, 0, mMVPMatrix, 0);
    // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
    // (which now contains model * view * projection).
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

其中mTemporaryMatrix基于旧旋转和作为触摸输入的mAngleX在给定帧上存储摄像机的旋转。 mCurrentTranslationxrotyrot 因此,当我旋转后四处走动时,移动会一直保持与世界坐标一致。 我的xrot,yrot更新公式如下:

  void setXYrot(float x, float y){
      Matrix.transposeM(temp,0,mTemporaryMatrix,0);
      Matrix.invertM(temp,0,temp,0);
      xrot-=(x*Math.sin(Math.acos(temp[5])));
      yrot+=(y*Math.cos(Math.acos(temp[5])));
  }

我完全确定问题取决于他们。 我转置并反转旋转矩阵,然后将反余弦作为该矩阵的第六个元素作为Y轴上的旋转。 有人在更新功能的某处看到错误吗? 但是大多数情况下有标准的方法吗?

发现了错误。 首先,我将旋转的mAngleX的累积量保持在一个称为angle的float变量中,然后简单地进行以下操作:

void setXYrot(float x, float y){

    xrot-=(y*Math.sin((angle*Math.PI)/180f))+(x*Math.cos((angle*Math.PI)/180f));
    yrot+=(y*Math.cos((a*Math.PI)/180f))-(x*Math.sin((angle*Math.PI)/180f));
}

像魅力一样

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM