简体   繁体   English

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

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

I have an island random generation app for Android, developed with Opengl ES 2.0 and i am trying to implement an FPS-like camera. 我有一个使用Opengl ES 2.0开发的Android孤岛随机生成应用程序,我正在尝试实现类似FPS的相机。 The main problem is to translate the view according to the camera rotation.This is how i rotate and translate the view: 主要问题是根据相机旋转来平移视图。这就是我旋转和平移视图的方式:

    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);

Where mTemporaryMatrix stores the rotation of the camera on a given frame based on the old rotation and mAngleX that is a touch input. 其中mTemporaryMatrix基于旧旋转和作为触摸输入的mAngleX在给定帧上存储摄像机的旋转。 Same for mCurrentTranslation and xrot , yrot . mCurrentTranslationxrotyrot So when i move around after a rotation the movement keeps being according to the world coordinates. 因此,当我旋转后四处走动时,移动会一直保持与世界坐标一致。 My xrot,yrot update equation are the following: 我的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])));
  }

I am quite sure the problem relies on them. 我完全确定问题取决于他们。 I transpose and invert the rotation matrix and then I take arccosine the sixth element of the matrix that should be the rotation on the Y-axis. 我转置并反转旋转矩阵,然后将反余弦作为该矩阵的第六个元素作为Y轴上的旋转。 Does someone see a bug somewhere in the update function? 有人在更新功能的某处看到错误吗? But mostly there is a standard way to do it? 但是大多数情况下有标准的方法吗?

Found the bug. 发现了错误。 First of all I keep the cumulative amount of mAngleX I rotate in a float variable called angle, and then simply: 首先,我将旋转的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));
}

Works like a charme 像魅力一样

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

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