java - 使用当前坐标调用 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 类中。
解决方案
推荐阅读
- jenkins - 如何根据作业名称在 Jenkins 管道中返回参数?
- python - OpenCV HLS youtube 流在几帧后停止
- php - 使用 PHP 显示 IMAP 电子邮件 - 如果线程中的邮件数量是偶数,则邮件无法正确显示
- java - 如何使用 flatMap() 强制并行执行 BufferedStream.lines()?
- sql - 如何通过比较同一表中的 2 列仅获得不匹配的结果
- git - 为什么我在 Git 中的贡献比其他同事少?
- msbuild - 烘干
有时有效,有时中断 - python - Dell Boomi 应用程序 (atom.exe) 找不到 python 解释器
- regex - RegEx 匹配除换行符之外的任何内容,直到正前瞻
- reactjs - 我的 html 没有呈现,但路线在反应中发生了变化