From 022c7eb1f0886dc66bbbd458e1a15685409c96ec Mon Sep 17 00:00:00 2001 From: "DESKTOP-EBR7IVA\\kimve" Date: Fri, 18 Jun 2021 11:20:24 +0200 Subject: [PATCH 1/2] [WIP] collision fixing --- src/entities/collision_entity.cpp | 5 ++++- src/entities/collision_entity.h | 2 +- src/entities/house_generator.cpp | 11 ++++++----- src/entities/house_generator.h | 3 ++- src/entities/main_character.cpp | 2 +- src/main.cpp | 17 +++++++++++++++++ src/scenes/game_Over_Scene.cpp | 1 - src/scenes/in_Game_Scene.cpp | 14 ++++++-------- src/scenes/startup_Scene.cpp | 2 +- 9 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/entities/collision_entity.cpp b/src/entities/collision_entity.cpp index 304ffb7..3418d3d 100644 --- a/src/entities/collision_entity.cpp +++ b/src/entities/collision_entity.cpp @@ -40,7 +40,10 @@ namespace entities const glm::vec3 size = bounding_box.size; + /*min_xyz = { bounding_box.center_pos.x - (0.5 * size.x), bounding_box.center_pos.y, bounding_box.center_pos.z + (0.5 * size.z) }; + max_xyz = { bounding_box.center_pos.x + (0.5 * size.x), bounding_box.center_pos.y + size.y, bounding_box.center_pos.z - (0.5 * size.z) };*/ + min_xyz = bounding_box.center_pos; - max_xyz = glm::vec3(min_xyz.x + size.x, min_xyz.y + size.y, min_xyz.z + size.z); + max_xyz = { bounding_box.center_pos.x + size.x, bounding_box.center_pos.y + size.y, bounding_box.center_pos.z - size.z }; } } diff --git a/src/entities/collision_entity.h b/src/entities/collision_entity.h index a1269d1..1eb8ee4 100644 --- a/src/entities/collision_entity.h +++ b/src/entities/collision_entity.h @@ -58,7 +58,7 @@ namespace entities void SetCollisionBehaviour(std::function function) { if (function != nullptr) { on_collide = function; } } - protected: + public: /* * @brief: This method moves the collision to the center of the entity diff --git a/src/entities/house_generator.cpp b/src/entities/house_generator.cpp index 6224dde..6d5ef96 100644 --- a/src/entities/house_generator.cpp +++ b/src/entities/house_generator.cpp @@ -20,12 +20,13 @@ namespace entities GenerateFurnitureModels(); } - std::deque> HouseGenerator::GenerateHouse(const glm::vec3& position, float y_rotation) + std::deque> HouseGenerator::GenerateHouse(const glm::vec3& position, float y_rotation) { - std::deque> furniture; + std::deque> furniture; // Add house - furniture.push_front(std::make_shared(house_model, position, glm::vec3(0, y_rotation, 0), HOUSE_SIZE)); + collision::Box house_box = { position, glm::vec3(0,0,0) }; + furniture.push_front(std::make_shared(house_model, position, glm::vec3(0, y_rotation, 0), HOUSE_SIZE, house_box)); for(int i = 0; i(misc, misc_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, misc_box)); - */ + return furniture; } diff --git a/src/entities/house_generator.h b/src/entities/house_generator.h index ab5e4f1..fcd734d 100644 --- a/src/entities/house_generator.h +++ b/src/entities/house_generator.h @@ -5,6 +5,7 @@ #include #include "../models/Model.h" #include "../collision/collision.h" +#include "collision_entity.h" namespace entities { @@ -44,7 +45,7 @@ namespace entities * * @return: A list with all the entities of the generated house (the furniture) */ - std::deque> GenerateHouse(const glm::vec3& position, float y_rotation); + std::deque> GenerateHouse(const glm::vec3& position, float y_rotation); /* * @brief: Returns the depth of the house (chunk) diff --git a/src/entities/main_character.cpp b/src/entities/main_character.cpp index b366b89..c37e00c 100644 --- a/src/entities/main_character.cpp +++ b/src/entities/main_character.cpp @@ -22,7 +22,7 @@ namespace entities void MainCharacter::Move(GLFWwindow* window) { if (is_playing) { - movement_speed = -0.5f; //Forward speed adjustment, bee is moving at a standard speedrate + movement_speed = -2.0f; //Forward speed adjustment, bee is moving at a standard speedrate down_speed = -1.0f; //Down speed adjustment, downspeed is difference between down_speed and UP_SPEED side_speed = 0; //Side speed adjustment diff --git a/src/main.cpp b/src/main.cpp index 472c320..a799195 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,6 +28,7 @@ #include "scenes/in_Game_Scene.h" #include "scenes/startup_Scene.h" #include "scenes/game_Over_Scene.h" +#include "entities/collision_entity.h" #include "computervision/ObjectDetection.h" //#include "computervision/OpenPoseImage.h" @@ -59,6 +60,22 @@ void retrieve_points(std::vector arm_points, cv::Mat points_on_image) int main(void) { + collision::Box house_box_1 = { glm::vec3(-50, 0,0), glm::vec3(5,5,5) }; + collision::Box house_box_2 = { glm::vec3(50, 0,0), glm::vec3(5,5,5) }; + entities::CollisionEntity ent1({ {0,0}, {0} }, glm::vec3(-50, 0, 0), glm::vec3(0, 0, 0), 1, house_box_1); + entities::CollisionEntity ent2({ {0,0}, {0} }, glm::vec3(50, 0, 0), glm::vec3(0, 0, 0), 1, house_box_2); + while (true){ + house_box_1.center_pos += 1; + ent1.IncreasePosition(glm::vec3(1, 0, 0)); + ent1.MoveCollisionBox(); + ent2.IncreasePosition(glm::vec3(-1, 0, 0)); + ent2.MoveCollisionBox(); + + if (ent1.IsColliding(ent2)) { + std::cout << "entities are colliding!! " << ent1.GetPosition().x << " : " << ent2.GetPosition().x << std::endl; + } + } + return 0; #pragma region OPENGL_SETTINGS if (!glfwInit()) throw "Could not inditialize glwf"; diff --git a/src/scenes/game_Over_Scene.cpp b/src/scenes/game_Over_Scene.cpp index 5fb9135..3832cb9 100644 --- a/src/scenes/game_Over_Scene.cpp +++ b/src/scenes/game_Over_Scene.cpp @@ -181,5 +181,4 @@ namespace scene hand_mode_gameOver = !hand_mode_gameOver; } } - } \ No newline at end of file diff --git a/src/scenes/in_Game_Scene.cpp b/src/scenes/in_Game_Scene.cpp index 0252fe5..71506a3 100644 --- a/src/scenes/in_Game_Scene.cpp +++ b/src/scenes/in_Game_Scene.cpp @@ -32,8 +32,6 @@ namespace scene std::shared_ptrmain_character; std::vector> collision_entities; - //std::deque> furniture_collision; - entities::HouseGenerator* house_generator; std::deque> house_models; @@ -132,14 +130,16 @@ namespace scene for (int i = 0; i < furniture_count; i++) { house_models.pop_front(); + collision_entities.pop_back(); } } int z_offset = model_pos * (house_generator->GetHouseDepth()); // how much "in the distance" we should load the model - std::deque> furniture = house_generator->GenerateHouse(glm::vec3(0, -75, -50 - z_offset), 90); + std::deque> furniture = house_generator->GenerateHouse(glm::vec3(0, -75, -50 - z_offset), 90); furniture_count = furniture.size(); house_models.insert(house_models.end(), furniture.begin(), furniture.end()); + collision_entities.insert(collision_entities.end(), furniture.begin(), furniture.end()); std::cout << "funriture_count in load chunk (house included): " << furniture_count << std::endl; furniture_count_old = furniture_count - 1; @@ -154,7 +154,6 @@ namespace scene texture.shine_damper = 10; texture.reflectivity = 0; - raw_model_char = render_engine::LoadObjModel("res/beeTwo.obj"); models::TexturedModel model_char = { raw_model_char, texture }; collision::Box char_box = create_bounding_box(raw_model_char.model_size, glm::vec3(0, 0, 0), 1); @@ -230,7 +229,6 @@ namespace scene break; } - glfwSwapBuffers(window); glfwPollEvents(); } @@ -279,6 +277,8 @@ namespace scene return_value = scene::Scenes::GAMEOVER; } + std::cout << "Pos of main char: " << main_character.get()->GetPosition().z << std::endl; + std::cout << "Pos z of collision entity: "<< collision_entities.at(0).get()->GetPosition().z << std::endl; //std::cout << "x get: " << movement.x << "\ny get: " << movement.y << "\nz get: " << movement.z << "\n"; camera->Follow(main_character->GetPosition()); @@ -293,16 +293,14 @@ namespace scene score += furniture_count_old; std::cout << "Score: " << score << std::endl; std::cout << "Furniture_count_old in model (house excluded): " << furniture_count_old << std::endl; - } // remember the position at which the new model was added last_model_pos = model_pos; + std::cout << "amount of collision entities: " << collision_entities.size() << std::endl; collision::CheckCollisions(collision_entities); update_hand_detection(); - - } //manages the key input in the game scene diff --git a/src/scenes/startup_Scene.cpp b/src/scenes/startup_Scene.cpp index 05698ce..b734214 100644 --- a/src/scenes/startup_Scene.cpp +++ b/src/scenes/startup_Scene.cpp @@ -211,7 +211,7 @@ namespace scene { if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { - return_value = scene::Scenes::GAMEOVER; + return_value = scene::Scenes::INGAME; cv::destroyWindow("camera"); } else if (glfwGetKey(window, GLFW_KEY_BACKSPACE) == GLFW_PRESS) { From c0a099a05e1946c83ca126abb6a8ac05bb6d165a Mon Sep 17 00:00:00 2001 From: Menno Date: Fri, 18 Jun 2021 12:32:45 +0200 Subject: [PATCH 2/2] [FIXED] collisions --- src/collision/collision_handler.cpp | 4 +- src/collision/collision_handler.h | 2 +- .../async/StaticCameraInstance.h | 2 +- src/entities/collision_entity.cpp | 8 +- src/entities/house_generator.cpp | 106 +++++++++--------- src/main.cpp | 18 +-- src/scenes/in_Game_Scene.cpp | 4 +- 7 files changed, 63 insertions(+), 81 deletions(-) diff --git a/src/collision/collision_handler.cpp b/src/collision/collision_handler.cpp index 4a82a7f..6859cab 100644 --- a/src/collision/collision_handler.cpp +++ b/src/collision/collision_handler.cpp @@ -3,7 +3,7 @@ namespace collision { - void CheckCollisions(std::vector> entities) + void CheckCollisions(std::vector>& entities) { if (entities.size() < 2) { return; } if (entities.size() == 2) @@ -15,7 +15,7 @@ namespace collision entities[1]->OnCollide(c); } } - + for (int i = 0; i < entities.size() - 2; i++) { std::shared_ptr entity = entities[i]; diff --git a/src/collision/collision_handler.h b/src/collision/collision_handler.h index ea1a67d..8b6d556 100644 --- a/src/collision/collision_handler.h +++ b/src/collision/collision_handler.h @@ -13,5 +13,5 @@ namespace collision * * @param entities: A list with all the collision entities. */ - void CheckCollisions(std::vector> entities); + void CheckCollisions(std::vector>& entities); } \ No newline at end of file diff --git a/src/computervision/async/StaticCameraInstance.h b/src/computervision/async/StaticCameraInstance.h index 625d478..93e4725 100644 --- a/src/computervision/async/StaticCameraInstance.h +++ b/src/computervision/async/StaticCameraInstance.h @@ -6,7 +6,7 @@ namespace static_camera static cv::VideoCapture getCap() { - static cv::VideoCapture cap(0); + static cv::VideoCapture cap(1); return cap; } }; diff --git a/src/entities/collision_entity.cpp b/src/entities/collision_entity.cpp index 3418d3d..ef22775 100644 --- a/src/entities/collision_entity.cpp +++ b/src/entities/collision_entity.cpp @@ -40,10 +40,10 @@ namespace entities const glm::vec3 size = bounding_box.size; - /*min_xyz = { bounding_box.center_pos.x - (0.5 * size.x), bounding_box.center_pos.y, bounding_box.center_pos.z + (0.5 * size.z) }; - max_xyz = { bounding_box.center_pos.x + (0.5 * size.x), bounding_box.center_pos.y + size.y, bounding_box.center_pos.z - (0.5 * size.z) };*/ + min_xyz = { bounding_box.center_pos.x - (0.5 * size.x), bounding_box.center_pos.y, bounding_box.center_pos.z - (0.5 * size.z) }; + max_xyz = { bounding_box.center_pos.x + (0.5 * size.x), bounding_box.center_pos.y + size.y, bounding_box.center_pos.z + (0.5 * size.z) }; - min_xyz = bounding_box.center_pos; - max_xyz = { bounding_box.center_pos.x + size.x, bounding_box.center_pos.y + size.y, bounding_box.center_pos.z - size.z }; + // min_xyz = bounding_box.center_pos; + // max_xyz = { bounding_box.center_pos.x + size.x, bounding_box.center_pos.y + size.y, bounding_box.center_pos.z + size.z }; } } diff --git a/src/entities/house_generator.cpp b/src/entities/house_generator.cpp index 6d5ef96..568b792 100644 --- a/src/entities/house_generator.cpp +++ b/src/entities/house_generator.cpp @@ -28,66 +28,66 @@ namespace entities collision::Box house_box = { position, glm::vec3(0,0,0) }; furniture.push_front(std::make_shared(house_model, position, glm::vec3(0, y_rotation, 0), HOUSE_SIZE, house_box)); - for(int i = 0; i(model, model_pos, glm::vec3(0, -90, 0), HOUSE_SIZE * 2, model_box)); - //furniture_collision.push_back(std::make_shared(model, model_pos, glm::vec3(0, -90, 0), HOUSE_SIZE * 2, model_box)); - - } + // for(int i = 0; i(model, model_pos, glm::vec3(0, -90, 0), HOUSE_SIZE * 2, model_box)); + // //furniture_collision.push_back(std::make_shared(model, model_pos, glm::vec3(0, -90, 0), HOUSE_SIZE * 2, model_box)); + // + // } //furniture_collision.pop_front(); // Add furniture - models::TexturedModel couch = GetFurnitureModel(FurnitureType::COUCH); - glm::vec3 couch_pos = glm::vec3(position.x + 200, position.y, position.z + 10); - collision::Box couch_box = { couch_pos, couch.raw_model.model_size }; - couch_box.SetRotation(-90); - furniture.push_back(std::make_shared(couch, couch_pos, glm::vec3(0, -90, 0), HOUSE_SIZE * 2, couch_box)); - - models::TexturedModel table = GetFurnitureModel(FurnitureType::TABLE); - glm::vec3 table_pos = glm::vec3(position.x - 30, position.y, position.z); - collision::Box table_box = { table_pos, table.raw_model.model_size }; - furniture.push_back(std::make_shared(table, table_pos, glm::vec3(0, 0, 0), HOUSE_SIZE * 1.3, table_box)); - - models::TexturedModel chair = GetFurnitureModel(FurnitureType::CHAIR); - glm::vec3 chair_pos = glm::vec3(position.x - 50, position.y, position.z + 220); - collision::Box chair_box = { chair_pos, chair.raw_model.model_size }; - furniture.push_back(std::make_shared(chair, chair_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, chair_box)); - - models::TexturedModel plant = GetFurnitureModel(FurnitureType::PLANT); - glm::vec3 plant_pos = glm::vec3(position.x - 50, position.y, position.z + 220); - collision::Box plant_box = { plant_pos, plant.raw_model.model_size }; - furniture.push_back(std::make_shared(plant, plant_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, plant_box)); - - models::TexturedModel guitar = GetFurnitureModel(FurnitureType::GUITAR); - glm::vec3 guitar_pos = glm::vec3(position.x - 50, position.y, position.z + 220); - collision::Box guitar_box = { guitar_pos, guitar.raw_model.model_size }; - furniture.push_back(std::make_shared(guitar, guitar_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, guitar_box)); - - models::TexturedModel bookshelf = GetFurnitureModel(FurnitureType::BOOKSHELF); - glm::vec3 bookshelf_pos = glm::vec3(position.x - 50, position.y, position.z + 220); - collision::Box bookshelf_box = { bookshelf_pos, bookshelf.raw_model.model_size }; - furniture.push_back(std::make_shared(bookshelf, bookshelf_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, bookshelf_box)); - - models::TexturedModel lamp = GetFurnitureModel(FurnitureType::LAMP); - glm::vec3 lamp_pos = glm::vec3(position.x - 50, position.y, position.z + 220); - collision::Box lamp_box = { lamp_pos, lamp.raw_model.model_size }; - furniture.push_back(std::make_shared(lamp, lamp_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, lamp_box)); - - models::TexturedModel ceiling_object = GetFurnitureModel(FurnitureType::CEILING_OBJECTS); - glm::vec3 ceiling_object_pos = glm::vec3(position.x - 50, position.y, position.z + 220); - collision::Box ceiling_object_box = { ceiling_object_pos, ceiling_object.raw_model.model_size }; - furniture.push_back(std::make_shared(ceiling_object, ceiling_object_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, ceiling_object_box)); + // models::TexturedModel couch = GetFurnitureModel(FurnitureType::COUCH); + // glm::vec3 couch_pos = glm::vec3(position.x + 200, position.y, position.z + 10); + // collision::Box couch_box = { couch_pos, couch.raw_model.model_size * (HOUSE_SIZE) }; + // couch_box.SetRotation(-90); + // furniture.push_back(std::make_shared(couch, couch_pos, glm::vec3(0, -90, 0), HOUSE_SIZE * 2, couch_box)); + // + // models::TexturedModel table = GetFurnitureModel(FurnitureType::TABLE); + // glm::vec3 table_pos = glm::vec3(position.x - 30, position.y, position.z); + // collision::Box table_box = { table_pos, table.raw_model.model_size * ((HOUSE_SIZE) * 1.3f) }; + // furniture.push_back(std::make_shared(table, table_pos, glm::vec3(0, 0, 0), HOUSE_SIZE * 1.3, table_box)); + // + // models::TexturedModel chair = GetFurnitureModel(FurnitureType::CHAIR); + // glm::vec3 chair_pos = glm::vec3(position.x - 50, position.y, position.z + 220); + // collision::Box chair_box = { chair_pos, chair.raw_model.model_size * (HOUSE_SIZE) }; + // furniture.push_back(std::make_shared(chair, chair_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, chair_box)); + // + // models::TexturedModel plant = GetFurnitureModel(FurnitureType::PLANT); + // glm::vec3 plant_pos = glm::vec3(position.x - 50, position.y, position.z + 220); + // collision::Box plant_box = { plant_pos, plant.raw_model.model_size * (HOUSE_SIZE) }; + // furniture.push_back(std::make_shared(plant, plant_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, plant_box)); + // + // models::TexturedModel guitar = GetFurnitureModel(FurnitureType::GUITAR); + // glm::vec3 guitar_pos = glm::vec3(position.x - 50, position.y, position.z + 220); + // collision::Box guitar_box = { guitar_pos, guitar.raw_model.model_size * (HOUSE_SIZE) }; + // furniture.push_back(std::make_shared(guitar, guitar_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, guitar_box)); + // + // models::TexturedModel bookshelf = GetFurnitureModel(FurnitureType::BOOKSHELF); + // glm::vec3 bookshelf_pos = glm::vec3(position.x - 50, position.y, position.z + 220); + // collision::Box bookshelf_box = { bookshelf_pos, bookshelf.raw_model.model_size * (HOUSE_SIZE) }; + // furniture.push_back(std::make_shared(bookshelf, bookshelf_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, bookshelf_box)); + // + // models::TexturedModel lamp = GetFurnitureModel(FurnitureType::LAMP); + // glm::vec3 lamp_pos = glm::vec3(position.x - 50, position.y, position.z + 220); + // collision::Box lamp_box = { lamp_pos, lamp.raw_model.model_size * (HOUSE_SIZE) }; + // furniture.push_back(std::make_shared(lamp, lamp_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, lamp_box)); + // + // models::TexturedModel ceiling_object = GetFurnitureModel(FurnitureType::CEILING_OBJECTS); + // glm::vec3 ceiling_object_pos = glm::vec3(position.x - 50, position.y, position.z + 220); + // collision::Box ceiling_object_box = { ceiling_object_pos, ceiling_object.raw_model.model_size * (HOUSE_SIZE / 2) }; + // furniture.push_back(std::make_shared(ceiling_object, ceiling_object_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, ceiling_object_box)); models::TexturedModel misc = GetFurnitureModel(FurnitureType::MISC); - glm::vec3 misc_pos = glm::vec3(position.x - 50, position.y, position.z + 220); - collision::Box misc_box = { misc_pos, misc.raw_model.model_size }; + glm::vec3 misc_pos = glm::vec3(position.x - 50, position.y - 10, position.z + 220); + collision::Box misc_box = { misc_pos, misc.raw_model.model_size * (HOUSE_SIZE) }; furniture.push_back(std::make_shared(misc, misc_pos, glm::vec3(0, 0, 0), HOUSE_SIZE, misc_box)); return furniture; diff --git a/src/main.cpp b/src/main.cpp index a799195..6b94b69 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -60,22 +60,6 @@ void retrieve_points(std::vector arm_points, cv::Mat points_on_image) int main(void) { - collision::Box house_box_1 = { glm::vec3(-50, 0,0), glm::vec3(5,5,5) }; - collision::Box house_box_2 = { glm::vec3(50, 0,0), glm::vec3(5,5,5) }; - entities::CollisionEntity ent1({ {0,0}, {0} }, glm::vec3(-50, 0, 0), glm::vec3(0, 0, 0), 1, house_box_1); - entities::CollisionEntity ent2({ {0,0}, {0} }, glm::vec3(50, 0, 0), glm::vec3(0, 0, 0), 1, house_box_2); - while (true){ - house_box_1.center_pos += 1; - ent1.IncreasePosition(glm::vec3(1, 0, 0)); - ent1.MoveCollisionBox(); - ent2.IncreasePosition(glm::vec3(-1, 0, 0)); - ent2.MoveCollisionBox(); - - if (ent1.IsColliding(ent2)) { - std::cout << "entities are colliding!! " << ent1.GetPosition().x << " : " << ent2.GetPosition().x << std::endl; - } - } - return 0; #pragma region OPENGL_SETTINGS if (!glfwInit()) throw "Could not inditialize glwf"; @@ -159,4 +143,4 @@ static double UpdateDelta() double delt_time = current_time - last_frame_time; last_frame_time = current_time; return delt_time; -} +} \ No newline at end of file diff --git a/src/scenes/in_Game_Scene.cpp b/src/scenes/in_Game_Scene.cpp index 71506a3..6627d20 100644 --- a/src/scenes/in_Game_Scene.cpp +++ b/src/scenes/in_Game_Scene.cpp @@ -274,11 +274,9 @@ namespace scene main_character->Move(window); if (!main_character.get()->GetOnCollide()) { - return_value = scene::Scenes::GAMEOVER; + //return_value = scene::Scenes::GAMEOVER; } - std::cout << "Pos of main char: " << main_character.get()->GetPosition().z << std::endl; - std::cout << "Pos z of collision entity: "<< collision_entities.at(0).get()->GetPosition().z << std::endl; //std::cout << "x get: " << movement.x << "\ny get: " << movement.y << "\nz get: " << movement.z << "\n"; camera->Follow(main_character->GetPosition());