diff --git a/src/collision/collision.h b/src/collision/collision.h new file mode 100644 index 0000000..d04f974 --- /dev/null +++ b/src/collision/collision.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include "../entities/entity.h" + +namespace collision +{ + /* + * This structure represents a collision box inside the world. + * + * center_pos: The center position of the collision box + * size: The size in each axis of the collision box + */ + struct Box + { + glm::vec3 center_pos; + glm::vec3 size; + }; + + /* + * This structure represents a collision between 2 entities + * + * entity1: The first entity + * entity2: The second entity + */ + struct Collision + { + entities::Entity& entity1; + entities::Entity& entity2; + }; +} \ No newline at end of file diff --git a/src/entities/Entity.h b/src/entities/Entity.h index ca09fe0..957b34b 100644 --- a/src/entities/Entity.h +++ b/src/entities/Entity.h @@ -13,6 +13,7 @@ namespace entities { private: models::TexturedModel model; + glm::vec3 position; glm::vec3 rotation; float scale; diff --git a/src/entities/collision_entity.cpp b/src/entities/collision_entity.cpp new file mode 100644 index 0000000..da5a2cd --- /dev/null +++ b/src/entities/collision_entity.cpp @@ -0,0 +1,38 @@ +#include "collision_entity.h" + +namespace entities +{ + CollisionEntity::CollisionEntity(const models::TexturedModel& model, const glm::vec3& position, + const glm::vec3& rotation, float scale, const collision::Box& bounding_box) + : 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); + } + + void CollisionEntity::OnCollide(const collision::Collision& collision) + { + if (on_collide != nullptr) + { + on_collide(collision); + } + } + + bool CollisionEntity::IsColliding(const glm::vec3& point) const + { + return (point.x >= min_xyz.x && point.x <= max_xyz.x) && + (point.y >= min_xyz.y && point.y <= max_xyz.y) && + (point.z >= min_xyz.z && point.z <= max_xyz.z); + } + + bool CollisionEntity::IsColliding(const CollisionEntity& e) const + { + return (min_xyz.x <= e.max_xyz.x && max_xyz.x >= e.min_xyz.x) && + (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); + } +} diff --git a/src/entities/collision_entity.h b/src/entities/collision_entity.h new file mode 100644 index 0000000..445fbb7 --- /dev/null +++ b/src/entities/collision_entity.h @@ -0,0 +1,58 @@ +#pragma once + +#include "entity.h" +#include "../collision/collision.h" + +namespace entities +{ + /* + * This class is an entity with a collision box + */ + class CollisionEntity : public Entity + { + private: + collision::Box bounding_box; + + glm::vec3 min_xyz; + glm::vec3 max_xyz; + + void (*on_collide)(const collision::Collision& collision); + + public: + CollisionEntity(const models::TexturedModel& model, const glm::vec3& position, const glm::vec3& rotation, + float scale, const collision::Box& bounding_box); + + /* + * @brief: A function to do some sort of behaviour when the entity collides + * + * @param collision: The collision + */ + virtual void OnCollide(const collision::Collision& collision); + + /* + * @brief: A function to check if the entity is colliding with a point in 3D space + * + * @param point: The point to check if its colliding with the entity + * + * @return: True is the entity is colliding, false if not + */ + bool IsColliding(const glm::vec3& point) const; + + /* + * @brief: A function to check if the entity is colliding with another entity + * + * @param e: The other entity to check if its colliding with this + * + * @return: True is the entity is colliding, false if not + */ + bool IsColliding(const CollisionEntity& e) const; + + /* + * @brief: A function to set the collision behaviour of the entity + * + * @param function: A function pointer to a function with the collision behaviour + */ + void SetCollisionBehaviour(void (*function)(const collision::Collision& collision)) + { if (function != nullptr) { on_collide = function; } } + }; +} \ No newline at end of file diff --git a/wk2_fps.vcxproj b/wk2_fps.vcxproj index c644541..384ca2e 100644 --- a/wk2_fps.vcxproj +++ b/wk2_fps.vcxproj @@ -20,6 +20,7 @@ + @@ -32,7 +33,9 @@ + + diff --git a/wk2_fps.vcxproj.filters b/wk2_fps.vcxproj.filters index 6b55913..df437dd 100644 --- a/wk2_fps.vcxproj.filters +++ b/wk2_fps.vcxproj.filters @@ -48,6 +48,9 @@ Source Files + + Source Files + @@ -92,5 +95,11 @@ Header Files + + Header Files + + + Header Files + \ No newline at end of file