2015-10-17 106 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