无法在openGl中同时向相反方向移动两个对象

dxxyhpgq  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(138)

我试图做一个项目,我们可以移动一个坦克在一个方向,其他在相反的方向通过使用translatef函数我能够移动坦克1,当我尝试移动坦克2两个坦克开始移动在同一方向沿着背景。这里是图像:-x1c 0d1x这里当我点击“移动俄罗斯坦克"。它是移动。当我点击乌克兰坦克我希望它移动在相反的方向。我不能移动乌克兰坦克在相反的方向。你能帮助这个吗?。这里是我的代码片段:-

float th = 0.0;
float trX = 0.0, trY = 0.0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    if (rFlag == 1) //Rotate Around origin
    {
        trX += 0.5;
        trY += 0.0;
    }
    background();
    tank2();
    glTranslatef(trX, trY, 0.0);
    if (rFlag == 2) {
        trX = 0.0;
    }
    tank1();
    glutPostRedisplay();
    glutSwapBuffers();
}
void rotateMenu(int option)
{
    if (option == 1)
        rFlag = 1;
    if (option == 2)
        rFlag = 2;
    if (option == 3)
        rFlag = 3;
}
void tank1()
{
    glColor3f(0.0f, 0.1f, 0.0f);
    glBegin(GL_QUADS);
    glVertex2f(-479, -429);
    glVertex2f(-479, -359);
    glVertex2f(-399, -359);
    glVertex2f(-399, -429);
    glEnd();
    glColor3f(1.0f, 1.0f, 1.0f);
    glBegin(GL_QUADS);
    glVertex2f(-459, -389);
    glVertex2f(-459, -379);
    glVertex2f(-419, -379);
    glVertex2f(-419, -389);
    glEnd();
    glColor3f(0.0f, 0.0f, 1.0f);
    glBegin(GL_QUADS);
    glVertex2f(-459, -399);
    glVertex2f(-459, -389);
    glVertex2f(-419, -389);
    glVertex2f(-419, -399);
    glEnd();
    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_QUADS);
    glVertex2f(-459, -409);
    glVertex2f(-459, -399);
    glVertex2f(-419, -399);
    glVertex2f(-419, -409);
    glEnd();
    glColor3f(0.0f, 0.1f, 0.0f);
    glBegin(GL_QUADS);
    glVertex2f(-459, -359);
    glVertex2f(-459, -329);
    glVertex2f(-419, -329);
    glVertex2f(-419, -359);
    glEnd();
    glBegin(GL_QUADS);
    glVertex2f(-439, -329);
    glVertex2f(-419, -289);
    glVertex2f(-414, -289);
    glVertex2f(-434, -329);
    glEnd();
}
void tank2()
{
    glColor3f(0.0f, 0.1f, 0.0f);
    glBegin(GL_QUADS);
    glVertex2f(391, -309);
    glVertex2f(391, -239);
    glVertex2f(471, -239);
    glVertex2f(471, -309);
    glEnd();
    glColor3f(0.0f, 0.0f, 1.0f);
    glBegin(GL_QUADS);
    glVertex2f(411, -279);
    glVertex2f(411, -264);
    glVertex2f(451, -264);
    glVertex2f(451, -279);
    glEnd();
    glColor3f(1.0f, 1.0f, 0.0f);
    glBegin(GL_QUADS);
    glVertex2f(411, -279);
    glVertex2f(411, -294);
    glVertex2f(451, -294);
    glVertex2f(451, -279);
    glEnd();
    glColor3f(0.0f, 0.1f, 0.0f);
    glBegin(GL_QUADS);
    glVertex2f(411, -239);
    glVertex2f(411, -209);
    glVertex2f(451, -209);
    glVertex2f(451, -239);
    glEnd();
    glBegin(GL_QUADS);
    glVertex2f(431, -209);
    glVertex2f(451, -169);
    glVertex2f(456, -169);
    glVertex2f(436, -209);
    glEnd();
}
int background()
{
    glColor3f(0.0f, 1.0f, 0.0f);//Green
    glBegin(GL_QUADS);
    glVertex2f(-600, -800);
    glVertex2f(-600, -100);
    glVertex2f(800, -100);
    glVertex2f(800, -800);
    glEnd();
}
5cnsuln7

5cnsuln71#

你需要为两个坦克设置单独的位置 * 和 * 一个单独的机制来更新它们的位置。让我们应用一些OO:

struct Tank {
  float x = 0, y = 0;
  float dx = 0, dy = 0;
  std::function<void()> drawFunc;

  void update() {
    x += dx;
    y += dy;
  }
  void startMoving(float dx_, float dy_) {
    dx = dx_;
    dy = dy_;
  }
  void stopMoving() {
    dx = dy = 0;
  }
  void draw() {
    glLoadIdentity();
    glTranslatef(dx, dy, 0);
    drawFunc();
  }
};

这意味着我们现在可以在某个全局位置声明以下内容:

Tank tank1{0, 0, 0, 0, &draw_tank1};
Tank tank2{0, 0, 0, 0, &draw_tank2};

您的显示功能现在简单地为:

tank1.update(); tank1.draw();
tank2.update(); tank2.draw();

当然,您需要在正确的槽上调用startMovingstopMoving以响应输入。
作为第一个改进,您应该使xy位置与屏幕上坦克的位置相匹配,并重写draw_tank函数以在(0,0)周围绘制。

相关问题