2013-02-17 42 views
0

我新的OpenGL和GLM,目前我在做它一類項目,我上傳兩個物體汽車如下關於在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幫助..

+0

僅供參考:有兩件事叫做「GLM」;你應該具體說明這是網格GLM,而不是數學GLM。 – 2013-02-17 21:39:05

+0

@ user2080429請仔細檢查答案,並投票支持你的答案。您也可以點擊答案附近的複選框將其選爲您問題的正式答案。 – karlphillip 2013-03-21 17:27:51

回答

1

對,但你可以確切地知道在哪裏模型放置在矩陣中,因爲您在glmDraw()之前將glTranslatef()放置在矩陣中。現在,由於您知道模型的座標,因此您可以開始檢查簡單的碰撞。

但是,如果你正在尋找一個更真實/複雜的碰撞檢測,你應該打開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; 
+0

我把translatef的汽車運動,但我不知道它仍然是位置,我很新,plz幫助 – 2013-02-17 15:56:17

+0

汽車的位置取決於'carx,cary,carz'。你還需要檢測與這個物體的碰撞嗎? – karlphillip 2013-02-17 15:59:43

+0

請原諒我的無知,但我仍然困惑,例如:是不是像我的汽車的初始座標是(x,y,z),然後翻譯函數將它翻譯爲(x + carx,y + cary, z + carz)?那麼我怎麼知道x,y,z仍然是什麼值?還是有什麼辦法使初始座標(即x,y,z)爲零,以便我可以使用carx,cary,carz進行碰撞檢測? – 2013-02-17 16:06:37