Skip to content

Commit 5830e7a

Browse files
authored
Merge pull request #38 from DianaVasiliu/bianca
Implemented bullet-asteroid collision.
2 parents fc05689 + 650c538 commit 5830e7a

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>
@@ -249,12 +250,14 @@ void Game::RenderFunction(void) {
249250
glm::mat4 animateMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, bullet->getY(), 0.0));
250251
bulletMatrix = backgroundMatrix * animateMatrix * glm::translate(glm::mat4(1.0f), glm::vec3(bullet->getX(), bullet->getY(), 0.0)) * bulletMatrix;
251252

253+
bullet->bulletMatrix = bulletMatrix;
252254
glUniformMatrix4fv(myMatrixLocation, 1, GL_FALSE, &bulletMatrix[0][0]);
253255
glBindVertexArray(bulletVao);
254256
glDrawArrays(GL_POLYGON, 0, Constants::nrOfVerticesPerCircle);
255257
}
256258

257259
rocket->RocketAsteroidsCollision(asteroids);
260+
BulletAsteroidCollision();
258261

259262
glm::mat4 heartMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(4.7f, 4.7f, 1.0f));
260263
heartMatrix = backgroundMatrix * heartMatrix;
@@ -515,6 +518,8 @@ void Game::loadTextures() {
515518
void Game::CreateBulletBuffers() {
516519
GLfloat Vertices[1000];
517520
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 };
518523
for (int k = 0; k < Constants::nrOfVerticesPerCircle; k++) {
519524
float theta = Constants::TWO_PI * (float)k / (float)Constants::nrOfVerticesPerCircle;
520525
float x = Constants::bulletRadius * cos(theta);
@@ -576,8 +581,50 @@ void Game::UpdateBullets() {
576581
bullets.erase(end, bullets.end());
577582
}
578583

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+
579626
void Game::CreateHeartBuffers() {
580-
627+
581628
GLfloat Vertices[] = {
582629
7.0f, 3.7f, 0.0f, 1.0f,
583630

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)