关于GLM网格中的碰撞检测

About collision detection in GLM mesh

本文关键字:碰撞检测 网格 GLM 关于      更新时间:2023-10-16

我是opengl和glm的新手,目前我正在其中做一个类项目,我上传了两个对象汽车,如下

void CARMODEL:: drawmodel_box()
{
    glPushMatrix();
    glTranslatef(carx,cary ,carz);
    if (!pmodel1){
        pmodel1 = glmReadOBJ("Car.obj");
    }
    glmDraw(pmodel1, GLM_SMOOTH | GLM_TEXTURE | GLM_MATERIAL);
    glPopMatrix();
}
void OpponentCarModel::drawopponentmodel()
{
    glPushMatrix();
    srand(time(NULL));
    opcarx=rand() % 7-3+(double)rand()/(RAND_MAX+1)*(1-0)+0;
    glTranslatef(opcarx,0,-20);
    if (!pmodel2){
        pmodel2 = glmReadOBJ("car.obj");
    }
    glmDraw(pmodel2, GLM_SMOOTH | GLM_TEXTURE | GLM_MATERIAL);
    glPopMatrix();
}

现在,一切都很好,现在我来谈谈碰撞检测部分,我不确定如何在两辆车之间进行,因为我不知道它们的坐标或顶点,所以plz有帮助。。

没错,但您可以确切地知道模型在矩阵中的位置,因为您在glmDraw()之前调用glTranslatef()来将它们放置在那里。现在,既然知道了模型的x,y,z坐标,就可以开始检查简单的碰撞了。

但是,如果你正在寻找更真实/复杂的碰撞检测,你应该打开glm.h并检查GLMmodel结构的定义,因为它存储了在屏幕上绘制模型所需的一切,包括顶点信息、法线、纹理坐标等:

/* GLMmodel: Structure that defines a model.
 */
typedef struct _GLMmodel {
  char*    pathname;            /* path to this model */
  char*    mtllibname;          /* name of the material library */
  GLuint   numvertices;         /* number of vertices in model */
  GLfloat* vertices;            /* array of vertices  */
  GLuint   numnormals;          /* number of normals in model */
  GLfloat* normals;             /* array of normals */
  GLuint   numtexcoords;        /* number of texcoords in model */
  GLfloat* texcoords;           /* array of texture coordinates */
  GLuint   numfacetnorms;       /* number of facetnorms in model */
  GLfloat* facetnorms;          /* array of facetnorms */
  GLuint       numtriangles;    /* number of triangles in model */
  GLMtriangle* triangles;       /* array of triangles */
  GLuint       nummaterials;    /* number of materials in model */
  GLMmaterial* materials;       /* array of materials */
  GLuint       numgroups;       /* number of groups in model */
  GLMgroup*    groups;          /* linked list of groups */
  GLfloat position[3];          /* position of the model */
} GLMmodel;