From 476427bce313db899fd1b707b41e87a055d7289a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Ry=C5=BCek?= Date: Thu, 29 Feb 2024 20:50:27 +0100 Subject: [PATCH 1/3] =?UTF-8?q?dodanie=20struktury=20planety,=20przepisani?= =?UTF-8?q?e=20renderu=20jako=20p=C4=99tla=20po=20planets,=20wst=C4=99pne?= =?UTF-8?q?=20collision=20detection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- projekt_grk/src/ex_7_1.hpp | 108 +++++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 4 deletions(-) diff --git a/projekt_grk/src/ex_7_1.hpp b/projekt_grk/src/ex_7_1.hpp index 46c7f72..cc44461 100644 --- a/projekt_grk/src/ex_7_1.hpp +++ b/projekt_grk/src/ex_7_1.hpp @@ -73,6 +73,20 @@ float aspectRatio = 1.f; unsigned int textureID; + +struct Planet { + glm::vec3 currentPos; + glm::vec3 modelScale; + float rotationSpeed; // ruch obrotowy + float orbitSpeed; // ruch obiegowy + float orbitRadius; + GLuint texture; + GLuint normalTexture; +}; + + +std::vector planets; + glm::mat4 createCameraMatrix() { glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir,glm::vec3(0.f,1.f,0.f))); @@ -191,6 +205,8 @@ void drawSpaceShip(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint t } + + void renderPlanet(float time, float orbitRadius, float orbitRotationSpeed, float selfRotationSpeed, float scale, GLuint planetTexture, GLuint normalTexture) { glm::mat4 planetTransform = glm::rotate(glm::mat4(1.0f), time * orbitRotationSpeed, glm::vec3(0, 1, 0)) // orbitowanie dookoła słońca * glm::translate(glm::vec3(orbitRadius, 0, 0)) // translacja na odp. odległość @@ -201,7 +217,8 @@ void renderPlanet(float time, float orbitRadius, float orbitRotationSpeed, float } void renderPlanets() { - float time = glfwGetTime(); + float time = 0; + //float time = glfwGetTime(); float mercuryOrbitRadius = 1.3f; float venusOrbitRadius = 2.0f; @@ -262,8 +279,41 @@ void renderPlanets() { } +void initializePlanets() { + planets.push_back(Planet{ glm::vec3(2.0f, 0, 0), glm::vec3(0.11f), 1 / 59.0f, 1.0f / 2, 2.0f, texture::mercury, texture::mercuryNormal }); + planets.push_back(Planet{ glm::vec3(3.5f, 0, 0), glm::vec3(0.29f), 1 / 243.0f, 1.0f / 5, 3.5f, texture::venus, texture::venusNormal }); + planets.push_back(Planet{ glm::vec3(5.0f, 0, 0), glm::vec3(0.3f), 1.f, 1.0f / 6, 5.0f, texture::earth, texture::earthNormal }); + planets.push_back(Planet{ glm::vec3(7.5f, 0, 0), glm::vec3(0.2f), 1.f, 1.0f / 7, 7.5f, texture::mars, texture::marsNormal }); + planets.push_back(Planet{ glm::vec3(10.f, 0, 0), glm::vec3(1.f), 2.4f, 1.0f / 9, 10.f, texture::jupiter, texture::jupiterNormal }); + planets.push_back(Planet{ glm::vec3(12.5f, 0, 0), glm::vec3(0.7f), 2.4f, 1.0f / 10, 12.5f, texture::saturn, texture::saturnNormal }); + planets.push_back(Planet{ glm::vec3(15.f, 0, 0), glm::vec3(1.f), 1.7f, 1.0f / 15, 15.f, texture::uranus, texture::uranusNormal }); + planets.push_back(Planet{ glm::vec3(20.f, 0, 0), glm::vec3(1.f), 1.2f, 1.0f / 24, 20.f, texture::neptune, texture::neptuneNormal }); + +} + +void printPlanetPos() { + int index = 0; + for (auto& planet : planets) { + std::cout << index << " " << glm::to_string(planet.currentPos) << std::endl; + ++index; + } +} + +bool checkCollision(glm::vec3 spaceshipPos, glm::vec3 planetPos, glm::vec3 planetScale) { + + float planetRadius = planetScale.x; + float shipSize = 0.5f; + + float distanceSquared = glm::distance2(spaceshipPos, planetPos); + float distance = glm::length(planetPos - spaceshipPos); + + float minDistance = planetRadius + shipSize; + return distanceSquared <= (planetRadius + shipSize) * (planetRadius + shipSize); +} void renderScene(GLFWwindow* window) { + //float time = 0; + float time = glfwGetTime(); glClearColor(0.0f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -274,8 +324,30 @@ void renderScene(GLFWwindow* window) drawSun(sphereContext, glm::mat4(), texture::sun); //rendering all the planets - renderPlanets(); + //renderPlanets(); + /*glm::mat4 modelMatrix = glm::eulerAngleY(time / planet.rotationSpeed) + * glm::translate(glm::vec3(planet.orbitRadius, 0, 0)) + * glm::eulerAngleY(time) + * glm::scale(planet.modelScale);*/ + + + for (auto& planet : planets) { + + glm::mat4 modelMatrix = glm::rotate(glm::mat4(1.0f), time * planet.orbitSpeed, glm::vec3(0, 1, 0)) // orbitowanie dookoła słońca + * glm::translate(glm::vec3(planet.orbitRadius, 0, 0)) // translacja na odp. odległość + * glm::rotate(glm::mat4(1.0f), time * planet.rotationSpeed, glm::vec3(0, 1, 0)) //obrót planety wokół własnej osi + * glm::scale(glm::vec3(planet.modelScale)); //skalowanie planety + drawObjectTexture(sphereContext, modelMatrix, planet.texture, planet.normalTexture); + planet.currentPos = glm::vec3(modelMatrix * glm::vec4(0, 0, 0, 1.0f)); + + + } + + drawObjectTexture(sphereContext, + glm::eulerAngleY(time * planets[2].orbitSpeed) * glm::translate(glm::vec3(planets[2].orbitRadius, 0, 0)) * glm::eulerAngleY(time * 0.5f) * glm::translate(glm::vec3(0.8f, 0, 0)) * glm::scale(glm::vec3(0.055f)), + texture::moon, + texture::asteroidNormal); // obliczanie orientacji statku glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f))); @@ -393,6 +465,8 @@ void init(GLFWwindow* window) glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); //skybox end + initializePlanets(); + } void shutdown(GLFWwindow* window) @@ -427,6 +501,9 @@ void processInput(GLFWwindow* window) spaceshipDir = glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(spaceshipDir, 0)); if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) spaceshipDir = glm::vec3(glm::eulerAngleY(-angleSpeed) * glm::vec4(spaceshipDir, 0)); + if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) + spaceshipPos += spaceshipDir * 5*moveSpeed; + cameraPos = spaceshipPos - 1.5 * spaceshipDir + glm::vec3(0, 1, 0) * 0.5f; cameraDir = spaceshipDir; @@ -434,12 +511,35 @@ void processInput(GLFWwindow* window) void renderLoop(GLFWwindow* window) { + + double timeOfLastUpdate = glfwGetTime(); + int loopCount = 0; + int interval = 2500; + while (!glfwWindowShouldClose(window)) { - processInput(window); - renderScene(window); + + for (auto& planet : planets) { + + if (checkCollision(spaceshipPos, planet.currentPos, planet.modelScale)) { + //placeholder + std::cout << "Kolizja statku z planeta " << planet.texture << std::endl; + } + } + + + loopCount++; + + if (loopCount >= interval) { + printPlanetPos(); + std::cout << "Pozycja statku " << glm::to_string(spaceshipPos) << std::endl; + loopCount = 0; + timeOfLastUpdate = glfwGetTime(); + } + glfwPollEvents(); } } + \ No newline at end of file -- 2.20.1 From 83173bfbddf734fc7a2255a666efef8c9b286983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Ry=C5=BCek?= Date: Fri, 1 Mar 2024 00:44:48 +0100 Subject: [PATCH 2/3] =?UTF-8?q?ksi=C4=99=C5=BCyc=20generuje=20sie=20jako?= =?UTF-8?q?=20satelita=20ziemi,=20kolizja=20z=20ksiezycem=20i=20sloncem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- projekt_grk/src/ex_7_1.hpp | 54 +++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/projekt_grk/src/ex_7_1.hpp b/projekt_grk/src/ex_7_1.hpp index cc44461..d691cec 100644 --- a/projekt_grk/src/ex_7_1.hpp +++ b/projekt_grk/src/ex_7_1.hpp @@ -8,6 +8,7 @@ #include "Shader_Loader.h" #include "Render_Utils.h" #include "Texture.h" +//#include "../SpriteRenderer.h" #include "Box.cpp" #include @@ -47,6 +48,8 @@ namespace texture { GLuint grid; + GLuint gameOverSprite; + } @@ -55,6 +58,8 @@ GLuint programSun; GLuint programTex; GLuint programSkyBox; GLuint programSpaceShip; +GLuint programSprite; + Core::Shader_Loader shaderLoader; Core::RenderContext shipContext; @@ -72,6 +77,7 @@ GLuint VAO,VBO; float aspectRatio = 1.f; unsigned int textureID; +bool gameOver = false; struct Planet { @@ -84,8 +90,19 @@ struct Planet { GLuint normalTexture; }; +struct Satellite { + glm::vec3 currentPos; + glm::vec3 modelScale; + float rotationSpeed; // ruch obrotowy + float orbitSpeed; // ruch obiegowy + float orbitRadius; + GLuint texture; + GLuint normalTexture; + Planet parentPlanet; +}; std::vector planets; +std::vector satellites; glm::mat4 createCameraMatrix() { @@ -288,7 +305,8 @@ void initializePlanets() { planets.push_back(Planet{ glm::vec3(12.5f, 0, 0), glm::vec3(0.7f), 2.4f, 1.0f / 10, 12.5f, texture::saturn, texture::saturnNormal }); planets.push_back(Planet{ glm::vec3(15.f, 0, 0), glm::vec3(1.f), 1.7f, 1.0f / 15, 15.f, texture::uranus, texture::uranusNormal }); planets.push_back(Planet{ glm::vec3(20.f, 0, 0), glm::vec3(1.f), 1.2f, 1.0f / 24, 20.f, texture::neptune, texture::neptuneNormal }); - + planets.push_back(Planet{ glm::vec3(0, 0, 0), glm::vec3(1.f), 0.f, 0.f, 0.f, texture::sun, texture::sun }); + satellites.push_back(Satellite{ glm::vec3(5.0f, 0, 0), glm::vec3(0.055f), 1.f / 2, 1.0f / 2, 0.8f, texture::moon, texture::asteroidNormal, planets[2] }); } void printPlanetPos() { @@ -344,10 +362,20 @@ void renderScene(GLFWwindow* window) } - drawObjectTexture(sphereContext, + for (auto& satellite : satellites) { + glm::mat4 modelMatrix = glm::eulerAngleY(time * satellite.parentPlanet.orbitSpeed) + * glm::translate(glm::vec3(satellite.parentPlanet.orbitRadius, 0, 0)) + * glm::eulerAngleY(time * satellite.rotationSpeed) + * glm::translate(glm::vec3(satellite.orbitRadius, 0, 0)) + * glm::scale(glm::vec3(satellite.modelScale)); + drawObjectTexture(sphereContext, modelMatrix, satellite.texture, satellite.normalTexture); + satellite.currentPos = glm::vec3(modelMatrix * glm::vec4(0, 0, 0, 1.0f)); + } + + /*drawObjectTexture(sphereContext, glm::eulerAngleY(time * planets[2].orbitSpeed) * glm::translate(glm::vec3(planets[2].orbitRadius, 0, 0)) * glm::eulerAngleY(time * 0.5f) * glm::translate(glm::vec3(0.8f, 0, 0)) * glm::scale(glm::vec3(0.055f)), texture::moon, - texture::asteroidNormal); + texture::asteroidNormal);*/ // obliczanie orientacji statku glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f))); @@ -469,6 +497,10 @@ void init(GLFWwindow* window) } +void gameOverScreen(GLFWwindow* window) { + // jakies game over dla jego +} + void shutdown(GLFWwindow* window) { shaderLoader.DeleteProgram(program); @@ -520,13 +552,24 @@ void renderLoop(GLFWwindow* window) { { processInput(window); renderScene(window); + int index = 0; for (auto& planet : planets) { if (checkCollision(spaceshipPos, planet.currentPos, planet.modelScale)) { //placeholder - std::cout << "Kolizja statku z planeta " << planet.texture << std::endl; + gameOver = true; + std::cout << "Kolizja statku z planeta " << index << std::endl; } + ++index; + } + for (auto& satellite : satellites) { + + if (checkCollision(spaceshipPos, satellite.currentPos, satellite.modelScale)) { + //placeholder + std::cout << "Kolizja statku z satelita " << index << std::endl; + } + ++index; } @@ -539,6 +582,9 @@ void renderLoop(GLFWwindow* window) { timeOfLastUpdate = glfwGetTime(); } + if (gameOver) { + gameOverScreen(window); + } glfwPollEvents(); } } -- 2.20.1 From 82a1d0ce8af32ea09361990c0c4d4f92a6c2fddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Ry=C5=BCek?= Date: Fri, 1 Mar 2024 16:57:15 +0100 Subject: [PATCH 3/3] zatrzymanie statku po kolizji --- projekt_grk/src/ex_7_1.hpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/projekt_grk/src/ex_7_1.hpp b/projekt_grk/src/ex_7_1.hpp index d691cec..53caafc 100644 --- a/projekt_grk/src/ex_7_1.hpp +++ b/projekt_grk/src/ex_7_1.hpp @@ -8,7 +8,6 @@ #include "Shader_Loader.h" #include "Render_Utils.h" #include "Texture.h" -//#include "../SpriteRenderer.h" #include "Box.cpp" #include @@ -58,7 +57,6 @@ GLuint programSun; GLuint programTex; GLuint programSkyBox; GLuint programSpaceShip; -GLuint programSprite; Core::Shader_Loader shaderLoader; @@ -79,6 +77,8 @@ unsigned int textureID; bool gameOver = false; +float angleSpeed = 0.005f; +float moveSpeed = 0.0025f; struct Planet { glm::vec3 currentPos; @@ -104,6 +104,8 @@ struct Satellite { std::vector planets; std::vector satellites; + + glm::mat4 createCameraMatrix() { glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir,glm::vec3(0.f,1.f,0.f))); @@ -398,6 +400,8 @@ void renderScene(GLFWwindow* window) ); glUseProgram(0); + + glfwSwapBuffers(window); } void framebuffer_size_callback(GLFWwindow* window, int width, int height) @@ -497,8 +501,10 @@ void init(GLFWwindow* window) } + void gameOverScreen(GLFWwindow* window) { - // jakies game over dla jego + moveSpeed = 0; + angleSpeed = 0; } void shutdown(GLFWwindow* window) @@ -512,8 +518,8 @@ void processInput(GLFWwindow* window) glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f))); glm::vec3 spaceshipUp = glm::vec3(0.f, 1.f, 0.f); - float angleSpeed = 0.005f; - float moveSpeed = 0.0025f; + //float angleSpeed = 0.005f; + //float moveSpeed = 0.0025f; if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose(window, true); } @@ -543,8 +549,8 @@ void processInput(GLFWwindow* window) void renderLoop(GLFWwindow* window) { - - double timeOfLastUpdate = glfwGetTime(); + + float timeOfLastUpdate = glfwGetTime(); int loopCount = 0; int interval = 2500; @@ -567,6 +573,7 @@ void renderLoop(GLFWwindow* window) { if (checkCollision(spaceshipPos, satellite.currentPos, satellite.modelScale)) { //placeholder + gameOver = true; std::cout << "Kolizja statku z satelita " << index << std::endl; } ++index; @@ -585,6 +592,7 @@ void renderLoop(GLFWwindow* window) { if (gameOver) { gameOverScreen(window); } + glfwPollEvents(); } } -- 2.20.1