From 44776f7abad3834ee43a5f7003064d7af12550b5 Mon Sep 17 00:00:00 2001 From: Foxar Date: Sun, 20 Dec 2020 13:13:03 +0100 Subject: [PATCH] Added: Incomplete implenetation of BoundingBox class and BoundingBoxFace struct. --- src/engine/include/models/bounding_box.hpp | 69 ++++++++++++++++++++++ src/engine/models/bounding_box.cpp | 33 +++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/engine/include/models/bounding_box.hpp create mode 100644 src/engine/models/bounding_box.cpp diff --git a/src/engine/include/models/bounding_box.hpp b/src/engine/include/models/bounding_box.hpp new file mode 100644 index 0000000..98a9128 --- /dev/null +++ b/src/engine/include/models/bounding_box.hpp @@ -0,0 +1,69 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#ifndef _TYRA_BOUNDING_BOX_ +#define _TYRA_BOUNDING_BOX_ + +#include +#include +#include "math/point.hpp" +#include "math/vector3.hpp" + +/** + * Struct containing face data used in + * bounding box class. + */ +struct BoundingBoxFace +{ + BoundingBoxFace(Vector3 t_minCorner, Vector3 t_maxCorner, float t_axisPos) + { + minCorner = t_minCorner; + maxCorner = t_maxCorner; + axisPosition = t_axisPos; + } + /** Position of the face on it's respective axis. */ + float axisPosition; + /** Corner of the face with lower coordinates */ + Vector3 minCorner; + /** Corner of the face with higher coordinates */ + Vector3 maxCorner; +}; + +/** + * Class containing bounding box data + * which can be used for collision detection. + */ + +class BoundingBox +{ +public: + BoundingBox(Vector3 *t_vertices); + ~BoundingBox(); + const float &getHeight() { return _height; }; + const float &getDepth() { return _depth; }; + const float &getWidth() { return _width; }; + /** Return the vector directly in middle of the bounding box. */ + const Vector3 &getCenter() { return _centerVector; }; + /** Return the front face (furhter on z-axis) */ + const BoundingBoxFace &getFrontFace() { return _frontFace; }; + +private: + Vector3 _vertices[8]; + float _height, _depth, _width; + Vector3 _centerVector; + BoundingBoxFace _frontFace; + BoundingBoxFace _backFace; + BoundingBoxFace _leftFace; + BoundingBoxFace _rightFace; + BoundingBoxFace _topFace; + BoundingBoxFace _bottomFace; +}; + +#endif \ No newline at end of file diff --git a/src/engine/models/bounding_box.cpp b/src/engine/models/bounding_box.cpp new file mode 100644 index 0000000..e16f04e --- /dev/null +++ b/src/engine/models/bounding_box.cpp @@ -0,0 +1,33 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Michał Mostowik +*/ + +#include "../include/models/bounding_box.hpp" +#include "../include/utils/debug.hpp" + +BoundingBox::BoundingBox(Vector3 *t_vectorArray) +{ + if (sizeof(t_vectorArray) / sizeof(Vector3) != 8) + PRINT_ERR("Attempted to create bounding box with invalid count of vectors!"); + + //Perform a deep copy of vertex array parameter + for (int i = 0; i < 9; i++) + _vertices[i] = t_vectorArray[i]; + + //This might be shortened with Vector3 operator overloading, but current + //implementation is more human readable. + _height = _vertices[0].y - _vertices[2].y; + _width = _vertices[0].x - _vertices[4].x; + _depth = _vertices[0].z - _vertices[1].z; + + _centerVector = _vertices[0]; + _centerVector.x += (_width / 2); + _centerVector.y += (_height / 2); + _centerVector.z += (_depth / 2); +} \ No newline at end of file