This commit is contained in:
2024-05-26 14:20:59 -04:00
parent 4c901efeeb
commit d8ad7bc914
8 changed files with 40 additions and 31 deletions

View File

@@ -49,7 +49,7 @@ CPMAddPackage(
CPMAddPackage(
NAME ReWindow
URL https://git.redacted.cc/Redacted/ReWindow/archive/vA0.2.30.zip
URL https://git.redacted.cc/Redacted/ReWindow/archive/vA0.2.31.zip
)
CPMAddPackage(
@@ -59,7 +59,7 @@ CPMAddPackage(
CPMAddPackage(
NAME J3ML
URL https://git.redacted.cc/josh/j3ml/archive/Release-6.zip
URL https://git.redacted.cc/josh/j3ml/archive/Release-7.zip
)
#Broken
#CPMAddPackage(

View File

@@ -8,9 +8,9 @@ using J3ML::Geometry::Frustum;
using J3ML::Geometry::Sphere;
namespace Collision {
AABB genMinimallyEnclosingAABB(const VertexArray& vA);
AABB genMinimallyEnclosingAABB(const VertexArray& vA, const Vector3& rotation); //Faster due to using the cached OBB.
Sphere genMinimallyEnclosingSphere(const VertexArray& vA); //Faster due to using the cached OBB.
OBB genMinimallyEnclosingOBB(const VertexArray& vA, const Vector3& rotation);
AABB genMinimallyEnclosingAABB(const VertexArray* vA);
AABB genMinimallyEnclosingAABB(const VertexArray* vA, const Vector3& rotation); //Faster due to using the cached OBB.
Sphere genMinimallyEnclosingSphere(const VertexArray* vA); //Faster due to using the cached OBB.
OBB genMinimallyEnclosingOBB(const VertexArray* vA, const Vector3& rotation);
VertexArray getDrawable(const Shape& collider);
}

View File

@@ -49,6 +49,7 @@ public:
virtual VertexArray* getGeometry();
AABB getAABB();
Sphere getSphere();
OBB getOBB();
std::vector<Entity*> GetChildren();
virtual void SetParent(Entity* parent);
bool IsDescendantOf(Entity* ancestor);

View File

@@ -35,9 +35,9 @@ public:
/// Loads data from an OBJ mesh file to populate the VertexArray
/// @see LearnOpenGL::Mesh LearnOpenGL::Model J3ML::Geometry::TriangleMesh
void load(const std::string& filename);
const OBB& getCachedOBB(const Matrix3x3& rotation) const;
const OBB& getCachedOBB(const Vector3& rotation) const;
const OBB& getCachedOBB(const EulerAngle& rotation) const;
[[nodiscard]] OBB getCachedOBB(const Matrix3x3& rotation) const;
[[nodiscard]] OBB getCachedOBB(const Vector3& rotation) const;
[[nodiscard]] OBB getCachedOBB(const EulerAngle& rotation) const;
static void erase(const VertexArray& vArray);
virtual void draw();
void drawWireframe();

View File

@@ -13,7 +13,6 @@ void Cube::pre_render() {
angle.z = angle.z + 24*engine->frameDelta;
OBB obb = getGeometry()->getCachedOBB(angle);
VertexArray drawable = Collision::getDrawable(getGeometry()->getCachedOBB(angle));
}
void Cube::update(float elapsed) {

View File

@@ -1,6 +1,6 @@
#include <engine/collision.h>
AABB Collision::genMinimallyEnclosingAABB(const VertexArray& vA) {
AABB Collision::genMinimallyEnclosingAABB(const VertexArray* vA) {
float minX = std::numeric_limits<float>::max();
float minY = std::numeric_limits<float>::max();
float minZ = std::numeric_limits<float>::max();
@@ -8,7 +8,7 @@ AABB Collision::genMinimallyEnclosingAABB(const VertexArray& vA) {
float maxY = -std::numeric_limits<float>::max();
float maxZ = -std::numeric_limits<float>::max();
AABB result;
for (const auto& vertex : vA.vertices) {
for (const auto& vertex : vA->vertices) {
if (vertex.x < minX)
minX = vertex.x;
if (vertex.y < minY)
@@ -28,14 +28,14 @@ AABB Collision::genMinimallyEnclosingAABB(const VertexArray& vA) {
return result;
}
AABB Collision::genMinimallyEnclosingAABB(const VertexArray& vA, const Vector3& rotation) {
return vA.getCachedOBB(rotation).MinimalEnclosingAABB();
AABB Collision::genMinimallyEnclosingAABB(const VertexArray* vA, const Vector3& rotation) {
return vA->getCachedOBB(rotation).MinimalEnclosingAABB();
}
Sphere Collision::genMinimallyEnclosingSphere(const VertexArray& vA) {
return vA.getCachedOBB(Vector3(0, 0, 0)).MinimalEnclosingSphere();
Sphere Collision::genMinimallyEnclosingSphere(const VertexArray* vA) {
return vA->getCachedOBB(Vector3(0, 0, 0)).MinimalEnclosingSphere();
}
OBB Collision::genMinimallyEnclosingOBB(const VertexArray& vA, const Vector3& rotation) {
OBB Collision::genMinimallyEnclosingOBB(const VertexArray* vA, const Vector3& rotation) {
Vector3 centroid(0, 0, 0);
Vector3 radii(0, 0, 0);
Matrix3x3 rotationMatrix = Matrix3x3::FromQuat(Quaternion((EulerAngle) rotation));
@@ -43,11 +43,11 @@ OBB Collision::genMinimallyEnclosingOBB(const VertexArray& vA, const Vector3& ro
Vector3 axis1 = Vector3(rotationMatrix[0][1], rotationMatrix[1][1], rotationMatrix[2][1]);
Vector3 axis2 = Vector3(rotationMatrix[0][2], rotationMatrix[1][2], rotationMatrix[2][2]);
for (const auto& vertex : vA.vertices)
for (const auto& vertex : vA->vertices)
centroid += vertex;
centroid /= vA.vertices.size();
centroid /= vA->vertices.size();
for (const auto& vertex : vA.vertices) {
for (const auto& vertex : vA->vertices) {
Vector3 diff = vertex - centroid;
radii.x = std::max(radii.x, std::abs(diff.x));
radii.y = std::max(radii.y, std::abs(diff.y));
@@ -56,13 +56,17 @@ OBB Collision::genMinimallyEnclosingOBB(const VertexArray& vA, const Vector3& ro
return {centroid, radii, axis0, axis1, axis2};
}
//TODO
VertexArray Collision::getDrawable(const Shape& collider) {
if (const auto* c = static_cast<const AABB*>(&collider)) {
if (const auto* s = dynamic_cast<const AABB*>(&collider)) {
std::cout << "We passed in an AABB" << std::endl;
exit(0);
}
if (const auto* c = static_cast<const OBB*>(&collider)) {
if (const auto* s = dynamic_cast<const OBB*>(&collider)) {
std::cout << "We passed in an OBB" << std::endl;
exit(0);
}
if (const auto* s = dynamic_cast<const Sphere*>(&collider)) {
std::cout << "We passed in a Sphere" << std::endl;
}
}

View File

@@ -69,12 +69,14 @@ void Entity::loadGeometry() {
VertexArray* Entity::getGeometry() {
if (engine->world->geometryList.empty())
loadGeometry();
for (auto& vArray : engine->world->geometryList)
if (vArray.name == name) {
if (std::find(vArray.usedBy.begin(), vArray.usedBy.end(), this) == vArray.usedBy.end())
vArray.usedBy.push_back(this);
return &vArray;
}
loadGeometry();
return getGeometry();
}
@@ -216,10 +218,13 @@ void Entity::erase() {
}
AABB Entity::getAABB() {
//TODO: use the OBB caching trick from before so we don't need to loop the whole vertex array.
return Collision::genMinimallyEnclosingAABB(*getGeometry());
return Collision::genMinimallyEnclosingAABB(getGeometry());
}
Sphere Entity::getSphere() {
return Collision::genMinimallyEnclosingSphere(*getGeometry());
return Collision::genMinimallyEnclosingSphere(getGeometry());
}
OBB Entity::getOBB() {
return getGeometry()->getCachedOBB(angle);
}

View File

@@ -33,7 +33,7 @@ void VertexArray::load (const std::string& filename) {
}
//Cached OBB.
this->cachedOBB = Collision::genMinimallyEnclosingOBB(*this, (Vector3) {0, 0, 0});
this->cachedOBB = Collision::genMinimallyEnclosingOBB(this, (Vector3) {0, 0, 0});
}
void VertexArray::draw() {
@@ -163,14 +163,14 @@ VertexArray::VertexArray(const std::string &filename) {
load(filename);
}
const OBB& VertexArray::getCachedOBB(const Matrix3x3& rotationMatrix) const {
OBB VertexArray::getCachedOBB(const Matrix3x3& rotationMatrix) const {
Vector3 axis0 = Vector3(rotationMatrix[0][0], rotationMatrix[1][0], rotationMatrix[2][0]);
Vector3 axis1 = Vector3(rotationMatrix[0][1], rotationMatrix[1][1], rotationMatrix[2][1]);
Vector3 axis2 = Vector3(rotationMatrix[0][2], rotationMatrix[1][2], rotationMatrix[2][2]);
return OBB(cachedOBB.pos, cachedOBB.r, axis0, axis1, axis2);
}
const OBB& VertexArray::getCachedOBB(const Vector3& rotation) const {
OBB VertexArray::getCachedOBB(const Vector3& rotation) const {
Matrix3x3 rotationMatrix = Matrix3x3::FromQuat(Quaternion((EulerAngle) rotation));
Vector3 axis0 = {rotationMatrix[0][0], rotationMatrix[1][0], rotationMatrix[2][0]};
Vector3 axis1 = {rotationMatrix[0][1], rotationMatrix[1][1], rotationMatrix[2][1]};
@@ -178,7 +178,7 @@ const OBB& VertexArray::getCachedOBB(const Vector3& rotation) const {
return OBB(cachedOBB.pos, cachedOBB.r, axis0, axis1, axis2);
}
const OBB& VertexArray::getCachedOBB(const EulerAngle &rotation) const {
OBB VertexArray::getCachedOBB(const EulerAngle &rotation) const {
Matrix3x3 rotationMatrix = Matrix3x3::FromQuat(Quaternion( rotation));
Vector3 axis0 = {rotationMatrix[0][0], rotationMatrix[1][0], rotationMatrix[2][0]};
Vector3 axis1 = {rotationMatrix[0][1], rotationMatrix[1][1], rotationMatrix[2][1]};