Skip to content

Commit a70d793

Browse files
committed
Merge branch 'master' of https://github.com/DianaVasiliu/OpenRocket into diana
2 parents c5d0bee + 5830e7a commit a70d793

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

OpenRocket/OpenRocket/Asteroid.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class Asteroid {
2121
float translatedDistance;
2222
string currentZone;
2323
int textureIndex;
24-
24+
bool toBeDeleted = false;
25+
2526
public:
2627
glm::mat4 asteroidMatrix;
2728
static glm::vec4 circlePoint;
@@ -35,6 +36,7 @@ class Asteroid {
3536
bool inLowerHalf();
3637
bool belowViewport();
3738

39+
bool getToBeDeleted() const { return toBeDeleted; }
3840
float getX() const;
3941
float getY() const;
4042
float getRadius() const;
@@ -47,6 +49,7 @@ class Asteroid {
4749
void setX(float);
4850
void setY(float);
4951
void setTranslatedDistance(float);
52+
void setToBeDeleted(bool t) { toBeDeleted = t; }
5053
void setAsteroidMatrix(glm::mat4);
5154
void setCurrentZone(string);
5255
};

OpenRocket/OpenRocket/Bullet.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ bool Bullet::aboveViewport() {
1515
return this->y > Constants::height;
1616
}
1717

18+
glm::vec4 Bullet::bulletCenter = { 0.0f, 0.0f, 0.0f, 1.0f };
19+
glm::vec4 Bullet::bulletPoint = { 0.0f, 0.0f, 0.0f, 1.0f };
20+
1821
float Bullet::getRadius() const {
1922
return radius;
2023
}

OpenRocket/OpenRocket/Bullet.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
#pragma once
1+
#include "glm/glm/glm.hpp"
2+
#include "glm/glm/ext/matrix_transform.hpp"
3+
#include "glm/glm/gtx/transform.hpp"
4+
#include "glm/glm/gtc/type_ptr.hpp"
25

36
class Bullet {
47
private:
58
float radius;
69
float translatedDistance;
710
float x;
811
float y;
9-
12+
bool toBeDeleted = false;
1013
public:
1114
Bullet(float, float, float);
1215
~Bullet() {}
@@ -15,6 +18,13 @@ class Bullet {
1518
float getX() const;
1619
float getY() const;
1720
float getTranslatedDistance() const;
21+
22+
bool getToBeDeleted() { return toBeDeleted; }
23+
void setToBeDeleted(bool t) { toBeDeleted = t; }
24+
25+
static glm::vec4 bulletCenter;
26+
static glm::vec4 bulletPoint;
27+
glm::mat4 bulletMatrix;
1828

1929
void setX(float);
2030
void setY(float);

OpenRocket/OpenRocket/Game.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "SOIL/SOIL.h"
77
#include "Rocket.h"
88
#include <ctime>
9+
#include <vector>
910

1011
#include <GL/glew.h>
1112
#include <GL/freeglut.h>
@@ -252,12 +253,14 @@ void Game::RenderFunction(void) {
252253
glm::mat4 bulletTranslateMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(bullet->getX(), bullet->getY(), 0.0));
253254
bulletMatrix = backgroundMatrix * animateMatrix * bulletTranslateMatrix * bulletMatrix;
254255

256+
bullet->bulletMatrix = bulletMatrix;
255257
glUniformMatrix4fv(myMatrixLocation, 1, GL_FALSE, &bulletMatrix[0][0]);
256258
glBindVertexArray(bulletVao);
257259
glDrawArrays(GL_POLYGON, 0, Constants::nrOfVerticesPerCircle);
258260
}
259261

260262
rocket->RocketAsteroidsCollision(asteroids);
263+
BulletAsteroidCollision();
261264

262265
glm::mat4 heartMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(4.7f, 4.7f, 1.0f));
263266
heartMatrix = backgroundMatrix * heartMatrix;
@@ -518,6 +521,8 @@ void Game::loadTextures() {
518521
void Game::CreateBulletBuffers() {
519522
GLfloat Vertices[1000];
520523
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 };
521526
for (int k = 0; k < Constants::nrOfVerticesPerCircle; k++) {
522527
float theta = Constants::TWO_PI * (float)k / (float)Constants::nrOfVerticesPerCircle;
523528
float x = Constants::bulletRadius * cos(theta);
@@ -579,8 +584,50 @@ void Game::UpdateBullets() {
579584
bullets.erase(end, bullets.end());
580585
}
581586

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+
582629
void Game::CreateHeartBuffers() {
583-
630+
584631
GLfloat Vertices[] = {
585632
7.0f, 3.7f, 0.0f, 1.0f,
586633

OpenRocket/OpenRocket/Game.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,5 @@ class Game {
146146

147147
void LoadTexture(GLuint&, const char*);
148148
void loadTextures();
149+
void BulletAsteroidCollision();
149150
};

0 commit comments

Comments
 (0)