Actually have matrixed cubes working

This commit is contained in:
2023-02-24 22:32:28 -08:00
parent 7b5bb990b0
commit cf222c469c
11 changed files with 132 additions and 53 deletions

View File

@ -7,16 +7,6 @@
using namespace Dawn;
struct Ray3D {
glm::vec3 origin;
glm::vec3 direction;
};
struct PhysicsSphere {
glm::vec3 center;
float_t radius;
};
bool_t Dawn::raytestSphere(
struct Ray3D ray,
struct PhysicsSphere sphere,
@ -140,5 +130,32 @@ bool_t Dawn::raytestAABB(
}
// The ray intersects the box
return true;
}
bool_t Dawn::raytestCube(
struct Ray3D ray,
struct AABB3D box,
glm::mat4 transform,
glm::vec3 *point,
glm::vec3 *normal,
float_t *distance
) {
// Compute the inverse transformation matrix
glm::mat4 inverseTransform = glm::inverse(transform);
// Transform the ray into model space
struct Ray3D localRay;
localRay.origin = glm::vec3(inverseTransform * glm::vec4(ray.origin, 1.0f));
localRay.direction = glm::normalize(glm::vec3(inverseTransform * glm::vec4(ray.direction, 0.0f)));
// Call raytestAABB with the transformed ray and cube
bool_t hit = raytestAABB(localRay, box, point, normal, distance);
if(!hit) return false;
// Transform the hit point and normal back into world space
*point = glm::vec3(transform * glm::vec4(*point, 1.0f));
*normal = glm::normalize(glm::vec3(glm::transpose(inverseTransform) * glm::vec4(*normal, 0.0f)));
return true;
}

View File

@ -38,4 +38,13 @@ namespace Dawn {
glm::vec3 *normal,
float_t *distance
);
}
bool_t raytestCube(
struct Ray3D ray,
struct AABB3D box,
glm::mat4 transform,
glm::vec3 *point,
glm::vec3 *normal,
float_t *distance
);
}