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