Кажется, объект не сталкивается, используя метод столкновения AABB

Я новичок в OpenGL. Я делаю простую 2D-игру со стрельбой и использую столкновение 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;
}

4

Решение

У вас есть проблема в этой строке:

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

Вы должны пройти plane.x, plane.y, enemy.x, enemy.y к функции столкновения вместо plane.moveX, plane.moveY, enemy.sqrMoveX, enemy.sqrMoveY,

2

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector