Android openGL ES 2.0 scene scrolling

前端 未结 1 508
悲&欢浪女
悲&欢浪女 2021-01-28 21:08

I\'m working on a simple game for android using openGL es 2.0. Game will be in 2D, something like old supermario, where player can move left-right/up-down but no depth.

相关标签:
1条回答
  • 2021-01-28 21:45

    I've solved this by modifying parameters for Matrix.frustumM so they represent FOV. Than when you know angle from middle to side, you can calculate offset with tangens function.

     GLES20.glViewport(0, 0, width, height);
    
        float ratio = (float) width/height;
        float near = 1.0f;
        float far = 150.0f;
        float fov = 80;
        float top = (float) (Math.tan(fov * Math.PI / 360.0f) * near);
        float bottom = -top;
        float left = ratio * bottom;
        float right = ratio * top;
    
        offsetY = (float) (Math.tan(Math.PI/180*fov/2) * Math.abs(cameraAway));
        offsetX = offsetY * ratio; //because we know ratio between x and y
    
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
    

    You can also use Matrix.perspectivM(), but it is only avaliable for api 14+.

    0 讨论(0)
提交回复
热议问题