Skip to content

Commit 3be24a1

Browse files
Implemented bullet-asteroid collision.
1 parent a37821a commit 3be24a1

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

OpenRocket/OpenRocket/Asteroid.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Asteroid {
2121
float translatedDistance;
2222
string currentZone;
2323
int textureIndex;
24+
bool toBeDeleted = false;
2425
public:
2526
glm::mat4 asteroidMatrix;
2627
static glm::vec4 circlePoint;
@@ -43,10 +44,12 @@ class Asteroid {
4344
float getTranslatedDistance();
4445
int getTextureIndex();
4546
glm::mat4 getAsteroidMatrix() { return asteroidMatrix; }
47+
bool getToBeDeleted() { return toBeDeleted; }
4648

4749
void setX(float);
4850
float getRealY();
4951
void setY(float);
5052
void setTranslatedDistance(float);
5153
void setAsteroidMatrix(glm::mat4 matrix) { asteroidMatrix = matrix; }
54+
void setToBeDeleted(bool t) { toBeDeleted = t; }
5255
};

OpenRocket/OpenRocket/Bullet.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ Bullet::Bullet(float radius, float posX, float posY) :
1414
bool Bullet::aboveViewport() {
1515
return this->y > Constants::height;
1616
}
17+
18+
glm::vec4 Bullet::bulletCenter = { 0.0f, 0.0f, 0.f, 1.f };
19+
glm::vec4 Bullet::bulletPoint = { 0.0f, 0.0f, 0.f, 1.f };

OpenRocket/OpenRocket/Bullet.h

Lines changed: 12 additions & 3 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 radius, float posX, float posY);
1215
~Bullet();
@@ -27,6 +30,8 @@ class Bullet {
2730
return translatedDistance;
2831
}
2932

33+
bool getToBeDeleted() { return toBeDeleted; }
34+
3035
void setX(float x) {
3136
this->x = x;
3237
}
@@ -39,6 +44,10 @@ class Bullet {
3944
translatedDistance = distance;
4045
}
4146

42-
bool aboveViewport();
47+
void setToBeDeleted(bool t) { toBeDeleted = t; }
4348

49+
bool aboveViewport();
50+
static glm::vec4 bulletCenter;
51+
static glm::vec4 bulletPoint;
52+
glm::mat4 bulletMatrix;
4453
};

OpenRocket/OpenRocket/Game.cpp

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

1011
#include <GL/glew.h>
1112
#include <GL/freeglut.h>
@@ -239,11 +240,13 @@ void Game::RenderFunction(void) {
239240
glm::mat4 animateMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, bullet->getY(), 0.0));
240241
bulletMatrix = backgroundMatrix * animateMatrix * glm::translate(glm::mat4(1.0f), glm::vec3(bullet->getX(), bullet->getY(), 0.0)) * bulletMatrix;
241242

243+
bullet->bulletMatrix = bulletMatrix;
242244
glUniformMatrix4fv(myMatrixLocation, 1, GL_FALSE, &bulletMatrix[0][0]);
243245
glBindVertexArray(bulletVao);
244246
glDrawArrays(GL_POLYGON, 0, Constants::nrOfVerticesPerCircle);
245247
}
246248
rocket->RocketAsteroidsCollision(asteroids);
249+
BulletAsteroidCollision();
247250

248251
glutPostRedisplay();
249252
glFlush();
@@ -510,6 +513,8 @@ void Game::loadTextures() {
510513
void Game::CreateBulletBuffers() {
511514
GLfloat Vertices[1000];
512515
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 };
513518
for (int k = 0; k < Constants::nrOfVerticesPerCircle; k++) {
514519
float theta = Constants::TWO_PI * (float)k / (float)Constants::nrOfVerticesPerCircle;
515520
float x = Constants::bulletRadius * cos(theta);
@@ -572,3 +577,47 @@ void Game::UpdateBullets() {
572577

573578
bullets.erase(end, bullets.end());
574579
}
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+

OpenRocket/OpenRocket/Game.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class Game {
137137
void UpdateBullets();
138138
void GenerateBullet();
139139

140-
void RocketAsteroidCollision();
141140
void LoadTexture(GLuint&, const char*);
142141
void loadTextures();
142+
void BulletAsteroidCollision();
143143
};

0 commit comments

Comments
 (0)