0

好的,这里是代码。我想要它做什么它翻译屏幕跟随对象l2rArrow。确实如此,但 l2rArrow 仍然在屏幕上飞行。即使我将平移设置为 l2rArrow 的 x,y,当 l2rArrow 应该保持死点时,它最终仍然会跑出屏幕。

当我将 camX 乘以 25 时,它似乎稍微好一点,但只是稍微好一点。箭头仍然飞出屏幕。

public void Render(GL10 gl, float deltaTime) {

    //Log.d("Game", "Render");
    gl.glViewport(0, 0, Screen.SCREEN_WIDTH, Screen.SCREEN_HEIGHT);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrthof(0, Screen.WIDTH_SCALE, 0, Screen.HEIGHT_SCALE, 1, -1);


    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();

    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);


    gl.glEnable(GL10.GL_TEXTURE_2D);
    MeshRect mRect; // this holds all of our rectangles



    // draw the sky/backdrop************************************************************
    texture.bind(gl, Texture.TEXTURE_SKY1);
    mRect = new MeshRect(Sky.getMyRect());
    mRect.Draw(GL10.GL_TRIANGLES, gl);

    //draw clouds***********************************************************************
    clouds.Update();
    Vector<Cloud> vClouds = clouds.getClouds();
    for (int i = 0; i < vClouds.size(); i++){
        vClouds.get(i).Update(deltaTime);

        texture.bind(gl, Texture.TEXTURE_CLOUDS1);
        mRect = new MeshRect(vClouds.get(i).getMyRect());
        mRect.Draw(GL10.GL_TRIANGLES, gl);

        if (vClouds.get(i).hasRunCourse()){
            clouds.RemoveCloud(vClouds.get(i).getID());
        }
    }
    // draw arrows and applied power ups***********************************************************************
    boolean hasTranslated = false;
    if (!l2rArrow.isFired) {
        gl.glPushMatrix(); //save our state

            gl.glTranslatef(l2rArrow.GetToOriginX, l2rArrow.GetToOriginY, 0f); // translate to origin 0,0,0
            gl.glRotatef(LBow.getAngle(), 0f, 0f, 1f); //rotate
            gl.glTranslatef(-l2rArrow.GetToOriginX, -l2rArrow.GetToOriginY, 0f); // translate to where we want to draw it

            texture.bind(gl, Texture.TEXTURE_L2RARROW);
            mRect = new MeshRect(l2rArrow.getMyRect());
            mRect.Draw(GL10.GL_TRIANGLES, gl);

        gl.glPopMatrix(); //return to saved state

    } else {
        l2rArrow.Update(deltaTime);
        texture.bind(gl, Texture.TEXTURE_L2RARROW);
        mRect = new MeshRect(l2rArrow.getMyRect());
        mRect.Draw(GL10.GL_TRIANGLES, gl);



        hasTranslated = true;
        gl.glPushMatrix();
            float camX = (float) ((l2rArrow.startX - l2rArrow.PosX));
            camX = camX * 25;
            float camY = (float) ((l2rArrow.startY - l2rArrow.PosY));
            if (camY > 0) { camY = 0;}

            gl.glTranslatef(camX, camY,0f);


        if (l2rArrow.hasCollided) {
            //run collision code for detecting a hit and ending turn
        }
    }
    /**=======================================================================================================
     * Everything between here should be effected by translate | | gl.gltranslatef(x,y,z) x = Xoffset
     *=======================================================================================================*/


    /**=======================================================================================================
     *  gl.glPopMatrix(); back to origin
     *=======================================================================================================*/

    if (hasTranslated){
        gl.glPopMatrix();
    }

    //finish drawing other non translated 

}
4

2 回答 2

1

我没有看到 gl.glLoadIdentity()for GL_MODELVIEW。您确定要从单位矩阵开始模型视图吗?如果不是,翻译将继续从前一帧连接。我也没有看到gl.glMatrixMode切换到的调用GL_MODELVIEW,在这种情况下,所有矩阵变换都将进入投影矩阵。

于 2012-08-05T04:30:27.593 回答
0

这是我的操作顺序,需要看起来像这样

    boolean hasTranslated = false;
    if (!l2rArrow.isFired) {
        gl.glPushMatrix(); //save our state

            gl.glTranslatef(l2rArrow.GetToOriginX, l2rArrow.GetToOriginY, 0f); // translate to origin 0,0,0
            gl.glRotatef(LBow.getAngle(), 0f, 0f, 1f); //rotate
            gl.glTranslatef(-l2rArrow.GetToOriginX, -l2rArrow.GetToOriginY, 0f); // translate to where we want to draw it

            texture.bind(gl, Texture.TEXTURE_L2RARROW);
            mRect = new MeshRect(l2rArrow.getMyRect());
            mRect.Draw(GL10.GL_TRIANGLES, gl);

        gl.glPopMatrix(); //return to saved state

    } else {
        l2rArrow.Update(deltaTime);

        hasTranslated = true;
        gl.glPushMatrix();
        gl.glLoadIdentity();
            float camX = (float) ((l2rArrow.startX - l2rArrow.PosX));
            //Log.d("cam X =" + Float.toString(camX),"Y=");
            float camY = (float) ((l2rArrow.startY - l2rArrow.PosY));
            if (camY > 0) { camY = 0;}
            if (camX < -4800) { camX = -4800;}

            gl.glTranslatef(camX, camY,0f);

            texture.bind(gl, Texture.TEXTURE_L2RARROW);
            mRect = new MeshRect(l2rArrow.getMyRect());
            //Log.d("arrow X =" + Float.toString(l2rArrow.getMyRect().BottomLeftCorner.x),"Y=");
            mRect.Draw(GL10.GL_TRIANGLES, gl);


        if (l2rArrow.hasCollided) {
            //run collision code for detecting a hit and ending turn
        }
    }
于 2012-08-05T07:48:57.390 回答