2015-10-17 130 views
4

我是新來的opengl。我正在做一個簡單的2D射擊遊戲,並使用AABB碰撞來做對象和子彈之間的碰撞。我爲我的飛機和廣場做了碰撞,但似乎並不奏效。可以幫我檢查我的問題是什麼?對象似乎不會碰撞使用AABB碰撞方法

#include <iostream> 
#include <time.h> 
#include <windows.h> 
#include <string.h> 
#include <math.h> 
#include <GL/glut.h> 

using namespace std; 

#define SPACEBAR 32 

class Plane { 
public: 
    GLfloat x = 0.05f; 
    GLfloat y = 0.95f; 
    GLfloat width = 0.05f; 
    GLfloat height = 0.10f; 
    GLfloat moveX = 0.0f; 
    GLfloat moveY = 0.0f; 

    void drawPlane(GLfloat, GLfloat, GLfloat, GLfloat); 
}; 

class Enemy { 
public: 
    GLfloat x, y; 
    GLfloat width, height; 
    GLfloat sqrMoveX, sqrMoveY; 

    void drawEnemySqr(GLfloat, GLfloat, GLfloat, GLfloat); 
}; 

Plane plane; 
Enemy enemy; 

GLfloat XMax, XMin, YMax, YMin; 
GLdouble clipAreaXLeft, clipAreaXRight, clipAreaYBottom, clipAreaYTop; 

int refreshMills = 30; 

float RandomNumberX() { 
    float Min = -1.0f; 
    float Max = 1.0f; 

    return ((float(rand())/float(RAND_MAX)) * (Max - Min)) + Min; 
} 

float RandomNumberY() { 
    float Min = 0.7f; 
    float Max = 1.0f; 

    return ((float(rand())/float(RAND_MAX)) * (Max - Min)) + Min; 
} 

void Timer(int value) { 
    glutPostRedisplay(); 
    glutTimerFunc(refreshMills, Timer, 0); 
} 

bool collision(GLfloat x1, GLfloat y1, GLfloat w1, GLfloat h1, 
       GLfloat x2, GLfloat y2, GLfloat w2, GLfloat h2) { 
    if ((x1 < (x2 + w2)) && 
     ((x1 + w1) > x2) && 
     (y1 < (y2 + h2)) && 
     ((h1 + y1) > y2)) { 
     return true; 
    } 
    else { 
     return false; 
    } 
} 

void initGL() { 
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
} 

void Plane :: drawPlane(GLfloat planeX, GLfloat planeY, GLfloat planeWidth, GLfloat planeHeight) { 
    glTranslatef(moveX, moveY, 0.0f); 

    glBegin(GL_POLYGON); 
    glColor3f(1.0f, 0.0f, 0.0f); 
    glVertex2f(-(planeX), -(planeY)); 
    glVertex2f(planeX, -(planeY)); 
    glVertex2f(planeX + planeWidth, -(planeY - planeHeight)); 
    glVertex2f(planeX - (planeWidth), -(planeY - (planeHeight + 0.20f))); 
    glVertex2f(-(planeX + planeWidth), -(planeY - planeHeight)); 
    glEnd(); 

    if (moveX > XMax) { 
     moveX = XMax; 
    } 
    else if (moveX < XMin) { 
     moveX = XMin; 
    } 
    if (moveY > YMax) { 
     moveY = YMax; 
    } 
    else if (moveY < YMin) { 
     moveY = YMin; 
    } 
} 

void Enemy :: drawEnemySqr(GLfloat enemyX, GLfloat enemyY, GLfloat enemyWidth, GLfloat enemyHeight) { 
    glTranslatef(sqrMoveX, sqrMoveY, 0.0f); 

    glBegin(GL_QUADS); 
    glColor3f(1.0f, 0.0f, 1.0f); 
    glVertex2f(enemyX, enemyY); 
    glVertex2f(enemyX + enemyWidth, enemyY); 
    glVertex2f(enemyX + enemyWidth, enemyY + enemyHeight); 
    glVertex2f(enemyX, enemyWidth + enemyY); 
    glEnd(); 

    sqrMoveY -= 0.0005f; 

    if (sqrMoveY < -1.8f) { 
     sqrMoveX = RandomNumberX(); 
     sqrMoveY = RandomNumberY(); 
    } 

    glutPostRedisplay(); 
} 

void display() { 
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glEnable(GL_TEXTURE_2D); 

    glPushMatrix(); 
    plane.drawPlane(plane.x, plane.y, plane.width, plane.height); 
    glPopMatrix(); 

    glPushMatrix(); 
    enemy.drawEnemySqr(0.0f, 0.0f, 0.3f, 0.3f); 
    glPopMatrix(); 

    if (collision(plane.moveX, plane.moveY, plane.width, plane.height, 
        enemy.sqrMoveX, enemy.sqrMoveY, enemy.width, enemy.height) == true) { 
     enemy.sqrMoveY = -2.0f; 
    } 

    glutSwapBuffers(); 
} 

void keyButton(int key, int x, int y) { 
    switch (key) { 
    case GLUT_KEY_RIGHT: 
     plane.moveX += 0.05f; 
     break; 
    case GLUT_KEY_LEFT: 
     plane.moveX += -0.05f; 
     break; 
    case GLUT_KEY_UP: 
     plane.moveY += 0.05f; 
     break; 
    case GLUT_KEY_DOWN: 
     plane.moveY += -0.05f; 
     break; 
    default: 
     break; 
    } 

    glutPostRedisplay(); 
} 

int main(int argc, char** argv) { 
    srand(time(NULL)); 
    glutInit(&argc, argv); 

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 
    glutInitWindowSize(480, 640); 
    glutInitWindowPosition(50, 50); 
    glutCreateWindow("Plane Shooting Game"); 

    glutDisplayFunc(display); 
    glutIdleFunc(idle); 
    glutReshapeFunc(reshape); 
    glutTimerFunc(25, Timer, 0); 
    glutSpecialFunc(keyButton); 
    initGL(); 
    glutMainLoop(); 
    return 0; 
} 
+0

我認爲你的'碰撞'需要更廣泛的測試。一目瞭然,它似乎只在對象#1完全位於對象#2內時才進行測試。 – usr2564301

+0

雅,我確實需要更多的測試,因爲這個功能碰撞並不完美。感謝您的評論! –

回答

2

你在這一行有一個問題:

if (collision(plane.moveX, plane.moveY, plane.width, plane.height, 
       enemy.sqrMoveX, enemy.sqrMoveY, enemy.width, enemy.height) == true) 

你必須通過plane.xplane.yenemy.xenemy.y碰撞的功能,而不是plane.moveXplane.moveYenemy.sqrMoveXenemy.sqrMoveY

+1

它的工作原理!我將我的平面的x和y座標更改爲0.0f,並將顯示函數中碰撞函數的變量輸入更改爲plane.x + plane.moveX和plane.y + plane.moveY。然後碰撞工作!謝謝您的幫助! –

+0

你應該除了這個傢伙回答 – Rob85