[ADDED] simple collision logic for entities
This commit is contained in:
31
src/collision/collision.h
Normal file
31
src/collision/collision.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#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;
|
||||
};
|
||||
}
|
||||
@@ -13,6 +13,7 @@ namespace entities
|
||||
{
|
||||
private:
|
||||
models::TexturedModel model;
|
||||
|
||||
glm::vec3 position;
|
||||
glm::vec3 rotation;
|
||||
float scale;
|
||||
|
||||
38
src/entities/collision_entity.cpp
Normal file
38
src/entities/collision_entity.cpp
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
58
src/entities/collision_entity.h
Normal file
58
src/entities/collision_entity.h
Normal file
@@ -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; } }
|
||||
};
|
||||
}
|
||||
@@ -20,6 +20,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\entities\camera.cpp" />
|
||||
<ClCompile Include="src\entities\collision_entity.cpp" />
|
||||
<ClCompile Include="src\entities\entity.cpp" />
|
||||
<ClCompile Include="src\gui\gui_interactable.cpp" />
|
||||
<ClCompile Include="src\main.cpp" />
|
||||
@@ -32,7 +33,9 @@
|
||||
<ClCompile Include="src\toolbox\toolbox.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\collision\collision.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\gui\gui_element.h" />
|
||||
|
||||
@@ -48,6 +48,9 @@
|
||||
<ClCompile Include="src\gui\gui_interactable.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\entities\collision_entity.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\entities\Camera.h">
|
||||
@@ -92,5 +95,11 @@
|
||||
<ClInclude Include="src\gui\gui_interactable.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\entities\collision_entity.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\collision\collision.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user