转换后,如何跟踪3D对象坐标。

发布于 2025-02-02 19:40:51 字数 4269 浏览 1 评论 0原文

我试图在每次转换3D对象(被视为球体)的每次转换后找到坐标,因此我可以检测到与小行星(也是球形)的碰撞。问题在于我的船可以围绕2个轴旋转:X和Y,并朝着船上的方向向前翻译,因此我不知道如何跟踪其坐标。该船最初具有一个浮点数组来确定其位置:

float position[3] = {0.f, 0.f, 0.f};

然后将船绘制为以下内容:

glPushMatrix();
LoadTextures();
glRotatef(angle_x, 1., 0., 0.);
glRotatef(angle_y, 0., 1., 0.);
glPushMatrix();
glTranslatef(position[0], position[1], position[2]-5);
GLUquadric* cylinder1=gluNewQuadric();
gluQuadricDrawStyle(cylinder1, GLU_FILL);
gluQuadricTexture(cylinder1,GLU_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[1]);
gluCylinder(cylinder1, 4, 4, 15, 20, 20);
gluDeleteQuadric(cylinder1);
glPopMatrix();
glDisable(GL_TEXTURE_2D);

glPushMatrix();
glTranslatef(position[0], position[1], position[2]-5);
GLUquadric* disk1_1=gluNewQuadric();
gluQuadricDrawStyle(disk1_1, GLU_FILL);
gluQuadricTexture(disk1_1,GLU_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[0]);
gluDisk(disk1_1, 0, 4, 20, 20);
gluDeleteQuadric(disk1_1);
glPopMatrix();
glDisable(GL_TEXTURE_2D);

glPushMatrix();
glTranslatef(position[0], position[1], position[2]+10);
GLUquadric* disk1_2=gluNewQuadric();
gluQuadricDrawStyle(disk1_2, GLU_FILL);
gluQuadricTexture(disk1_2,GLU_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[2]);
gluDisk(disk1_2, 0, 4, 20, 20);
gluDeleteQuadric(disk1_2);
glPopMatrix();
glDisable(GL_TEXTURE_2D);

// Second Cylinder
glPushMatrix();
glTranslatef(position[0], position[1], position[2]-15);
GLUquadric* cylinder2=gluNewQuadric();
gluQuadricDrawStyle(cylinder2, GLU_FILL);
gluQuadricTexture(cylinder2,GLU_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[0]);
gluCylinder(cylinder2, 3, 3, 10, 20, 20);
gluDeleteQuadric(cylinder2);
glPopMatrix();
glDisable(GL_TEXTURE_2D);

glPushMatrix();
glTranslatef(position[0], position[1], position[2]-5);
GLUquadric* disk2_1=gluNewQuadric();
gluQuadricDrawStyle(disk2_1, GLU_FILL);
gluQuadricTexture(disk2_1,GLU_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[0]);
gluDisk(disk2_1, 0, 3, 20, 20);
gluDeleteQuadric(disk2_1);
glPopMatrix();
glDisable(GL_TEXTURE_2D);

glPushMatrix();
glTranslatef(position[0], position[1], position[2]-15);
GLUquadric* disk2_2=gluNewQuadric();
gluQuadricDrawStyle(disk2_2, GLU_FILL);
gluQuadricTexture(disk2_2,GLU_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[0]);
gluDisk(disk2_2, 0, 3, 20, 20);
gluDeleteQuadric(disk2_2);
glPopMatrix();
glDisable(GL_TEXTURE_2D);

glPopMatrix();

然后,根据用户输入,我们要么使用它移动它:

void add_z(float pos) { position[2] += pos; };

或者我们旋转它:

void add_x(float angle) { angle_x=angle_x+angle;  };
void add_y(float angle) { angle_y=angle_y+angle;  };

是在keypressevent中使用的:

void MKWidget::keyPressEvent(QKeyEvent* keyEvent)
{
switch (keyEvent->key())
{
    case Qt::Key_Left:
        ship->add_y(7.);
        this->update();
    break;
    case Qt::Key_Right:
        ship->add_y(-7.);
        this->update();
    break;
    case Qt::Key_Up:
        ship->add_x(7.);
        this->update();
    break;
    case Qt::Key_Down:
        ship->add_x(-7.);
        this->update();
    break;
    case Qt::Key_Space:
        ship->add_z(ship->getSpeed());
        this->update();
        updateShipInfos();
    break;
    case Qt::Key_0:
        this->GameOver();

    break;
}
}

这 使用所有这些,在PaintEvent中调用的显示功能中,我们可以根据用户输入重新绘制船。现在位置[0]和位置[1]未更新,我不知道该怎么做,并且它起作用,因为我们只需要位置[2]和glrotatef函数在显示()开头画船。所有这些问题是,我无法跟踪船舶的坐标,我需要进行碰撞检测。这就是我现在能够做到的:

void MKWidget::updateShipInfos()
{
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0, 0.0, ship->getSpeed());
glRotatef(ship->getAngle_x(), 1., 0., 0.);
glRotatef(ship->getAngle_y(), 0., 1., 0.);
float matrix[16];

matrix[0] = p[0];
matrix[1] = p[1];
matrix[2] = p[2];
matrix[3] = 1;
glMultMatrixf(matrix);
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
p[0] = matrix[0];
p[1] = matrix[1];
p[2] = matrix[2];
cout << p[0] << " " << p[1] << " " << p[2] << endl;
glPopMatrix();
}

想法是通过我们具有的转换填充当前矩阵,以便OpenGL可以返回该点的输出坐标,但是它的工作原理非常差,很少能输出正确的结果。我该怎么做才能改善这一点?

I'm trying to find coordinates after each transformation of a 3d object (space ship perceived as a sphere), so I can detect collisions with asteroids (who are also spheres). The problem is that my ship can rotate around 2 axes : x and y, and translate forward in the direction the ship is looking at, and so I don't know how to keep track of its coordinates. The ship initially has a float array to determine its position :

float position[3] = {0.f, 0.f, 0.f};

The ship is then drawn as the following :

glPushMatrix();
LoadTextures();
glRotatef(angle_x, 1., 0., 0.);
glRotatef(angle_y, 0., 1., 0.);
glPushMatrix();
glTranslatef(position[0], position[1], position[2]-5);
GLUquadric* cylinder1=gluNewQuadric();
gluQuadricDrawStyle(cylinder1, GLU_FILL);
gluQuadricTexture(cylinder1,GLU_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[1]);
gluCylinder(cylinder1, 4, 4, 15, 20, 20);
gluDeleteQuadric(cylinder1);
glPopMatrix();
glDisable(GL_TEXTURE_2D);

glPushMatrix();
glTranslatef(position[0], position[1], position[2]-5);
GLUquadric* disk1_1=gluNewQuadric();
gluQuadricDrawStyle(disk1_1, GLU_FILL);
gluQuadricTexture(disk1_1,GLU_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[0]);
gluDisk(disk1_1, 0, 4, 20, 20);
gluDeleteQuadric(disk1_1);
glPopMatrix();
glDisable(GL_TEXTURE_2D);

glPushMatrix();
glTranslatef(position[0], position[1], position[2]+10);
GLUquadric* disk1_2=gluNewQuadric();
gluQuadricDrawStyle(disk1_2, GLU_FILL);
gluQuadricTexture(disk1_2,GLU_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[2]);
gluDisk(disk1_2, 0, 4, 20, 20);
gluDeleteQuadric(disk1_2);
glPopMatrix();
glDisable(GL_TEXTURE_2D);

// Second Cylinder
glPushMatrix();
glTranslatef(position[0], position[1], position[2]-15);
GLUquadric* cylinder2=gluNewQuadric();
gluQuadricDrawStyle(cylinder2, GLU_FILL);
gluQuadricTexture(cylinder2,GLU_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[0]);
gluCylinder(cylinder2, 3, 3, 10, 20, 20);
gluDeleteQuadric(cylinder2);
glPopMatrix();
glDisable(GL_TEXTURE_2D);

glPushMatrix();
glTranslatef(position[0], position[1], position[2]-5);
GLUquadric* disk2_1=gluNewQuadric();
gluQuadricDrawStyle(disk2_1, GLU_FILL);
gluQuadricTexture(disk2_1,GLU_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[0]);
gluDisk(disk2_1, 0, 3, 20, 20);
gluDeleteQuadric(disk2_1);
glPopMatrix();
glDisable(GL_TEXTURE_2D);

glPushMatrix();
glTranslatef(position[0], position[1], position[2]-15);
GLUquadric* disk2_2=gluNewQuadric();
gluQuadricDrawStyle(disk2_2, GLU_FILL);
gluQuadricTexture(disk2_2,GLU_TRUE);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureID[0]);
gluDisk(disk2_2, 0, 3, 20, 20);
gluDeleteQuadric(disk2_2);
glPopMatrix();
glDisable(GL_TEXTURE_2D);

glPopMatrix();

And then, according to user input, we either move it using this :

void add_z(float pos) { position[2] += pos; };

Or we rotate it :

void add_x(float angle) { angle_x=angle_x+angle;  };
void add_y(float angle) { angle_y=angle_y+angle;  };

This is used in a keyPressEvent :

void MKWidget::keyPressEvent(QKeyEvent* keyEvent)
{
switch (keyEvent->key())
{
    case Qt::Key_Left:
        ship->add_y(7.);
        this->update();
    break;
    case Qt::Key_Right:
        ship->add_y(-7.);
        this->update();
    break;
    case Qt::Key_Up:
        ship->add_x(7.);
        this->update();
    break;
    case Qt::Key_Down:
        ship->add_x(-7.);
        this->update();
    break;
    case Qt::Key_Space:
        ship->add_z(ship->getSpeed());
        this->update();
        updateShipInfos();
    break;
    case Qt::Key_0:
        this->GameOver();

    break;
}
}

And so using all this, in the display function that is called in the paintEvent, we can redraw the ship according to the user input. Now position[0] and position[1] are not updated and I have no idea how to do that, and it works because we only need the position[2] and the glRotatef functions in the beginning of the Display() to determine where to draw the ship. The problem in all of this, is that I'm unable to keep track of the coordinates of the ship, which I need for collision detection. This is what I've been able to do for now :

void MKWidget::updateShipInfos()
{
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0, 0.0, ship->getSpeed());
glRotatef(ship->getAngle_x(), 1., 0., 0.);
glRotatef(ship->getAngle_y(), 0., 1., 0.);
float matrix[16];

matrix[0] = p[0];
matrix[1] = p[1];
matrix[2] = p[2];
matrix[3] = 1;
glMultMatrixf(matrix);
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
p[0] = matrix[0];
p[1] = matrix[1];
p[2] = matrix[2];
cout << p[0] << " " << p[1] << " " << p[2] << endl;
glPopMatrix();
}

The idea is to populate the current matrix with the transformations we have so that OpenGL can return the output coordinates of the point, but it works very badly and rarely outputs correct results. What can I do to improve this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文