2012-06-28 101 views
2

編輯:原來我只是有一些值-switched-。簡單的碰撞檢測?


我與OpenGL的工作,並與我的Windows計算機上的C語言的表達,而我試圖得到一個非常,非常基本的碰撞檢測回事。 現在,我想這個,看有沒有點牛,奧茲是兩個框內:

int collision(int box[2][4]) { 
    int col; 
    int i; 
    for (i=0;i<2;i++){ 
    col = 0; 
    printf("x1:%.0f y1:%.0f x2:%.0f y2:%.0f \n",colbox[i][0],colbox[i][1],colbox[i][2],colbox[i][3]); 
    printf("Ox:%.1f Oy:%.1f Oz:%.1f \n" , Ox,here,Oz); 
    if ((colbox[i][0] < Ox) && (Ox < colbox[i][2])&& (colbox[i][1] < Oz) && (colbox[i][3] > Oz)) 
     return 1; 
    else 
     return 0; 
    } 

} 

我也嘗試變化,就像沒有穿過的功能,將外循環功能等問題似乎與平等檢查?

在我試圖做的最低級別是看一個點(相機)是否在一個邊界內(這是一個保存在二維數組中的4個點的集合),如果它是在列出的邊界中,返回1.如果點不在任何邊界中,則返回0.

當此功能起作用時,它僅對一個區域執行操作,如果移動到首先列出的有界區域,它適當返回,但第二個區域不能正確返回。如果它真的有用

全碼:

// Values 
double Ox=0, Oz=0; 
float Oy=1.0; 
double asp=1;  // Aspect ratio 
double dim=20;  // Size of world 
double fov=55;  // Field of View 
double ph,th = 0; 
// angle of rotation for the camera direction 
float angle = 0.0; 
// actual vector representing the camera's direction 
float lx=0.0,lz=-1.0; 
// the key states. These variables will be zero 
//when no key is being presses 
float deltaAngle = 0.0; 
float deltaMove = 0; 
double here; 
int collide; 

// {x1,y1,x2,y2} 
double colbox[2][4] = { 
    {3,3,6,6}, 
    {-1,-14,-3,-16} 
}; 

//front-1 back-2 left-3 right-4 
int collision(double box[2][4]) { 
    int i; 
    for (i=0;i<2;i++){ 
    col = 0; 
    printf("x1:%.0f y1:%.0f x2:%.0f y2:%.0f \n",colbox[i][0],colbox[i][1],colbox[i][2],colbox[i][3]); 
    printf("Ox:%.1f Oy:%.1f Oz:%.1f \n" , Ox,here,Oz); 
    if ((colbox[i][0] < Ox) && (Ox < colbox[i][2])&& (colbox[i][1] < Oz) && (colbox[i][3] > Oz)) 
     return 1; 
    else 
     return 0; 
    } 

}    

//test 
static void ball(double x,double y,double z,double r) 
{ 
    // Save transformation 
    glPushMatrix(); 
    // Offset, scale and rotate 
    glTranslated(x,y,z); 
    glScaled(r,r,r); 
    // White ball 
    glColor3f(1,1,1); 
    glutSolidSphere(1.0,16,16); 
    // Undo transofrmations 
    glPopMatrix(); 
} 
static void square(double r) { 
    glPushMatrix(); 
    glScaled(r,r,r); 
    glColor3f(1,0,0); 
    glBegin(GL_QUADS); 
    glVertex3f(-1,0,-1); 
    glVertex3f(1,0,-1); 
    glVertex3f(1,0,1); 
    glVertex3f(-1,0,1); 
    glEnd(); 
    glPopMatrix(); 
} 

//test 
void computePos(float deltaMove) { 
    Ox += deltaMove * lx * 0.1f; 
    Oz += deltaMove * lz * 0.1f; 
} 
void computeDir(float deltaAngle) { 
    angle += deltaAngle; 
    lx = sin(angle); 
    lz = -cos(angle); 
} 


// display 
void display() { 
    here = 2.0f*Oy; 
    if (deltaMove) 
     computePos(deltaMove); 
    if (deltaAngle) 
     computeDir(deltaAngle); 
       const double len=2.0; 
    // Erase the window and the depth buffer 
    glClearColor(0.3,0.5,1.0,1); 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
    // Enable Z-buffering in OpenGL 
    glEnable(GL_DEPTH_TEST); 
    // Set perspective 
    glLoadIdentity(); 
    gluLookAt(Ox,2,Oz, Ox+lx, here, Oz+lz, 0,1,0); 

    ball(5,5,5,2); ball(-2,0,-15,1); 
    square(15); 
    glColor3f(1,1,1); 
     glBegin(GL_LINES); 
     glVertex3d(0.0,0.0,0.0); 
     glVertex3d(len,0.0,0.0); 
     glVertex3d(0.0,0.0,0.0); 
     glVertex3d(0.0,len,0.0); 
     glVertex3d(0.0,0.0,0.0); 
     glVertex3d(0.0,0.0,len); 
     glEnd(); 
     // Label axes 
     glRasterPos3d(len,0.0,0.0); 
     Print("X"); 
     glRasterPos3d(0.0,len,0.0); 
     Print("Y"); 
     glRasterPos3d(0.0,0.0,len); 
     Print("Z"); 
    // Render the scene and make it visible 
    glColor3f(0,0,0); 
    glWindowPos2i(5,5); 
    Print("Ox:%.1f Oy:%.1f Oz:%.1f lx:%.1f lz:%.1f Collide:%d", Ox,here,Oz,lx,lz,collide); 
    ErrCheck("display"); 
    glFlush(); 
    glutSwapBuffers(); 
} 


void key(unsigned char ch,int x,int y) { 
    // Exit on ESC 
    if (ch == 27) 
     exit(0); 
    // WASD controls 
    else if (ch == 'a' || ch == 'A') 
     deltaAngle = -0.05; 
    else if (ch == 'd' || ch == 'D') 
     deltaAngle = 0.05; 
    else if (ch == 'w' || ch == 'W') { 
      collide=collision(colbox); 
      deltaMove = 1; } 

    else if (ch == 's' || ch == 'S') { 
      collide=collision(colbox); 
      deltaMove = -1; } 

    else if ((ch == 'e' || ch == 'E') && here < 4) 
     Oy += 0.01; 
    else if ((ch == 'c' || ch == 'C') && here > .5) 
     Oy -= 0.01; 

    Project(fov,asp,dim); 
    glutPostRedisplay(); 
} 

void reshape(int width,int height) { 
    // Ratio of the width to the height of the window 
    asp = (height>0) ? (double)width/height : 1; 
    // Set the viewport to the entire window 
    glViewport(0,0, width,height); 
    // Set projection 
    Project(fov,asp,dim); 
} 

void releaseKey(unsigned char ch, int x, int y) { 
    if (ch == 'a' || ch == 'A') 
     deltaAngle = 0; 
    else if (ch == 'd' || ch == 'D') 
     deltaAngle = 0; 
    else if (ch == 'w' || ch == 'W') 
     deltaMove = 0; 
    else if (ch == 's' || ch == 'S') 
     deltaMove = 0; 
    Project(fov,asp,dim); 
    glutPostRedisplay(); 
} 


int main(int argc,char* argv[]) { 
    // Initialize GLUT 
    glutInit(&argc,argv); 
    // Request double buffered, true color window with Z buffering at 600x600 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); 
    glutInitWindowSize(800,500); 
    glutCreateWindow("Haunted House"); 
    // Set callbacks 
    glutDisplayFunc(display); 
    glutReshapeFunc(reshape); 
// glutSpecialFunc(special); 
    glutKeyboardFunc(key); 

    glutKeyboardUpFunc(releaseKey); 
    // Pass control to GLUT so it can interact with the user 
    ErrCheck("init"); 
    glutMainLoop(); 
    return 0; 
} 
+0

這些盒子是否與X軸和Y軸對齊? – argentage

+0

從技術上講,它們與X和Z軸對齊,但由於我沒有在3D中進行檢測,因此我認爲它是X和Y. –

+0

我試圖理解您擁有的多維數組設置,而不是真的能夠弄清楚發生了什麼 - 尤其是牛和奧茲。我在JS中編寫了類似這樣的代碼,但它看起來不同,當然,數組的命名方案和這些函數的功能讓我沮喪。 – argentage

回答

2

我認爲當你有我= 1的第二行

if ((colbox[i+1][0] < Ox) && (Ox < colbox[i+1][2])&& (colbox[i+1][1] < Oz) && (colbox[i+1][3] > Oz)) 

走開大小2的數組(因爲最大的索引因爲這是1.)

因此,鑑於它讀取的內存不在陣列內部(我認爲一般都可以),您會發現第在條件可能會失敗很多,甚至大部分時間,因爲它基本上是隨機數據。因此它返回0.

+0

阿哈..我忘了我把那個放在那裏,甚至。謝謝你指出! 我想我最初把它放在那裏,因爲在看printf時,它甚至沒有迭代到第二個條目...讓我調整它,看看會發生什麼。 –