c++ - 关于 GLM 网格中的碰撞检测

标签 c++ opengl

我是 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();
}

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

最佳答案

是的,但是您可以确切地知道模型在矩阵中的位置,因为您在 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;

关于c++ - 关于 GLM 网格中的碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14921392/

相关文章:

c++ - 将代码从 OpenGL 2.1 转换为 OpenGL 3.2

C++11 文件系统 (VS2012)

c++ - 使用 C++ 通过互联网发送 vector

c++ - 打开文件对话框 Windows API

java - 如何将相机固定在玩家身上,以便当玩家移动时相机也会跟随

c++ - 后处理窗口视频输出

c++ - OpenGL 和 GLSL : Greyscale texture not showing

c++ - weak_ptr 包含哪些变量?

c++ - C/C++ 中的跨平台通用文本处理

opengl - 高级摩尔纹,HLSL/GLSL 程序纹理着色器中的图案减少 - 抗锯齿