From 551d53a3dc49a967100d9fc1e3e94828e80efa0e Mon Sep 17 00:00:00 2001 From: Nathalie Seen Date: Tue, 8 Jun 2021 15:25:24 +0200 Subject: [PATCH] [ADD] made collision work --- src/collision/collision_handler.cpp | 66 +++++++++++++++-------------- src/collision/collision_handler.h | 3 +- src/entities/collision_entity.cpp | 4 +- src/entities/collision_entity.h | 7 ++- src/entities/main_character.cpp | 9 +++- src/entities/main_character.h | 2 + src/scenes/in_Game_Scene.cpp | 66 ++++++++++++++++++++--------- 7 files changed, 100 insertions(+), 57 deletions(-) diff --git a/src/collision/collision_handler.cpp b/src/collision/collision_handler.cpp index 312d283..4a82a7f 100644 --- a/src/collision/collision_handler.cpp +++ b/src/collision/collision_handler.cpp @@ -1,40 +1,44 @@ #include "collision_handler.h" +#include namespace collision { - void CheckCollisions(std::vector& entities) + void CheckCollisions(std::vector> entities) { - if (entities.size() == 2) - { - if (entities[0]->IsColliding(*entities[1])) + if (entities.size() < 2) { return; } + if (entities.size() == 2) { - collision::Collision c = { *entities[0], *entities[1] }; - entities[0]->OnCollide(c); - entities[1]->OnCollide(c); + if (entities[0]->IsColliding(*entities[1])) + { + collision::Collision c = { *entities[0], *entities[1] }; + entities[0]->OnCollide(c); + entities[1]->OnCollide(c); + } } - } - - for (int i = 0; i < entities.size() - 2; i++) - { - entities::CollisionEntity* entity = entities[i]; - - for (int j = i + 1; i < entities.size() - 1; j++) - { - entities::CollisionEntity* entity2 = entities[j]; - - if (entity == entity2) - { - continue; - } - - if (entity->IsColliding(*entity2)) - { - collision::Collision c = { *entity, *entity2 }; - entity->OnCollide(c); - entity2->OnCollide(c); - break; - } - } - } + + for (int i = 0; i < entities.size() - 2; i++) + { + std::shared_ptr entity = entities[i]; + + for (int j = i + 1; j < entities.size() - 1; j++) + { + + std::shared_ptr entity2 = entities[j]; + + if (entity == entity2) + { + continue; + } + + if (entity->IsColliding(*entity2)) + { + collision::Collision c = { *entity, *entity2 }; + entity->OnCollide(c); + entity2->OnCollide(c); + break; + } + } + } + } } diff --git a/src/collision/collision_handler.h b/src/collision/collision_handler.h index b9420c8..ea1a67d 100644 --- a/src/collision/collision_handler.h +++ b/src/collision/collision_handler.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include "../entities/collision_entity.h" #include "collision.h" @@ -12,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/entities/collision_entity.cpp b/src/entities/collision_entity.cpp index 0d52e1d..304ffb7 100644 --- a/src/entities/collision_entity.cpp +++ b/src/entities/collision_entity.cpp @@ -14,7 +14,9 @@ namespace entities { if (on_collide != nullptr) { - on_collide(collision); + + on_collide(collision); + } } diff --git a/src/entities/collision_entity.h b/src/entities/collision_entity.h index d1b78cf..a1269d1 100644 --- a/src/entities/collision_entity.h +++ b/src/entities/collision_entity.h @@ -2,6 +2,8 @@ #include "entity.h" #include "../collision/collision.h" +#include +#include namespace entities { @@ -16,7 +18,8 @@ namespace entities glm::vec3 min_xyz; glm::vec3 max_xyz; - void (*on_collide)(const collision::Collision& collision); + //void (*on_collide)(const collision::Collision& collision); + std::function on_collide; public: CollisionEntity(const models::TexturedModel& model, const glm::vec3& position, const glm::vec3& rotation, @@ -52,7 +55,7 @@ namespace entities * * @param function: A function pointer to a function with the collision behaviour */ - void SetCollisionBehaviour(void (*function)(const collision::Collision& collision)) + void SetCollisionBehaviour(std::function function) { if (function != nullptr) { on_collide = function; } } protected: diff --git a/src/entities/main_character.cpp b/src/entities/main_character.cpp index f57959f..83c9ca5 100644 --- a/src/entities/main_character.cpp +++ b/src/entities/main_character.cpp @@ -10,11 +10,12 @@ namespace entities MainCharacter::MainCharacter(const models::TexturedModel& model, const glm::vec3& position, const glm::vec3& rotation, float scale, const collision::Box& bounding_box) : CollisionEntity(model, position, rotation, scale, bounding_box) - {} + { + } glm::vec3 MainCharacter::Move(GLFWwindow* window) { - float movement_speed = -1.0f; //Forward speed adjustment, bee is moving at a standard speedrate + float movement_speed = -0.5f; //Forward speed adjustment, bee is moving at a standard speedrate float down_speed = -1.0f; //Down speed adjustment, downspeed is difference between down_speed and UP_SPEED float side_speed = 0; //Side speed adjustment @@ -65,4 +66,8 @@ namespace entities MoveCollisionBox(); return glm::vec3(side_speed, down_speed, movement_speed ); } + + void MainCharacter::OnCollide(const collision::Collision& collision) { + std::cout << "collide" << std::endl; + } } \ No newline at end of file diff --git a/src/entities/main_character.h b/src/entities/main_character.h index a44e268..7c444c4 100644 --- a/src/entities/main_character.h +++ b/src/entities/main_character.h @@ -32,5 +32,7 @@ namespace entities * @return: Vector with the adjusted side_speed, down_speed, and movement_speed */ glm::vec3 Move(GLFWwindow* window); + + void OnCollide(const collision::Collision& collision) override; }; } diff --git a/src/scenes/in_Game_Scene.cpp b/src/scenes/in_Game_Scene.cpp index 91d7de6..6a17a05 100644 --- a/src/scenes/in_Game_Scene.cpp +++ b/src/scenes/in_Game_Scene.cpp @@ -1,9 +1,11 @@ #include +#include #include #include #include "in_Game_Scene.h" #include "startup_Scene.h" #include "../entities/main_character.h" +#include "../collision/collision_handler.h" #include "../gui/gui_interactable.h" #include "../models/model.h" #include "../renderEngine/loader.h" @@ -20,9 +22,10 @@ namespace scene { std::deque house_models; - std::vector main_character; + std::shared_ptrmain_character; std::deque lights; - std::deque trees; + std::deque trees; + std::vector> collision_entities; models::RawModel raw_model, raw_model_char; models::ModelTexture texture; @@ -44,6 +47,18 @@ namespace scene gui_shader = new shaders::GuiShader(); gui_shader->Init(); } + /** + * temporary!!!! + * just to make some bounding boxes + */ + collision::Box create_bounding_box(glm::vec3 size, glm::vec3 pos, int scale) { + collision::Box box = collision::Box(); + box.size.x = size.z* scale; + box.size.y = size.y* scale; + box.size.z = size.x* scale; + box.center_pos = pos; + return box; + } /** * @brief loads a new chunk in front of the camera, and deletes the chunk behind the camera. @@ -58,13 +73,23 @@ namespace scene { house_models.pop_back(); trees.pop_back(); + collision_entities.erase(collision_entities.begin() + 1); + } int z_offset = model_pos * (model.raw_model.model_size.x * 20); // how much "in the distance" we should load the model house_models.push_front(entities::Entity(model, glm::vec3(0, -50, -50 - z_offset), glm::vec3(0, 90, 0), 20)); - - trees.push_front(entities::Entity(tree, glm::vec3(0, 0, -50 - z_offset), glm::vec3(0, 90, 0), 3)); + collision::Box tree_box = create_bounding_box(tree.raw_model.model_size, glm::vec3(0, 0, -50 - z_offset),3); + std::shared_ptr tree_entity = std::make_shared(tree, glm::vec3(0, 0, -50 - z_offset), glm::vec3(0, 90, 0), 3, tree_box); + trees.push_front(*tree_entity); + collision_entities.push_back(tree_entity); + //std::cout << collision_entities.size() << std::endl; + /*if (collision_entities.size() > 0) { + std::cout << collision_entities[0].get()->GetPosition().z << std::endl; + std::cout << "x: " << main_character->GetPosition().x << "\ny: " << main_character->GetPosition().y << "\nz: " << main_character->GetPosition().z << "\n"; + }*/ } + scene::Scenes scene::In_Game_Scene::start(GLFWwindow* window) @@ -79,18 +104,19 @@ namespace scene models::ModelTexture tree_texture = { render_engine::loader::LoadTexture("res/TreeTexture.png") }; tree = { raw_tree_model, tree_texture }; + 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); + main_character = std::make_shared(model_char, glm::vec3(0, -50, -100), glm::vec3(0, 90, 0), 5, char_box); + collision_entities.push_back(main_character); + // load the first few house models for (int i = 0; i <= UPCOMING_MODEL_AMOUNT; i++) { load_chunk(i); } - raw_model_char = render_engine::LoadObjModel("res/beeTwo.obj"); - models::TexturedModel model_char = { raw_model_char, texture }; - entities::MainCharacter character{ model_char, glm::vec3(0, -50, -100), glm::vec3(0, 90, 0), 5,collision::Box() }; - main_character.push_back(&character); - - lights.push_back(entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5))); // sun + lights.push_back(entities::Light(glm::vec3(0, 1000, 7000), glm::vec3(5, 5, 5))); // sun lights.push_back(entities::Light(glm::vec3(0, 0, -30), glm::vec3(2, 0, 2), glm::vec3(0.0001f, 0.0001f, 0.0001f))); lights.push_back(entities::Light(glm::vec3(0, 0, -200), glm::vec3(0, 2, 0), glm::vec3(0.0001f, 0.0001f, 0.0001f))); @@ -139,10 +165,8 @@ namespace scene { render_engine::renderer::Render(tree_entity, *shader); } - for (entities::Entity* main_char : main_character) - { - render_engine::renderer::Render(*main_char, *shader); - } + + render_engine::renderer::Render(*main_character, *shader); // Render GUI items render_engine::renderer::Render(guis, *gui_shader); @@ -154,16 +178,17 @@ namespace scene void scene::In_Game_Scene::update(GLFWwindow* window) { //camera.Move(window); - entities::MainCharacter* character = main_character[0]; - glm::vec3 movement = character->Move(window); - std::cout << "x: " << character->GetPosition().x << "\ny: " << character->GetPosition().y << "\nz: " << character->GetPosition().z << "\n"; - std::cout << "x get: " << movement.x << "\ny get: " << movement.y << "\nz get: " << movement.z << "\n"; - camera.Follow(character->GetPosition()); - + + glm::vec3 movement = main_character->Move(window); + + //std::cout << "x get: " << movement.x << "\ny get: " << movement.y << "\nz get: " << movement.z << "\n"; + camera.Follow(main_character->GetPosition()); // calculate where the next house model should be loaded static int last_model_pos = 0; int model_pos = -round(camera.GetPosition().z / (model.raw_model.model_size.x * 20)); // how much models we have passed, minus because we are moving in the negative z axis + //std::cout << collision_entities.size() << std::endl; + // if we have passed a model, load a new one and delete the one behind us if (last_model_pos != model_pos) @@ -172,6 +197,7 @@ namespace scene } // remember the position at which the new model was added last_model_pos = model_pos; + collision::CheckCollisions(collision_entities); } void scene::In_Game_Scene::onKey(GLFWwindow* window, int key, int scancode, int action, int mods)