|
6 | 6 | #include "SOIL.h" |
7 | 7 | #include "Rocket.h" |
8 | 8 | #include <ctime> |
| 9 | +#include <vector> |
9 | 10 |
|
10 | 11 | #include <GL/glew.h> |
11 | 12 | #include <GL/freeglut.h> |
@@ -239,11 +240,13 @@ void Game::RenderFunction(void) { |
239 | 240 | glm::mat4 animateMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, bullet->getY(), 0.0)); |
240 | 241 | bulletMatrix = backgroundMatrix * animateMatrix * glm::translate(glm::mat4(1.0f), glm::vec3(bullet->getX(), bullet->getY(), 0.0)) * bulletMatrix; |
241 | 242 |
|
| 243 | + bullet->bulletMatrix = bulletMatrix; |
242 | 244 | glUniformMatrix4fv(myMatrixLocation, 1, GL_FALSE, &bulletMatrix[0][0]); |
243 | 245 | glBindVertexArray(bulletVao); |
244 | 246 | glDrawArrays(GL_POLYGON, 0, Constants::nrOfVerticesPerCircle); |
245 | 247 | } |
246 | 248 | rocket->RocketAsteroidsCollision(asteroids); |
| 249 | + BulletAsteroidCollision(); |
247 | 250 |
|
248 | 251 | glutPostRedisplay(); |
249 | 252 | glFlush(); |
@@ -510,6 +513,8 @@ void Game::loadTextures() { |
510 | 513 | void Game::CreateBulletBuffers() { |
511 | 514 | GLfloat Vertices[1000]; |
512 | 515 | GLfloat Colors[1000]; |
| 516 | + Bullet::bulletCenter = { 0.f, 0.0f, 0.f, 1.f }; |
| 517 | + Bullet::bulletPoint = { Constants::bulletRadius, 0.0f, 0.f, 1.f }; |
513 | 518 | for (int k = 0; k < Constants::nrOfVerticesPerCircle; k++) { |
514 | 519 | float theta = Constants::TWO_PI * (float)k / (float)Constants::nrOfVerticesPerCircle; |
515 | 520 | float x = Constants::bulletRadius * cos(theta); |
@@ -572,3 +577,47 @@ void Game::UpdateBullets() { |
572 | 577 |
|
573 | 578 | bullets.erase(end, bullets.end()); |
574 | 579 | } |
| 580 | + |
| 581 | +double distance(glm::vec4 p1, glm::vec4 p2) { |
| 582 | + return sqrt(pow(p1[0] - p2[0], 2) + pow(p1[1] - p2[1], 2)); |
| 583 | +} |
| 584 | + |
| 585 | + |
| 586 | +void Game::BulletAsteroidCollision() { |
| 587 | + vector<int> eraseAsteroids; |
| 588 | + vector<int> eraseBullets; |
| 589 | + for (int i = 0; i < int(bullets.size()); i++) { |
| 590 | + for (int j = 0; j < int(asteroids.size()); j++) { |
| 591 | + glm::vec4 currentBulletCenter = bullets[i]->bulletMatrix * Bullet::bulletCenter; |
| 592 | + glm::vec4 currentBulletPoint = bullets[i]->bulletMatrix * Bullet::bulletPoint; |
| 593 | + |
| 594 | + glm::vec4 currentAsteroidCenter = asteroids[j]->asteroidMatrix * Asteroid::circleCenter; |
| 595 | + glm::vec4 currentAsteroidPoint = asteroids[j]->asteroidMatrix * Asteroid::circlePoint; |
| 596 | + |
| 597 | + double currentBulletRadius = sqrt(pow(currentBulletCenter[0] - currentBulletPoint[0], 2) + pow(currentBulletCenter[1] - currentBulletPoint[1], 2)); |
| 598 | + double currentAsteroidRadius = sqrt(pow(currentAsteroidCenter[0] - currentAsteroidPoint[0], 2) + pow(currentAsteroidCenter[1] - currentAsteroidPoint[1], 2)); |
| 599 | + |
| 600 | + if (distance(currentAsteroidCenter, currentBulletCenter) < currentBulletRadius + currentAsteroidRadius) { |
| 601 | + bullets[i]->setToBeDeleted(true); |
| 602 | + asteroids[j]->setToBeDeleted(true); |
| 603 | + } |
| 604 | + } |
| 605 | + } |
| 606 | + |
| 607 | + auto end1 = std::remove_if(asteroids.begin(), |
| 608 | + asteroids.end(), |
| 609 | + [](Asteroid* const& i) { |
| 610 | + return i->getToBeDeleted(); |
| 611 | + }); |
| 612 | + |
| 613 | + asteroids.erase(end1, asteroids.end()); |
| 614 | + |
| 615 | + auto end2 = std::remove_if(bullets.begin(), |
| 616 | + bullets.end(), |
| 617 | + [](Bullet* const& i) { |
| 618 | + return i->getToBeDeleted(); |
| 619 | + }); |
| 620 | + |
| 621 | + bullets.erase(end2, bullets.end()); |
| 622 | +} |
| 623 | + |
0 commit comments