diff --git a/src/collision/collision_handler.cpp b/src/collision/collision_handler.cpp new file mode 100644 index 0000000..312d283 --- /dev/null +++ b/src/collision/collision_handler.cpp @@ -0,0 +1,40 @@ +#include "collision_handler.h" + +namespace collision +{ + void CheckCollisions(std::vector& entities) + { + if (entities.size() == 2) + { + 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; + } + } + } + } +} diff --git a/src/collision/collision_handler.h b/src/collision/collision_handler.h new file mode 100644 index 0000000..b9420c8 --- /dev/null +++ b/src/collision/collision_handler.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include "../entities/collision_entity.h" +#include "collision.h" + +namespace collision +{ + /* + * @brief: This function will check all the collision entities for + * collisions and call the OnCollide function when a entity collides. + * + * @param entities: A list with all the collision entities. + */ + void CheckCollisions(std::vector& entities); +} \ No newline at end of file diff --git a/src/entities/Entity.h b/src/entities/Entity.h index 957b34b..3fd3c2b 100644 --- a/src/entities/Entity.h +++ b/src/entities/Entity.h @@ -11,7 +11,7 @@ namespace entities class Entity { - private: + protected: models::TexturedModel model; glm::vec3 position; diff --git a/src/entities/collision_entity.cpp b/src/entities/collision_entity.cpp index da5a2cd..0d52e1d 100644 --- a/src/entities/collision_entity.cpp +++ b/src/entities/collision_entity.cpp @@ -7,11 +7,7 @@ namespace entities : Entity(model, position, rotation, scale), bounding_box(bounding_box) { - const glm::vec3& center = bounding_box.center_pos; - const glm::vec3& size = bounding_box.size; - - min_xyz = glm::vec3(center.x - size.x, center.y - size.y, center.z - size.z); - max_xyz = glm::vec3(center.x + size.x, center.y + size.y, center.z + size.z); + MoveCollisionBox(); } void CollisionEntity::OnCollide(const collision::Collision& collision) @@ -35,4 +31,14 @@ namespace entities (min_xyz.y <= e.max_xyz.y && max_xyz.y >= e.min_xyz.y) && (min_xyz.z <= e.max_xyz.z && max_xyz.z >= e.min_xyz.z); } + + void CollisionEntity::MoveCollisionBox() + { + bounding_box.center_pos = position; + + const glm::vec3 size = bounding_box.size; + + 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); + } } diff --git a/src/entities/collision_entity.h b/src/entities/collision_entity.h index 445fbb7..d1b78cf 100644 --- a/src/entities/collision_entity.h +++ b/src/entities/collision_entity.h @@ -10,7 +10,7 @@ namespace entities */ class CollisionEntity : public Entity { - private: + public: collision::Box bounding_box; glm::vec3 min_xyz; @@ -54,5 +54,12 @@ namespace entities */ void SetCollisionBehaviour(void (*function)(const collision::Collision& collision)) { if (function != nullptr) { on_collide = function; } } + + protected: + + /* + * @brief: This method moves the collision to the center of the entity + */ + void MoveCollisionBox(); }; } \ No newline at end of file diff --git a/src/entities/player.h b/src/entities/player.h new file mode 100644 index 0000000..ea5424d --- /dev/null +++ b/src/entities/player.h @@ -0,0 +1,43 @@ +#pragma once + +#include +#include "collision_entity.h" + +namespace entities +{ + class Player : public CollisionEntity + { + public: + Player(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) + {} + + void Update() + { + position.x += 0.11f; + MoveCollisionBox(); + } + + void OnCollide(const ::collision::Collision& collision) override + { + std::cout << "Player got HIT" << std::endl; + } + }; + + class Player2 : public CollisionEntity + { + public: + Player2(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) + {} + + void Update() { position.x -= 0.01f; MoveCollisionBox(); } + + void OnCollide(const ::collision::Collision& collision) override + { + std::cout << "Player2 got HIT" << std::endl; + } + }; +} diff --git a/src/main.cpp b/src/main.cpp index bd57547..86ccdc0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,6 +16,9 @@ #include "renderEngine/renderer.h" #include "shaders/entity_shader.h" #include "toolbox/toolbox.h" +#include "entities/collision_entity.h" +#include "entities/player.h" +#include "collision/collision_handler.h" #pragma comment(lib, "glfw3.lib") #pragma comment(lib, "glew32s.lib") @@ -55,42 +58,41 @@ int main(void) texture.reflectivity = 0; models::TexturedModel model = { raw_model, texture }; - /** - * load and add some models (in this case some level sections) to the entities list. - * */ - std::vector entities; - int z = 0; - for (int i = 0; i < 5; ++i) - { - entities.push_back(entities::Entity(model, glm::vec3(0, -50, -50 - z), glm::vec3(0, 90, 0), 20)); - z += (raw_model.model_size.x * 20); - } + + // load and add some models (in this case some level sections) to the entities list. + std::vector entities; + std::vector collision_entities; + + // int z = 0; + // for (int i = 0; i < 5; ++i) + // { + // entities.push_back(entities::Entity(model, glm::vec3(0, -50, -50 - z), glm::vec3(0, 90, 0), 20)); + // z += (raw_model.model_size.x * 20); + // } std::vector lights; lights.push_back(entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5))); 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))); + + // Collision testing + entities::Player player(model, glm::vec3(0, 0, 0), glm::vec3(0, 0, 0), 1, { {0, 0, 0}, raw_model.model_size }); + entities.push_back(&player); + collision_entities.push_back(&player); + + entities::Player2 player2(model, glm::vec3(50, 0, 0), glm::vec3(0, 0, 0), 1, { {50, 0, 0}, raw_model.model_size }); + entities.push_back(&player2); + collision_entities.push_back(&player2); + + shaders::EntityShader shader; shader.Init(); render_engine::renderer::Init(shader); - entities::Camera camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0)); + entities::Camera camera(glm::vec3(40, 10, 80), glm::vec3(0, 0, 0)); - // GUI stuff - shaders::GuiShader gui_shader; - gui_shader.Init(); - - std::vector guis; - gui::Button button(render_engine::loader::LoadTexture("res/Mayo.png"), glm::vec2(0.5f, 0.0f), glm::vec2(0.25f, 0.25f)); - button.SetHoverTexture(render_engine::loader::LoadTexture("res/Texture.png")); - button.SetClickedTexture(render_engine::loader::LoadTexture("res/Mayo.png")); - button.SetOnClickAction([]() - { - std::cout << "I got clicked on!" << std::endl; - }); - guis.push_back(&button); // Main game loop @@ -99,7 +101,13 @@ int main(void) // Update const double delta = UpdateDelta(); camera.Move(window); - button.Update(window); + + + player.Update(); + player2.Update(); + + collision::CheckCollisions(collision_entities); + // Render render_engine::renderer::Prepare(); @@ -111,17 +119,14 @@ int main(void) shader.LoadViewMatrix(camera); // Renders each entity in the entities list - for (entities::Entity& entity : entities) + for (entities::Entity* entity : entities) { - render_engine::renderer::Render(entity, shader); + render_engine::renderer::Render(*entity, shader); } // Stop rendering the entities shader.Stop(); - // Render GUI items - render_engine::renderer::Render(guis, gui_shader); - // Finish up glfwSwapBuffers(window); glfwPollEvents(); @@ -129,7 +134,6 @@ int main(void) // Clean up shader.CleanUp(); - gui_shader.CleanUp(); render_engine::loader::CleanUp(); glfwTerminate(); return 0; diff --git a/wk2_fps.vcxproj b/wk2_fps.vcxproj index 384ca2e..012e9bb 100644 --- a/wk2_fps.vcxproj +++ b/wk2_fps.vcxproj @@ -19,6 +19,7 @@ + @@ -34,10 +35,12 @@ + + diff --git a/wk2_fps.vcxproj.filters b/wk2_fps.vcxproj.filters index df437dd..5d59244 100644 --- a/wk2_fps.vcxproj.filters +++ b/wk2_fps.vcxproj.filters @@ -51,6 +51,9 @@ Source Files + + Source Files + @@ -101,5 +104,11 @@ Header Files + + Header Files + + + Header Files + \ No newline at end of file