0

我的应用程序是在 android 上使用 openGL ES 2.0 制作的,我在对象位置更新方面遇到了相当严重的问题。我的游戏循环是这样的,绘制所有对象 --> 更新游戏对象位置 --> 迭代。但是,我有两种不同的位置更新:一种是基于计算的,当一个值足够低或足够高时,它会改变方向。另一种是基于触摸的,当用户在任何位置触摸和/或滑动屏幕时,对象将跟随。

现在来解决问题。当用户触摸屏幕和/或滑动时,应该只响应触摸的对象也会获得基于计算的对象的不断变化的 x 值,我不知道为什么会这样,因为它们使用完全不同的位置变量.

下面的代码是 GLsurfaceView 的摘录,显示了触摸位置值是如何传递到 GL 渲染器的

    public boolean onTouchEvent(MotionEvent e){
    if(e != null){
        if(e.getAction() == MotionEvent.ACTION_DOWN){
            x = e.getX();
            y = e.getY();
            if(_renderer != null){
                queueEvent(new Runnable(){

                    @Override
                    public void run() {

                        _renderer.touchInput(x, y);
                    }

                });
                return true;
            }
        }
        if(e.getAction() == MotionEvent.ACTION_MOVE){
            x = e.getX();
            y = e.getY();
            if(_renderer != null){
                queueEvent(new Runnable(){

                    @Override
                    public void run() {

                        _renderer.touchInput(x, y);

                    }

                });


            }

        }

下面的代码是 GL 渲染器的摘录,它显示了触摸值如何进入渲染器并转换为世界坐标。

public void touchInput(float x, float y){
    y = (float)_view[3] - y;
    GLU.gluUnProject(x, y, -1.5f, _ModelMatrix, 0, _ProjectionMatrix, 0, _view, 0, touch_to_world_coords, 0);

    _world_x = touch_to_world_coords[0] * touch_to_world_coords[3];
    _world_y = touch_to_world_coords[1] * touch_to_world_coords[3];
}

接下来是计算非触摸对象的新位置的代码

public void updateObjectCoords(){
    _test_x += 0.05f;
            _test_y += 0.05f;
}

最后是来自渲染器的 onDrawFrame,我将其用作游戏循环。

public void onDrawFrame(GL10 glContext) {
    //Tell OpenGL to use this program when rendering.
    GLES20.glUseProgram(_current_shaderProgramHandle);

    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

    //Sets the active texture unit to texture unit 0
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    //Bind the texture to current unit
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, _TextureDataHandle);

    //Tells texture uniform sampler to use this texture in the shader by binding it to unit 0
    GLES20.glUniform1i(_TextureUniformHandle, 0);

    //Draw square, gets coordinates from gluUnproject method
    Matrix.setIdentityM(_ModelMatrix, 0);
    Matrix.translateM(_ModelMatrix, 0, _world_x, _world_y, -1.0f);
    drawSquare();

    //Draw square, gets coordinates from updateObjectCoords method
    Matrix.setIdentityM(_ModelMatrix, 0);
    Matrix.translateM(_ModelMatrix, 0, _test_x, _test_y, -1.0f);
    drawSquare();


    //GLES20.glUseProgram(_point_shaderProgramHandle);
    //drawLight();
    updateObjectCoords();

}

编辑:注意到我的错误解释,所以我添加了这个,以便更清楚时会发生什么:)

(当立方体 1 获得触摸输入并且立方体 2 从 updateObjectCoords 获得坐标时): 应用程序启动:一切按预期工作,立方体 2(updateObjectCoords one)按预期移动。用户触摸屏幕:立方体 2 继续按预期移动,立方体 1(触摸控制)坐标数据似乎与立方体 2 混合,因为它的移动看起来是立方体 2 的两倍大并且朝向相同的方向,您仍然可以稍微操作它但是它不是很响应。

(Cube2 是静态的,但是 updateObjectCoords 仍然处于活动状态,并在每一帧上调用以更新 _test_x 和 _test_y 的值): 应用程序启动:一切如预期用户触摸屏幕:一切正常

因此,似乎正是cube2的不断重新定位干扰了cube1的定位。

希望这让事情变得更清楚!:)

为什么要绘制的一个对象的坐标会影响所有其他 translateM 的坐标?为什么所有其他对象都不受触摸坐标的影响?

4

1 回答 1

0

我通过将 cube1 的绘图与 cube2 切换来解决此问题。这似乎解决了一切。假设最后绘制触摸控制对象将结束干扰:D

于 2012-05-15T16:16:08.677 回答