OpenGL - 旋转2D小行星船

时间:2016-02-05 16:26:07

标签: c opengl rotation

所以我一直在搜索wazoo并浏览各种资源 - 文本,讲座等 - 但我要么缺少某些东西,要么就是没有正确把握它。对于OpenGL中的一个类,我们需要设计一个Asteroids游戏,这似乎是一个简单的概念,但我发现很难让我的船的运动正确。我知道我们需要将我们的船转换为原点,执行旋转,并将其转换回适当的位置 - 我面临的问题是理解它的执行。到目前为止,我用于绘制船舶的代码是这样的:

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);

一旦我完成了轮换,我相信我可以完成所有其他工作。实际上,上面的代码将允许我的对象在原点上或在它周围旋转,但它不会旋转到位,除非它直接在原点上。船本身已初始化为(0,0),(25,25)和(50,50)点,为了我的目的,屏幕的原点(0,0)是左下角。

有人可以帮助我理解为什么我的船不会旋转到位,只有原点?我希望它与我如何执行翻译和轮换有关,但我不知道是什么。

2 个答案:

答案 0 :(得分:1)

你的绘图功能似乎错了。 您的船舶设计不应该取决于它的位置。这意味着顶点的坐标应始终相同。然后使用GlTranslate和Rotate将其置于正确的位置。在绘制之后和弹出矩阵之前,您也可以进行平移。这没有任何作用,因为矩阵更改仅适用于矩阵更改后定义的顶点。

结构应该是这样的:

// 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.

答案 1 :(得分:1)

好吧,我实际上用一些戳戳了解它。问题是我一直在直接从绘图中绘制速度的变化,而不是根据速度和平移来计算新的位置。这是固定代码:

if (UserPage.Values[0] = 'NC') then FileName: {sys}\inetsrv\appcmd.exe; Parameters: "set......"