From 625965ed1b3ba6d1ac3c5062c6ba559c81deef45 Mon Sep 17 00:00:00 2001 From: Menno Date: Tue, 8 Jun 2021 12:22:54 +0200 Subject: [PATCH] [ADDED] rotating bounding boxes --- src/collision/collision.h | 13 +++++++++++++ src/entities/HouseGenerator.cpp | 8 ++++++-- src/main.cpp | 11 +++++++++++ src/scenes/in_Game_Scene.cpp | 2 +- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/collision/collision.h b/src/collision/collision.h index d04f974..dadb11f 100644 --- a/src/collision/collision.h +++ b/src/collision/collision.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include "../entities/entity.h" @@ -15,6 +16,18 @@ namespace collision { glm::vec3 center_pos; glm::vec3 size; + + void SetRotation(float angle) + { + double sinTheta = glm::sin(glm::radians(angle)); + double cosTheta = glm::cos(glm::radians(angle)); + + float x = size.x * cosTheta + size.z * sinTheta; + float z = size.z * cosTheta - size.x * sinTheta; + + size.x = x < 0 ? -x : x; + size.z = z < 0 ? -z : z; + } }; /* diff --git a/src/entities/HouseGenerator.cpp b/src/entities/HouseGenerator.cpp index 972ceb7..0767071 100644 --- a/src/entities/HouseGenerator.cpp +++ b/src/entities/HouseGenerator.cpp @@ -6,6 +6,7 @@ #include "../renderEngine/obj_loader.h" #include "../renderEngine/Loader.h" #include "../toolbox/toolbox.h" +#include "collision_entity.h" namespace entities { @@ -27,8 +28,11 @@ namespace entities furniture.push_front(std::make_shared(house_model, position, glm::vec3(0, y_rotation, 0), HOUSE_SIZE)); // Add furniture - models::TexturedModel couch = GetFurnitureModel(FurnitureType::COUCH); - furniture.push_back(std::make_shared(couch, glm::vec3(position.x, position.y + 20, position.z + 10), glm::vec3(0, 0, 0), HOUSE_SIZE)); + models::TexturedModel couch = GetFurnitureModel(FurnitureType::COUCH); + glm::vec3 couchPos = glm::vec3(position.x, position.y + 20, position.z + 10); + collision::Box couchBox = { couchPos, couch.raw_model.model_size }; + couchBox.SetRotation(90); + furniture.push_back(std::make_shared(couch, couchPos, glm::vec3(0, 0, 0), HOUSE_SIZE, couchBox)); return furniture; } diff --git a/src/main.cpp b/src/main.cpp index cf20294..b6f5604 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ #include #include +#include "collision/collision.h" #include "gui/gui_interactable.h" #include "models/model.h" #include "renderEngine/loader.h" @@ -36,6 +37,16 @@ scene::Scene* current_scene; int main(void) { + + collision::Box box = { glm::vec3(0, 0, 0), glm::vec3(5, 2, 10) }; + box.SetRotation(180); + std::cout << box.size.x << std::endl; + std::cout << box.size.y << std::endl; + std::cout << box.size.z << std::endl; + + return 0; + + #pragma region OPENGL_SETTINGS if (!glfwInit()) throw "Could not inditialize glwf"; diff --git a/src/scenes/in_Game_Scene.cpp b/src/scenes/in_Game_Scene.cpp index 3706ab5..9c3284d 100644 --- a/src/scenes/in_Game_Scene.cpp +++ b/src/scenes/in_Game_Scene.cpp @@ -162,7 +162,7 @@ namespace scene void scene::In_Game_Scene::onKey(GLFWwindow* window, int key, int scancode, int action, int mods) { - if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { return_value = scene::Scenes::STOP; }