[FEATURE] collisions!!!!!!!!!!! YAY

This commit is contained in:
Menno
2021-05-28 15:45:46 +02:00
parent 28400fb320
commit e8b3e1b482
9 changed files with 166 additions and 38 deletions

View File

@@ -0,0 +1,40 @@
#include "collision_handler.h"
namespace collision
{
void CheckCollisions(std::vector<entities::CollisionEntity*>& 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;
}
}
}
}
}

View File

@@ -0,0 +1,16 @@
#pragma once
#include <vector>
#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::CollisionEntity*>& entities);
}

View File

@@ -11,7 +11,7 @@ namespace entities
class Entity
{
private:
protected:
models::TexturedModel model;
glm::vec3 position;

View File

@@ -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);
}
}

View File

@@ -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();
};
}

43
src/entities/player.h Normal file
View File

@@ -0,0 +1,43 @@
#pragma once
#include <iostream>
#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;
}
};
}

View File

@@ -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::Entity> 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::Entity*> entities;
std::vector<entities::CollisionEntity*> 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<entities::Light> 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<gui::GuiTexture*> 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;

View File

@@ -19,6 +19,7 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\collision\collision_handler.cpp" />
<ClCompile Include="src\entities\camera.cpp" />
<ClCompile Include="src\entities\collision_entity.cpp" />
<ClCompile Include="src\entities\entity.cpp" />
@@ -34,10 +35,12 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\collision\collision.h" />
<ClInclude Include="src\collision\collision_handler.h" />
<ClInclude Include="src\entities\camera.h" />
<ClInclude Include="src\entities\collision_entity.h" />
<ClInclude Include="src\entities\entity.h" />
<ClInclude Include="src\entities\light.h" />
<ClInclude Include="src\entities\player.h" />
<ClInclude Include="src\gui\gui_element.h" />
<ClInclude Include="src\gui\gui_interactable.h" />
<ClInclude Include="src\models\model.h" />

View File

@@ -51,6 +51,9 @@
<ClCompile Include="src\entities\collision_entity.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\collision\collision_handler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\entities\Camera.h">
@@ -101,5 +104,11 @@
<ClInclude Include="src\collision\collision.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\collision\collision_handler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\entities\player.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>