diff options
Diffstat (limited to 'engine-ocean/Game/Components')
22 files changed, 931 insertions, 0 deletions
diff --git a/engine-ocean/Game/Components/CollisionComponents/BoundingDynamicMesh.cpp b/engine-ocean/Game/Components/CollisionComponents/BoundingDynamicMesh.cpp new file mode 100644 index 0000000..d39d118 --- /dev/null +++ b/engine-ocean/Game/Components/CollisionComponents/BoundingDynamicMesh.cpp @@ -0,0 +1,88 @@ +#include "boundingdynamicmesh.h" +#include "Graphics/modeltransform.h" +#include <vector> + +// without mesh obj data +BoundingDynamicMesh::BoundingDynamicMesh(std::shared_ptr<ModelTransform> mt, + const glm::vec3 &initial_pos) : + m_mt(mt) +{ + m_isMesh = false; + m_bounding_cylinder = std::make_shared<CylinderCollider>(initial_pos, mt->getScale()); +} + +// with mesh obj data +BoundingDynamicMesh::BoundingDynamicMesh(std::shared_ptr<ModelTransform> mt, + const glm::vec3 &initial_pos, + std::vector<glm::vec3> &obj_data) : + m_mt(mt), + m_obj_data(obj_data) +{ + m_isMesh = true; + m_bounding_cylinder = std::make_shared<CylinderCollider>(initial_pos, mt->getScale()*getMeshDimensions()*2.f); +} + + +glm::vec3 BoundingDynamicMesh::getCenterPos(){ + return m_mt->getPos(); +} + +void BoundingDynamicMesh::updateCenterPos(glm::vec3 new_pos){ + m_mt->setPos(new_pos); + m_bounding_cylinder->updateCollisionPos(new_pos); +} + + + +glm::vec3 BoundingDynamicMesh::getMeshDimensions(){ + float max_x = m_obj_data[0].x; + float min_x = m_obj_data[0].x; + float max_y = m_obj_data[0].y; + float min_y = m_obj_data[0].y; + float max_z = m_obj_data[0].z; + float min_z = m_obj_data[0].z; + + for (const glm::vec3 &v : m_obj_data){ + // check max + if (v.x > max_x){ + max_x = v.x; + } + if (v.y > max_y){ + max_y = v.y; + } + if (v.z > max_z){ + max_z = v.z; + } + + // check mins + if (v.x < min_x){ + min_x = v.x; + } + if (v.y < min_y){ + min_y = v.y; + } + if (v.z < min_z){ + min_z = v.z; + } + } + + float r_x = (max_x - min_x)/2.f; + float r_y = (max_y - min_y)/2.f; + float r_z = (max_z - min_z)/2.f; + + return glm::vec3(r_x, r_y, r_z); +} + +glm::vec3 BoundingDynamicMesh::getEllipsoidDimensions(){ + if (m_isMesh){ + return m_mt->getScale()*getMeshDimensions(); + } + + return m_mt->getScale()/2.f; +} + +Cylinder BoundingDynamicMesh::getCylinder(){ + return m_bounding_cylinder->getCylinder(); +} + + diff --git a/engine-ocean/Game/Components/CollisionComponents/BoundingDynamicMesh.h b/engine-ocean/Game/Components/CollisionComponents/BoundingDynamicMesh.h new file mode 100644 index 0000000..5d24ac8 --- /dev/null +++ b/engine-ocean/Game/Components/CollisionComponents/BoundingDynamicMesh.h @@ -0,0 +1,32 @@ +#ifndef BOUNDINGDYNAMICMESH_H +#define BOUNDINGDYNAMICMESH_H +#include "Game/Components/CollisionComponents/CylinderCollider.h" +#include "Graphics/modeltransform.h" +#include "glm/glm.hpp" +#include <memory> +#include <vector> + + +class BoundingDynamicMesh : public BoundingShape +{ +public: + BoundingDynamicMesh(std::shared_ptr<ModelTransform> mt, + const glm::vec3 &initial_pos); + BoundingDynamicMesh(std::shared_ptr<ModelTransform> mt, + const glm::vec3 &initial_pos, + std::vector<glm::vec3> &obj_data); + glm::vec3 getCenterPos(); + void updateCenterPos(glm::vec3 new_pos); + glm::vec3 getEllipsoidDimensions(); + Cylinder getCylinder(); + +private: + glm::vec3 getMeshDimensions(); + + bool m_isMesh = false; + std::shared_ptr<ModelTransform> m_mt; + std::vector<glm::vec3> m_obj_data; + std::shared_ptr<CylinderCollider> m_bounding_cylinder; +}; + +#endif // BOUNDINGDYNAMICMESH_H diff --git a/engine-ocean/Game/Components/CollisionComponents/BoundingEllipsoid.h b/engine-ocean/Game/Components/CollisionComponents/BoundingEllipsoid.h new file mode 100644 index 0000000..8dce7c3 --- /dev/null +++ b/engine-ocean/Game/Components/CollisionComponents/BoundingEllipsoid.h @@ -0,0 +1,21 @@ +#ifndef BOUNDINGELLIPSOID_H +#define BOUNDINGELLIPSOID_H +#include "glm/glm.hpp" +#include "BoundingShape.h" + +struct Ellipsoid{ + glm::vec3 R = glm::vec3(0.f); // holds Rx, Ry, Rz radii + glm::vec3 center_pos; +}; + +struct UnitSphere{ + glm::vec3 center_pos; +}; + +class BoundingEllipsoid : public BoundingShape +{ +public: + BoundingEllipsoid(); +}; + +#endif // BOUNDINGELLIPSOID_H diff --git a/engine-ocean/Game/Components/CollisionComponents/BoundingShape.cpp b/engine-ocean/Game/Components/CollisionComponents/BoundingShape.cpp new file mode 100644 index 0000000..b3abba4 --- /dev/null +++ b/engine-ocean/Game/Components/CollisionComponents/BoundingShape.cpp @@ -0,0 +1,6 @@ +#include "BoundingShape.h" + +BoundingShape::BoundingShape() +{ + +} diff --git a/engine-ocean/Game/Components/CollisionComponents/BoundingShape.h b/engine-ocean/Game/Components/CollisionComponents/BoundingShape.h new file mode 100644 index 0000000..1303628 --- /dev/null +++ b/engine-ocean/Game/Components/CollisionComponents/BoundingShape.h @@ -0,0 +1,15 @@ +#ifndef SHAPECOLLIDER_H +#define SHAPECOLLIDER_H + +struct CollisionShape{ + +}; + +class BoundingShape +{ +public: + BoundingShape(); + //virtual CollisionShape getCollisionShape() = 0; +}; + +#endif // SHAPECOLLIDER_H diff --git a/engine-ocean/Game/Components/CollisionComponents/BoundingTriangle.cpp b/engine-ocean/Game/Components/CollisionComponents/BoundingTriangle.cpp new file mode 100644 index 0000000..519d9c7 --- /dev/null +++ b/engine-ocean/Game/Components/CollisionComponents/BoundingTriangle.cpp @@ -0,0 +1,200 @@ +#include "boundingtriangle.h" +#include "Graphics/global.h" +#include "Graphics/modeltransform.h" +#include <iostream> +#include "glm/gtx/hash.hpp" + +// ONLY FOR ENVIRONMENTS +BoundingTriangle::BoundingTriangle(const std::vector<glm::vec3> &obj_data, + const std::shared_ptr<ModelTransform> &mt, + bool isGround) : + obj_mt(mt), + m_isGround(isGround) +{ + populateTriangleData(obj_data); + calculateBounds(obj_data); + //m_datasize = obj_data.size(); +} + +glm::vec3 BoundingTriangle::getRandomSurfacePos(){ + int randomIndex = std::floor(Global::graphics.generateRandomNumbers(0, m_triangles.size()-1)); + int randomVertex = std::floor(Global::graphics.generateRandomNumbers(0, 3)); + + Triangle randomTri = m_triangles[randomIndex]; + + if (glm::dot(glm::vec3(0,1,0), randomTri.normal) > 0.2f){ + switch(randomVertex){ + case 0: + return randomTri.vertexA; + break; + case 1: + return randomTri.vertexB; + break; + default: + return randomTri.vertexC; + break; + } + } else { + // do again until returning a surface triangle + getRandomSurfacePos(); + } +} + + +void BoundingTriangle::addTriangle(const glm::vec3 &vertexA, const glm::vec3 &vertexB, const glm::vec3 &vertexC){ + Triangle tri; + tri.vertexA = vertexA; + tri.vertexB = vertexB; + tri.vertexC = vertexC; + + tri.edge1 = vertexB - vertexA; // edge ab + tri.edge2 = vertexC - vertexA; // edge bc + tri.normal = glm::normalize(glm::cross(tri.edge1, tri.edge2)); + tri.bounds = calculateTriangleBounds(vertexA, vertexB, vertexC); + + // if triangle is a ground triangle + if (m_isGround){ + if (glm::dot(glm::vec3(0,1,0), tri.normal) > 0.2f){ + //std::cout << "area: " << getArea(vertexA, vertexB, vertexC) << std::endl; + tesselateTriangle(vertexA, vertexB, vertexC); + } + } + + m_triangles.push_back(tri); +} + +float BoundingTriangle::getArea(const glm::vec3 &A, const glm::vec3 &B, const glm::vec3 &C){ + glm::vec3 AB = B-A; + glm::vec3 AC = C-A; + return .5f*glm::length((glm::cross(AB,AC))); + +} + +glm::vec3 BoundingTriangle::getCentroid(const glm::vec3 &A, const glm::vec3 &B, const glm::vec3 &C){ + return .333f*(A + B + C); +} + +// tesselation into smaller triangles +void BoundingTriangle::tesselateTriangle(const glm::vec3 &A, const glm::vec3 &B, const glm::vec3 &C){ + float min_area = 1.5f; + + // add centroid if area is small enough + if (getArea(A,B,C) <= min_area){ + m_surface_points.push_back(getCentroid(A, B, C)); + return; + } + + // otherwise divide triangle in 4 (tesselate) + + glm::vec3 ab_mid = .5f*(A+B); + glm::vec3 ac_mid = .5f*(A+C); + glm::vec3 bc_mid = .5f*(B+C); + m_surface_points.push_back(ab_mid); + m_surface_points.push_back(ac_mid); + m_surface_points.push_back(bc_mid); + + tesselateTriangle(ab_mid, ac_mid, A); + tesselateTriangle(bc_mid, ab_mid, B); + tesselateTriangle(bc_mid, ac_mid, C); + tesselateTriangle(ab_mid, ac_mid, bc_mid); +} + + +void BoundingTriangle::populateTriangleData(const std::vector<glm::vec3> &obj_data){ + for (int i=0; i<obj_data.size(); i += 3){ + // convert to worldspace + glm::mat4 modelMat = obj_mt->getModelMatrix(); + glm::vec3 v1 = modelMat*glm::vec4(obj_data[i],1.0); + glm::vec3 v2 = modelMat*glm::vec4(obj_data[i+1],1.0); + glm::vec3 v3 = modelMat*glm::vec4(obj_data[i+2],1.0); + + // make triangle + addTriangle(v1, v2, v3); + } + +// m_surface_points.reserve(m_unique_surface_points.size()); +// std::copy(m_unique_surface_points.begin(), m_unique_surface_points.end(), m_surface_points.begin()); +} + +Bounds3f BoundingTriangle::calculateTriangleBounds(const glm::vec3 &vertexA, + const glm::vec3 &vertexB, + const glm::vec3 &vertexC){ + Bounds3f bounds; + + float max_x, min_x = vertexA.x; + float max_y, min_y = vertexA.y; + float max_z, min_z = vertexA.z; + + if (vertexB.x > max_x) max_x = vertexB.x; + if (vertexB.y > max_y) max_y = vertexB.y; + if (vertexB.z > max_z) max_z = vertexB.z; + + if (vertexC.x > max_x) max_x = vertexC.x; + if (vertexC.y > max_y) max_y = vertexC.y; + if (vertexC.z > max_z) max_z = vertexC.z; + + if (vertexB.x < min_x) min_x = vertexB.x; + if (vertexB.y < min_y) min_y = vertexB.y; + if (vertexB.z < min_z) min_z = vertexB.z; + + if (vertexC.x < min_x) min_x = vertexC.x; + if (vertexC.y < min_y) min_y = vertexC.y; + if (vertexC.z < min_z) min_z = vertexC.z; + + + bounds.max = glm::vec3(max_x, max_y, max_z); + bounds.min = glm::vec3(min_x, min_y, min_z); + + return bounds; +} + +void BoundingTriangle::calculateBounds(const std::vector<glm::vec3> &obj_data){ + max_x = obj_data[0].x; + min_x = obj_data[0].x; + max_y = obj_data[0].y; + min_y = obj_data[0].y; + max_z = obj_data[0].z; + min_z = obj_data[0].z; + + for (const glm::vec3 &v : obj_data){ + // check max + if (v.x > max_x){ + max_x = v.x; + } + if (v.y > max_y){ + max_y = v.y; + } + if (v.z > max_z){ + max_z = v.z; + } + + // check mins + if (v.x < min_x){ + min_x = v.x; + } + if (v.y < min_y){ + min_y = v.y; + } + if (v.z < min_z){ + min_z = v.z; + } + } +} + +Bounds3f BoundingTriangle::getMeshBounds(){ + Bounds3f bounds; + bounds.max = glm::vec3(max_x, max_y, max_z); + bounds.min = glm::vec3(min_x, min_y, min_z); + return bounds; +} + +std::vector<glm::vec3> BoundingTriangle::getSurfacePoints(){ + if (!m_isGround){ + std::cout << "getting surface points of not-ground object!" << std::endl; + } + return m_surface_points; +} + +std::vector<Triangle> BoundingTriangle::getTriangleData(){ + return m_triangles; +} diff --git a/engine-ocean/Game/Components/CollisionComponents/CollisionComponent.cpp b/engine-ocean/Game/Components/CollisionComponents/CollisionComponent.cpp new file mode 100644 index 0000000..bea3f9e --- /dev/null +++ b/engine-ocean/Game/Components/CollisionComponents/CollisionComponent.cpp @@ -0,0 +1,47 @@ +#include "collisioncomponent.h" +#include "Game/Components/CollisionComponents/BoundingTriangle.h" +#include "Game/Components/CollisionComponents/CylinderCollider.h" +#include "Game/Components/CollisionComponents/BoundingDynamicMesh.h" +#include "Graphics/shape.h" +#include <memory> + +// for dynamic objects +CollisionComponent::CollisionComponent(std::string shapeType, std::shared_ptr<ModelTransform> mt, const glm::vec3 &initial_pos) +{ + if (shapeType == "dynamic_mesh"){ + //addCollisionShape<CylinderCollider>(std::make_shared<CylinderCollider>(initial_pos, initial_scale)); + addCollisionShape<BoundingDynamicMesh>(std::make_shared<BoundingDynamicMesh>(mt, initial_pos)); + } +} + +CollisionComponent::CollisionComponent(std::string shapeType, std::shared_ptr<ModelTransform> mt, const glm::vec3 &initial_pos, std::vector<glm::vec3> &obj_data) +{ + if (shapeType == "dynamic_mesh"){ + addCollisionShape<BoundingDynamicMesh>(std::make_shared<BoundingDynamicMesh>(mt, initial_pos, obj_data)); + } +} + +// for rigid meshes / environment +CollisionComponent::CollisionComponent(std::string shapeType, + const std::vector<glm::vec3> &obj_data, + const std::shared_ptr<ModelTransform> &mt, + bool isGround){ + if (shapeType == "obj"){ + addCollisionShape<BoundingTriangle>(std::make_shared<BoundingTriangle>(obj_data, mt, isGround)); + } + +} + + + +bool CollisionComponent::isRigidBody(){ + return m_isRigid; +} + +float CollisionComponent::getReboundVel(){ + return m_rebound_vel; +} + +float CollisionComponent::getAcceleration(){ + return m_acceleration; +} diff --git a/engine-ocean/Game/Components/CollisionComponents/CylinderCollider.cpp b/engine-ocean/Game/Components/CollisionComponents/CylinderCollider.cpp new file mode 100644 index 0000000..3dfded9 --- /dev/null +++ b/engine-ocean/Game/Components/CollisionComponents/CylinderCollider.cpp @@ -0,0 +1,47 @@ +#include "cylindercollider.h" +#include "Game/Components/CollisionComponents/BoundingTriangle.h" +#include "Graphics/modeltransform.h" + +CylinderCollider::CylinderCollider(glm::vec3 initial_pos, glm::vec3 initial_scale){ + m_scale = initial_scale; + + m_cyl.point = glm::vec2(initial_pos.x, initial_pos.z); + m_cyl.radius = .5f * glm::max(initial_scale.x, initial_scale.z); + m_cyl.height = 1.f * abs(initial_scale.y); + m_cyl.min = initial_pos.y - (initial_scale.y/2.f); + m_cyl.max = initial_pos.y + (initial_scale.y/2.f); + + m_cyl.aabbDimensions = abs(glm::vec3(2*m_cyl.radius, m_cyl.height, 2*m_cyl.radius)); + m_cyl.aabbCenterPos = initial_pos; + + updateBounds(); +} + +void CylinderCollider::updateCollisionPos(glm::vec3 new_pos){ + m_cyl.point = glm::vec2(new_pos.x, new_pos.z); // x and z loc + m_cyl.radius = .5f * m_scale.x; // x and z dim + m_cyl.height = 1.f * m_scale.y; // y dimensions + m_cyl.min = new_pos.y - (m_scale.y/2.f); // y coord + m_cyl.max = new_pos.y + (m_scale.y/2.f); // y coord + m_cyl.aabbCenterPos = new_pos; + + updateBounds(); +} + + +void CylinderCollider::updateBounds(){ + Bounds3f bounds; + bounds.min = glm::vec3(m_cyl.aabbCenterPos-glm::vec3(m_cyl.aabbDimensions/2.f)); + bounds.max = glm::vec3(m_cyl.aabbCenterPos+glm::vec3(m_cyl.aabbDimensions/2.f)); + + m_cyl.bounds = bounds; +} + + +Cylinder CylinderCollider::getCylinder(){ + return m_cyl; +} + + + + diff --git a/engine-ocean/Game/Components/CollisionComponents/CylinderCollider.h b/engine-ocean/Game/Components/CollisionComponents/CylinderCollider.h new file mode 100644 index 0000000..3363b84 --- /dev/null +++ b/engine-ocean/Game/Components/CollisionComponents/CylinderCollider.h @@ -0,0 +1,37 @@ +#ifndef CYLINDERCOLLIDER_H +#define CYLINDERCOLLIDER_H + +#include "Game/Components/CollisionComponents/BoundingTriangle.h" +#include "Graphics/modeltransform.h" +#include "glm/glm.hpp" +#include "BoundingShape.h" +#include <memory> + + +struct Cylinder { + glm::vec2 point; // bottom Center + float radius; + + // lines + float height; + float min; + float max; + + glm::vec3 aabbDimensions; + glm::vec3 aabbCenterPos; + Bounds3f bounds; +}; + +class CylinderCollider +{ +public: + CylinderCollider(glm::vec3 initial_pos, glm::vec3 initial_scale); + Cylinder getCylinder();// override; + void updateCollisionPos(glm::vec3 new_pos); + void updateBounds(); +private: + glm::vec3 m_scale; + Cylinder m_cyl; +}; + +#endif // CYLINDERCOLLIDER_H diff --git a/engine-ocean/Game/Components/CollisionComponents/boundingellipsoid.cpp b/engine-ocean/Game/Components/CollisionComponents/boundingellipsoid.cpp new file mode 100644 index 0000000..3f8ed07 --- /dev/null +++ b/engine-ocean/Game/Components/CollisionComponents/boundingellipsoid.cpp @@ -0,0 +1,6 @@ +#include "boundingellipsoid.h" + +BoundingEllipsoid::BoundingEllipsoid() +{ + +} diff --git a/engine-ocean/Game/Components/CollisionComponents/boundingtriangle.h b/engine-ocean/Game/Components/CollisionComponents/boundingtriangle.h new file mode 100644 index 0000000..0263007 --- /dev/null +++ b/engine-ocean/Game/Components/CollisionComponents/boundingtriangle.h @@ -0,0 +1,68 @@ +#ifndef BOUNDINGTRIANGLE_H +#define BOUNDINGTRIANGLE_H +#include "Graphics/modeltransform.h" +#include "glm/glm.hpp" +#include <set> +#include <vector> +#include "BoundingShape.h" + +struct Bounds3f { + glm::vec3 min; + glm::vec3 max; +}; + +struct Triangle{ + glm::vec3 vertexA; // one vertex + glm::vec3 vertexB; + glm::vec3 vertexC; + + glm::vec3 edge1; // two edges + glm::vec3 edge2; + + glm::vec3 normal;// = glm::cross(edge1, edge2); + Bounds3f bounds; + + +}; + +class BoundingTriangle : public BoundingShape +{ +public: + BoundingTriangle(const std::vector<glm::vec3> &obj_data, + const std::shared_ptr<ModelTransform> &mt, + bool isGround = false); + std::vector<Triangle> getTriangleData(); + + Bounds3f getMeshBounds(); + std::vector<glm::vec3> getSurfacePoints(); + glm::vec3 getRandomSurfacePos(); + + + +private: + void addTriangle(const glm::vec3 &vertexA, const glm::vec3 &vertexB, const glm::vec3 &vertexC); + void populateTriangleData(const std::vector<glm::vec3> &obj_data); + void calculateBounds(const std::vector<glm::vec3> &obj_data); + Bounds3f calculateTriangleBounds(const glm::vec3 &vertexA, const glm::vec3 &vertexB, const glm::vec3 &vertexC); + + float getArea(const glm::vec3 &A, const glm::vec3 &B, const glm::vec3 &C); + glm::vec3 getCentroid(const glm::vec3 &A, const glm::vec3 &B, const glm::vec3 &C); + void tesselateTriangle(const glm::vec3 &A, const glm::vec3 &B, const glm::vec3 &C); + + + int m_datasize = 0; + + + + + + std::vector<Triangle> m_triangles; + std::shared_ptr<ModelTransform> obj_mt; + std::set<glm::vec3> m_unique_surface_points; + std::vector<glm::vec3> m_surface_points; + bool m_isGround = false; + + float min_x, min_y, min_z, max_x, max_y, max_z; +}; + +#endif // BOUNDINGTRIANGLE_H diff --git a/engine-ocean/Game/Components/CollisionComponents/collisioncomponent.h b/engine-ocean/Game/Components/CollisionComponents/collisioncomponent.h new file mode 100644 index 0000000..daef723 --- /dev/null +++ b/engine-ocean/Game/Components/CollisionComponents/collisioncomponent.h @@ -0,0 +1,49 @@ +#ifndef COLLISIONCOMPONENT_H +#define COLLISIONCOMPONENT_H + +#include "Game/Components/CollisionComponents/BoundingShape.h" +#include <map> +#include <string> +#include "Game/TypeMap.h" +#include "Graphics/modeltransform.h" +#include "glm/glm.hpp" +#include "Game/Components/Component.h" + + +class CollisionComponent : public Component +{ +public: + template <typename T> + void addCollisionShape(std::shared_ptr<T> &&component){ + m_collision_shapes.put<T>(std::forward<std::shared_ptr<T>>(component)); + } + + template <typename T> + bool hasCollisionShape(){ + return m_collision_shapes.contains<T>(); + } + + template <class T> + T* getCollisionShape(){ + auto comp = m_collision_shapes.find<T>(); + assert(comp != m_collision_shapes.end()); + return static_cast<T*>(comp->second.get()); + } + + CollisionComponent(std::string shapeType, std::shared_ptr<ModelTransform> mt, const glm::vec3 &initial_pos, std::vector<glm::vec3> &obj_data); + CollisionComponent(std::string shapeType, std::shared_ptr<ModelTransform> mt, const glm::vec3 &initial_pos); + CollisionComponent(std::string shapeType, const std::vector<glm::vec3> &obj_data, const std::shared_ptr<ModelTransform> &mt, + bool isGround = false); + + bool isRigidBody(); + float getReboundVel(); + float getAcceleration(); + +private: + TypeMap<std::shared_ptr<BoundingShape>> m_collision_shapes; + bool m_isRigid = true; + float m_rebound_vel = 0.f; + float m_acceleration = 0.f; +}; + +#endif // COLLISIONCOMPONENT_H diff --git a/engine-ocean/Game/Components/Component.h b/engine-ocean/Game/Components/Component.h new file mode 100644 index 0000000..fef4330 --- /dev/null +++ b/engine-ocean/Game/Components/Component.h @@ -0,0 +1,13 @@ +#ifndef COMPONENT_H +#define COMPONENT_H + + +class Component +{ +public: + Component(); + + +}; + +#endif // COMPONENT_H diff --git a/engine-ocean/Game/Components/DrawComponent.cpp b/engine-ocean/Game/Components/DrawComponent.cpp new file mode 100644 index 0000000..28640dd --- /dev/null +++ b/engine-ocean/Game/Components/DrawComponent.cpp @@ -0,0 +1,54 @@ +#include "drawcomponent.h" +#include <string> + +DrawComponent::DrawComponent(std::shared_ptr<Shape> shape, std::string shape_name) +{ + m_shape = shape; + m_shape_name = shape_name; +} + +DrawComponent::DrawComponent(std::shared_ptr<Shape> shape) +{ + m_shape = shape; +} + +DrawComponent::DrawComponent(std::vector<std::shared_ptr<Shape>> shapes) +{ + m_shapes = shapes; + hasMultipleShapes = true; +} + + +void DrawComponent::addMaterial(std::string material_name, std::string material_filepath){ + Global::graphics.addMaterial(material_name, material_filepath); + m_material_name = material_name; + hasMaterial = true; +} + +std::shared_ptr<Shape> DrawComponent::getShape(){ + return m_shape; +} + +std::vector<std::shared_ptr<Shape>> DrawComponent::getShapesWithMaterials(){ + if (hasMultipleShapes){ + return m_shapes; + } +} + +std::string DrawComponent::getShapeName(){ + return m_shape_name; +} + +std::shared_ptr<Material> DrawComponent::getMaterial(){ + return Global::graphics.getMaterial(m_material_name); +} + +bool DrawComponent::objHasMaterial(){ + return hasMaterial; +} + +bool DrawComponent::objHasMultipleShapes(){ + return hasMultipleShapes; +} + + diff --git a/engine-ocean/Game/Components/PathfindComponent.h b/engine-ocean/Game/Components/PathfindComponent.h new file mode 100644 index 0000000..13f4e6d --- /dev/null +++ b/engine-ocean/Game/Components/PathfindComponent.h @@ -0,0 +1,22 @@ +#ifndef PATHFINDCOMPONENT_H +#define PATHFINDCOMPONENT_H + + +#include "Game/Systems/Pathfinding/pathfinder.h" +#include "glm/fwd.hpp" +#include <vector> +#include "Component.h" + +class PathfindComponent : public Component +{ +public: + PathfindComponent(std::vector<glm::vec3> vertices, std::vector<glm::ivec3> triangles); + std::vector<glm::vec3> getPath(const glm::vec3 &A, const glm::vec3 &B); + +private: + std::vector<glm::vec3> m_vertices; + std::vector<glm::ivec3> m_triangles; + std::unique_ptr<Pathfinder> m_pathfinder; +}; + +#endif // PATHFINDCOMPONENT_H diff --git a/engine-ocean/Game/Components/TransformComponent.cpp b/engine-ocean/Game/Components/TransformComponent.cpp new file mode 100644 index 0000000..582b623 --- /dev/null +++ b/engine-ocean/Game/Components/TransformComponent.cpp @@ -0,0 +1,92 @@ +#include "transformcomponent.h" + +TransformComponent::TransformComponent(std::shared_ptr<ModelTransform> mt, + std::string entity_id, + std::map<std::string, + BlackboardData>& global_blackboard, bool isAI): + m_model_transform(mt), + m_global_blackboard(global_blackboard), + isAIObject(isAI) +{ + old_pos = mt->getPos(); + estimated_final_pos = mt->getPos(); + m_entity_id = entity_id; + + // initialize blackboard data for entity + BlackboardData data; + m_global_blackboard.insert(std::pair<std::string, BlackboardData>(m_entity_id, data)); + m_global_blackboard[m_entity_id].locationData.currPos = old_pos; +} + +std::shared_ptr<ModelTransform> TransformComponent::getMT(){ + return m_model_transform; +} +std::vector<std::shared_ptr<ModelTransform>> TransformComponent::getAllMT(){ + return m_all_model_transforms; +} + +bool TransformComponent::hasMultipleMT(){ + return multipleMT; +} + +void TransformComponent::translate(const glm::vec3 &delta){ + m_model_transform->translate(delta); + + // update old pos for collisions + + old_pos = m_model_transform->getPos(); + + if (isAIObject){ + m_global_blackboard[m_entity_id].locationData.currPos = old_pos; + } +} + +void TransformComponent::setPos(const glm::vec3 &new_pos){ + m_model_transform->setPos(new_pos); + + // update old pos for collisions + old_pos = new_pos; + + if (isAIObject){ + m_global_blackboard[m_entity_id].locationData.currPos = old_pos; + } +} + +glm::vec3 TransformComponent::getPos(){ + return m_model_transform->getPos(); +} + +void TransformComponent::setScale(const glm::vec3 &scale){ + m_model_transform->setScale(scale); +} + +glm::vec3 TransformComponent::getScale(){ + return m_model_transform->getScale(); +} + +float TransformComponent::getYRotationAngle(){ + glm::mat4 rotMat = m_model_transform->getRotation(); + float ry0 = rotMat[0][2]; +// float ry1 = rotMat[1][2]; +// float ry2 = rotMat[2][2]; + +// float sign = 1.f; +// if (ry1*ry2 < 0) sign = -1.f; + +// float angley = std::atan2(-ry0, sign*pow((pow(ry1,2) + pow(ry2,2)), .5)); + float angley = -std::asin(ry0); + float angley2 = M_PI - angley; + + //std::cout << "angle 1: " << angley << std::endl; + std::cout << "angle 2: " << angley2 << std::endl; + + return angley; +} + +void TransformComponent::setRotation(float angle, glm::vec3 axis){ + m_model_transform->rotate(angle, axis); +} + + + + diff --git a/engine-ocean/Game/Components/component.cpp b/engine-ocean/Game/Components/component.cpp new file mode 100644 index 0000000..f557273 --- /dev/null +++ b/engine-ocean/Game/Components/component.cpp @@ -0,0 +1,6 @@ +#include "component.h" + +Component::Component() +{ + +} diff --git a/engine-ocean/Game/Components/drawcomponent.h b/engine-ocean/Game/Components/drawcomponent.h new file mode 100644 index 0000000..94f4b21 --- /dev/null +++ b/engine-ocean/Game/Components/drawcomponent.h @@ -0,0 +1,41 @@ +#ifndef DRAWCOMPONENT_H +#define DRAWCOMPONENT_H + + +#include "Graphics/global.h" +#include <string> +#include "Component.h" + +class DrawComponent : public Component +{ +public: + DrawComponent(std::shared_ptr<Shape> shape); + DrawComponent(std::shared_ptr<Shape> shape, std::string shape_name); + + // for materials, with multiple shape parts + DrawComponent(std::vector<std::shared_ptr<Shape>> shapes); + + + void draw(const std::shared_ptr<ModelTransform> &entity_mt); + void addMaterial(std::string material_name, std::string material_filepath); + std::shared_ptr<Material> getMaterial(); + std::shared_ptr<Shape> getShape(); + std::vector<std::shared_ptr<Shape>> getShapesWithMaterials(); + bool objHasMaterial(); + bool objHasMultipleShapes(); + + std::string getShapeName(); + +private: + std::shared_ptr<Shape> m_shape; + std::vector<std::shared_ptr<Shape>> m_shapes; + + std::string m_material_name; + bool hasMaterial = false; + std::string m_shape_name = "empty"; + + bool hasMultipleShapes = false; + +}; + +#endif // DRAWCOMPONENT_H diff --git a/engine-ocean/Game/Components/pathfindcomponent.cpp b/engine-ocean/Game/Components/pathfindcomponent.cpp new file mode 100644 index 0000000..ad87e23 --- /dev/null +++ b/engine-ocean/Game/Components/pathfindcomponent.cpp @@ -0,0 +1,16 @@ +#include "pathfindcomponent.h" +#include "glm/glm.hpp" +#include <vector> + +PathfindComponent::PathfindComponent(std::vector<glm::vec3> vertices, std::vector<glm::ivec3> triangles): + m_vertices(vertices), + m_triangles(triangles), + m_pathfinder(std::make_unique<Pathfinder>(vertices, triangles)) +{ + +} + + +std::vector<glm::vec3> PathfindComponent::getPath(const glm::vec3 &A, const glm::vec3 &B){ + return m_pathfinder->findPath(A,B); +} diff --git a/engine-ocean/Game/Components/playercontrolcomponent.cpp b/engine-ocean/Game/Components/playercontrolcomponent.cpp new file mode 100644 index 0000000..40062f1 --- /dev/null +++ b/engine-ocean/Game/Components/playercontrolcomponent.cpp @@ -0,0 +1,6 @@ +#include "playercontrolcomponent.h" + +PlayerControlComponent::PlayerControlComponent() +{ + +} diff --git a/engine-ocean/Game/Components/playercontrolcomponent.h b/engine-ocean/Game/Components/playercontrolcomponent.h new file mode 100644 index 0000000..2a3d91c --- /dev/null +++ b/engine-ocean/Game/Components/playercontrolcomponent.h @@ -0,0 +1,11 @@ +#ifndef PLAYERCONTROLCOMPONENT_H +#define PLAYERCONTROLCOMPONENT_H + + +class PlayerControlComponent +{ +public: + PlayerControlComponent(); +}; + +#endif // PLAYERCONTROLCOMPONENT_H diff --git a/engine-ocean/Game/Components/transformcomponent.h b/engine-ocean/Game/Components/transformcomponent.h new file mode 100644 index 0000000..7b6a5c2 --- /dev/null +++ b/engine-ocean/Game/Components/transformcomponent.h @@ -0,0 +1,54 @@ +#ifndef TRANSFORMCOMPONENT_H +#define TRANSFORMCOMPONENT_H + +#include "Game/Systems/aisystem.h" +#include "Graphics/global.h" +#include <memory> +#include "Component.h" + +class TransformComponent : public Component +{ +public: + TransformComponent(std::shared_ptr<ModelTransform> mt, + std::string entity_id, + std::map<std::string, BlackboardData>& m_global_blackboard, + bool isAI = false); + + void translate(const glm::vec3 &delta); + void setPos(const glm::vec3 &new_pos); + void setScale(const glm::vec3 &scale); + std::shared_ptr<ModelTransform> getMT(); + std::vector<std::shared_ptr<ModelTransform>> getAllMT(); + glm::vec3 getScale(); + //glm::vec3 getRotation(); + void setRotation(float angle, glm::vec3 axis); + float getYRotationAngle(); + + + + + bool hasMultipleMT(); + glm::vec3 getPos(); + + // used for collisions + glm::vec3 old_pos; + glm::vec3 estimated_final_pos; + bool onGround = false; + bool movingLaterally = false; + float gravity = -25.f; + float yVelocity = 0.f; + + + +private: + std::shared_ptr<ModelTransform> m_model_transform; + std::vector<std::shared_ptr<ModelTransform>> m_all_model_transforms; + bool multipleMT = false; + std::map<std::string, BlackboardData>& m_global_blackboard; + std::string m_entity_id; + bool isAIObject = false; + + +}; + +#endif // TRANSFORMCOMPONENT_H |