因此,我在wazoo上用谷歌搜索了一下,查阅了各种资源--文本、讲座等--但我要么漏掉了什么,要么就是没有正确理解。对于OpenGL中的一个类,我们需要设计一个小行星游戏,这看起来是一个足够简单的概念,但我发现很难让我的船向下移动。我知道我们需要将我们的船平移到原点,执行旋转,然后将它平移回适当的位置-我面临的问题是理解它的执行。到目前为止,我画一艘船的代码是这样的:
void
drawShip(Ship *s)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
myTranslate2D(s->x, s->y);
float x = s->x, y = s->y;
myRotate2D(s->phi);
glBegin(GL_LINE_LOOP);
glVertex2f(s->x + s->dx, s->y + s->dy + 3);
glVertex2f(s->x + s->dx-2, s->y + s->dy - 3);
glVertex2f(s->x + s->dx, s->y + s->dy -1.5);
glVertex2f(s->x + s->dx + 2, s->y + s->dy - 3);
glEnd();
myTranslate2D(s->x - x, s->y - y);
glPopMatrix();
}旋转和运动由以下变量确定,这些变量在键盘功能中被正确修改:
if (left == 1)
{
ship.phi -= 0.3;
}
if (right == 1)
{
ship.phi += 0.3;
}
if (up == 1)
{
ship.dx += -2.0*sin(ship.phi);
ship.dy += 2.0*cos(ship.phi);
}
if (down == 1)
{
ship.dx += 2.0*sin(ship.phi);
ship.dy += -2.0*cos(ship.phi);
}
drawShip(&ship);一旦我得到轮换,我有信心我可以完成其余的一切。实际上,上面的代码将允许我的对象在原点上或围绕原点旋转,但它不会原地旋转,除非它直接在原点上。ship本身已初始化为(0,0)、(25,25)和(50,50)点,出于我的目的,屏幕(0,0)的原点是左下角。
有人能帮我理解为什么我的船不会原地旋转,而只是围绕原点旋转吗?我希望这与我如何进行平移和旋转有关,但我不知道是什么。
发布于 2016-02-06 00:35:24
您的绘图函数似乎错误。你的船设计不应该依赖于它的位置。这意味着顶点的坐标应该总是相同的。然后使用GlTranslate和旋转将其放到正确的位置。您还可以在绘制后和弹出矩阵之前进行转换。这不会做任何事情,因为矩阵更改只应用于矩阵更改后定义的顶点。
结构应该是这样的:
// Make sure the origin is in the 0, 0 of your space.
GlPushMatrix(); // Save this setup
GlTranslate(ship.x, ship.y); // Location of the ship.
GlRotate(ship.phi); // Or whatever is the name of the angle of the ship.
DrawShip(); // Draws the sprite/wireframe/object you want.
GlPopMatrix(); // Return to the original setup.发布于 2016-02-06 02:53:44
好吧,实际上我是通过戳一下才弄明白的。问题是,我一直在直接从绘图中绘制速度的变化,而不是根据速度计算新的位置并进行转换。下面是固定的代码:
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
float newX = s->x + s->dx, newY = s->y + s->dy;
myTranslate2D(newX, newY);
myRotate2D(s->phi);
glBegin(GL_LINE_LOOP);
glVertex2f(s->x, s->y + 3);
glVertex2f(s->x - 2, s->y - 3);
glVertex2f(s->x, s->y - 1.5);
glVertex2f(s->x + 2, s->y - 3);
glEnd();
glPopMatrix();https://stackoverflow.com/questions/35228913
复制相似问题