|
6 | 6 | #include "SOIL/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> |
@@ -252,12 +253,14 @@ void Game::RenderFunction(void) { |
252 | 253 | glm::mat4 bulletTranslateMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(bullet->getX(), bullet->getY(), 0.0)); |
253 | 254 | bulletMatrix = backgroundMatrix * animateMatrix * bulletTranslateMatrix * bulletMatrix; |
254 | 255 |
|
| 256 | + bullet->bulletMatrix = bulletMatrix; |
255 | 257 | glUniformMatrix4fv(myMatrixLocation, 1, GL_FALSE, &bulletMatrix[0][0]); |
256 | 258 | glBindVertexArray(bulletVao); |
257 | 259 | glDrawArrays(GL_POLYGON, 0, Constants::nrOfVerticesPerCircle); |
258 | 260 | } |
259 | 261 |
|
260 | 262 | rocket->RocketAsteroidsCollision(asteroids); |
| 263 | + BulletAsteroidCollision(); |
261 | 264 |
|
262 | 265 | glm::mat4 heartMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(4.7f, 4.7f, 1.0f)); |
263 | 266 | heartMatrix = backgroundMatrix * heartMatrix; |
@@ -518,6 +521,8 @@ void Game::loadTextures() { |
518 | 521 | void Game::CreateBulletBuffers() { |
519 | 522 | GLfloat Vertices[1000]; |
520 | 523 | GLfloat Colors[1000]; |
| 524 | + Bullet::bulletCenter = { 0.f, 0.0f, 0.f, 1.f }; |
| 525 | + Bullet::bulletPoint = { Constants::bulletRadius, 0.0f, 0.f, 1.f }; |
521 | 526 | for (int k = 0; k < Constants::nrOfVerticesPerCircle; k++) { |
522 | 527 | float theta = Constants::TWO_PI * (float)k / (float)Constants::nrOfVerticesPerCircle; |
523 | 528 | float x = Constants::bulletRadius * cos(theta); |
@@ -579,8 +584,50 @@ void Game::UpdateBullets() { |
579 | 584 | bullets.erase(end, bullets.end()); |
580 | 585 | } |
581 | 586 |
|
| 587 | +double distance(glm::vec4 p1, glm::vec4 p2) { |
| 588 | + return sqrt(pow(p1[0] - p2[0], 2) + pow(p1[1] - p2[1], 2)); |
| 589 | +} |
| 590 | + |
| 591 | +void Game::BulletAsteroidCollision() { |
| 592 | + vector<int> eraseAsteroids; |
| 593 | + vector<int> eraseBullets; |
| 594 | + for (int i = 0; i < int(bullets.size()); i++) { |
| 595 | + for (int j = 0; j < int(asteroids.size()); j++) { |
| 596 | + glm::vec4 currentBulletCenter = bullets[i]->bulletMatrix * Bullet::bulletCenter; |
| 597 | + glm::vec4 currentBulletPoint = bullets[i]->bulletMatrix * Bullet::bulletPoint; |
| 598 | + |
| 599 | + glm::vec4 currentAsteroidCenter = asteroids[j]->asteroidMatrix * Asteroid::circleCenter; |
| 600 | + glm::vec4 currentAsteroidPoint = asteroids[j]->asteroidMatrix * Asteroid::circlePoint; |
| 601 | + |
| 602 | + double currentBulletRadius = sqrt(pow(currentBulletCenter[0] - currentBulletPoint[0], 2) + pow(currentBulletCenter[1] - currentBulletPoint[1], 2)); |
| 603 | + double currentAsteroidRadius = sqrt(pow(currentAsteroidCenter[0] - currentAsteroidPoint[0], 2) + pow(currentAsteroidCenter[1] - currentAsteroidPoint[1], 2)); |
| 604 | + |
| 605 | + if (distance(currentAsteroidCenter, currentBulletCenter) < currentBulletRadius + currentAsteroidRadius) { |
| 606 | + bullets[i]->setToBeDeleted(true); |
| 607 | + asteroids[j]->setToBeDeleted(true); |
| 608 | + } |
| 609 | + } |
| 610 | + } |
| 611 | + |
| 612 | + auto end1 = std::remove_if(asteroids.begin(), |
| 613 | + asteroids.end(), |
| 614 | + [](Asteroid* const& i) { |
| 615 | + return i->getToBeDeleted(); |
| 616 | + }); |
| 617 | + |
| 618 | + asteroids.erase(end1, asteroids.end()); |
| 619 | + |
| 620 | + auto end2 = std::remove_if(bullets.begin(), |
| 621 | + bullets.end(), |
| 622 | + [](Bullet* const& i) { |
| 623 | + return i->getToBeDeleted(); |
| 624 | + }); |
| 625 | + |
| 626 | + bullets.erase(end2, bullets.end()); |
| 627 | +} |
| 628 | + |
582 | 629 | void Game::CreateHeartBuffers() { |
583 | | - |
| 630 | + |
584 | 631 | GLfloat Vertices[] = { |
585 | 632 | 7.0f, 3.7f, 0.0f, 1.0f, |
586 | 633 |
|
|
0 commit comments