首页 > 解决方案 > 使用当前坐标调用 glTranslatef 以恒定速度移动对象(OpenGL ES 1.1)

问题描述

我目前一直在关注一本书,详细介绍了 android 上的 OpenGL ES 1.1。在一个示例中,我们一直在屏幕上移动许多不同的纹理对象。当我们使用 glTranslatef() 来移动对象时,我希望我们会使用一个恒定的量(例如,50 以便对象具有相同的速度)。但是,我们所做的是获取对象的当前坐标(不断增加 50)。那不应该使对象更快吗?当坐标为 (5,5) 时,我们在每个轴上移动 5 个像素。当它变为 (55,55) 时,对象将在每个轴上移动 55 个像素。但是,随着时间的推移,我根本没有看到速度的提高。为什么是这样?

            gl.glClearColor(1, 0, 0, 1);
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(0, 320, 0, 480, 1, -1);

            gl.glEnable(GL10.GL_TEXTURE_2D);
            bobTexture.bind();

            gl.glMatrixMode(GL10.GL_MODELVIEW);
            for (int i = 0; i < NUM_BOBS; i ++){
                gl.glLoadIdentity();
                gl.glTranslatef(bobs[i].x, bobs[i].y, 0);
                gl.glRotatef(45, 0, 0, 1);
                gl.glScalef(2, 0.5f, 0);
                bobModel.draw(GL10.GL_TRIANGLES, 0, 6);
            }

鲍勃.java

    public float x, y;
    float dirX, dirY;

    public Bob() {
        x = rand.nextFloat() * 320;
        y = rand.nextFloat() * 480;
        dirX = 50;
        dirY = 50;
    }

    public void update(float deltaTime) {
        x = x + dirX * deltaTime;
        y = y + dirY * deltaTime;

        if (x < 0) {
            dirX = -dirX;
            x = 0;
        }

        if (x > 320) {
            dirX = -dirX;
            x = 320;
        }

        if (y < 0) {
            dirY = -dirY;
            y = 0;
        }

        if (y > 480) {
            dirY = -dirY;
            y = 480;
        }
    }

完整代码可在https://github.com/Apress/beg-android-games-3ed/tree/master/ch07-gl-basics/Ch07/app/src/main/java/com/badlogic/androidgames获得,在 gl 和 glbasics 包、Bob、BobTest 和 Vertices 类中。

标签: javaandroidopengl-es

解决方案


推荐阅读