Kolizje oraz koniec gry

This commit is contained in:
Nikodem145 2024-02-06 01:47:46 +01:00
parent 8882b979c2
commit 2be5ef3136

View File

@ -18,6 +18,8 @@
#include <cmath> #include <cmath>
#include "SOIL/SOIL.h" #include "SOIL/SOIL.h"
#include <chrono> #include <chrono>
#include <sstream>
namespace texture { namespace texture {
GLuint earth; GLuint earth;
GLuint clouds; GLuint clouds;
@ -77,7 +79,7 @@ std::uniform_real_distribution<float> radiusDistribution(-5.f, 5.f);
std::uniform_real_distribution<float> planetoidsYDistribution(-3.f, 3.f); std::uniform_real_distribution<float> planetoidsYDistribution(-3.f, 3.f);
std::uniform_real_distribution<float> planetoidsXDistribution(-2.f, 10.f); std::uniform_real_distribution<float> planetoidsXDistribution(-2.f, 10.f);
std::uniform_real_distribution<float> planetoidsScaleDistribution(0.1f, 0.2f); std::uniform_real_distribution<float> planetoidsScaleDistribution(0.1f, 0.2f);
float planetoidsArray[400][4]; float planetoidsArray[400][5];
glm::vec3 asteroid_Calc = spaceshipDir * glm::vec3(a, a, a); glm::vec3 asteroid_Calc = spaceshipDir * glm::vec3(a, a, a);
glm::vec3 asteroid_Pos = spaceshipPos + glm::vec3(0, a, 0) + asteroid_Calc; glm::vec3 asteroid_Pos = spaceshipPos + glm::vec3(0, a, 0) + asteroid_Calc;
glm::vec3 distance = asteroid_Pos - spaceshipPos; glm::vec3 distance = asteroid_Pos - spaceshipPos;
@ -93,6 +95,9 @@ unsigned int textureID;
float tiltAngleSide; float tiltAngleSide;
float tiltAngleUpDown; float tiltAngleUpDown;
int colission = 3;
int star = 0;
double easeInExpo(double x) { double easeInExpo(double x) {
return pow(2, 10 * x - 10); return pow(2, 10 * x - 10);
@ -197,19 +202,36 @@ void generateAsteroids(glm::vec3 asteroid_Pos, glm::vec3 distance, double step)
} }
float speed = 0.03; float speed = 0.03;
float starind = 50; float starind = 50;
bool checkCollision(glm::vec3 objectPos, float objectRadius, glm::vec3 spaceshipPos, float spaceshipRadius, bool isStar) {
float distance = glm::length(objectPos - spaceshipPos);
float starExtraRadius = isStar ? 0.03f : 0.0f;
return distance < objectRadius + spaceshipRadius + starExtraRadius;
}
void generatePlanetoidBelt() { void generatePlanetoidBelt() {
for (int i = 0; i < 400; ++i) { for (int i = 0; i < 400; ++i) {
float z = planetoidsArray[i][0]; float z = planetoidsArray[i][0];
float y = planetoidsArray[i][1]; float y = planetoidsArray[i][1];
float pScale = planetoidsArray[i][2]; float pScale = planetoidsArray[i][2];
bool collision = false; bool collision = false;
planetoidsArray[i][3] -= speed; planetoidsArray[i][3] -= speed;
if (planetoidsArray[i][4] == 1) {
// Planeta ju¿ uczestniczy³a w kolizji, przejdŸ do kolejnej iteracji
continue;
}
if (planetoidsArray[i][3] < -3.f) { if (planetoidsArray[i][3] < -3.f) {
planetoidsArray[i][3] = 10.f; planetoidsArray[i][3] = 10.f;
} }
float x = planetoidsArray[i][3]; float x = planetoidsArray[i][3];
for (int j = 0; j < i; ++j) { for (int j = 0; j < i; ++j) {
float prevZ = planetoidsArray[j][0]; float prevZ = planetoidsArray[j][0];
float prevY = planetoidsArray[j][1]; float prevY = planetoidsArray[j][1];
@ -220,22 +242,63 @@ void generatePlanetoidBelt() {
float sumRadii = pScale + prevScale; float sumRadii = pScale + prevScale;
if (distance < sumRadii) { if (distance < sumRadii) {
collision = true; collision = true;
break; break;
} }
} }
if (!collision) {
if( fmod(i, starind) == 0) {
if (checkCollision(glm::vec3(x, y, z), 0.1f, spaceshipPos, 0.025f,true)) {
// Kolizja z gwiazd¹
std::cout << "Collision with star " << i << std::endl;
planetoidsArray[i][4] = 1;
}
}
else if (checkCollision(glm::vec3(x, y, z), 0.1f, spaceshipPos, 0.025f, true))
{
//kolizja z asteroida
std::cout << "Collision with asteroid " << i << std::endl;
colission--;
planetoidsArray[i][4] = 1;
if (colission == 0)
{
exit(0);
}
}
}
if (!collision) { if (!collision) {
if (fmod(i, starind) == 0) { if (fmod(i, starind) == 0) {
float time = glfwGetTime(); float time = glfwGetTime();
glm::mat4 modelMatrix = glm::translate(glm::vec3(x, y, z)) * glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 1.0f)); glm::mat4 modelMatrix = glm::translate(glm::vec3(x, y, z)) * glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 1.0f));
drawObjectColor(starContext, modelMatrix * glm::eulerAngleX(time) * glm::scale(glm::vec3(0.03f)), glm::vec3(0.2,0.3,0.2)); drawObjectColor(starContext, modelMatrix * glm::eulerAngleX(time) * glm::scale(glm::vec3(0.03f)), glm::vec3(0.2,0.3,0.2));
if (star == 0)
{
star++;
std::cout << "Star " << star << std::endl;
}
} }
else { else {
drawObjectTexture(sphereContext, glm::translate(glm::vec3(x, y, z)) * glm::scale(glm::vec3(pScale)), texture::moon, texture::moonNormal, texture::metalnessSphere, texture::roughnessSphere); drawObjectTexture(sphereContext, glm::translate(glm::vec3(x, y, z)) * glm::scale(glm::vec3(pScale)), texture::moon, texture::moonNormal, texture::metalnessSphere, texture::roughnessSphere);
} }
} }
}
if(star==1)
{
star = 0;
}
else
{
exit(0);
} }
} }
@ -249,6 +312,7 @@ glm::mat4 specshipCameraRotrationMatrix = glm::mat4({
}); });
void renderScene(GLFWwindow* window) void renderScene(GLFWwindow* window)
{ {
glClearColor(0.0f, 0.3f, 0.3f, 1.0f); glClearColor(0.0f, 0.3f, 0.3f, 1.0f);