This commit is contained in:
@@ -1,18 +1,28 @@
|
||||
#include "blitz/maths/Physics.h"
|
||||
|
||||
#include "blitz/maths/Maths.h"
|
||||
#include "blitz/misc/Log.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace blitz {
|
||||
namespace maths {
|
||||
|
||||
float Intersects(const Ray& ray, const AABB& aabb) {
|
||||
Vec3f dirFrac{
|
||||
/**
|
||||
* @brief Returns `true` if the half-line `ray` _strictly_ intersects with `aabb`,
|
||||
* and `false` if it _strictly_ doesn't intersect.
|
||||
* Note that if it only intersects with corners, edges, or sides of the box,
|
||||
* or if any coordinate in `ray` is `NAN` or `inf` or if any coordinate in
|
||||
* `aabb` is `NAN` the result is unspecified.
|
||||
* */
|
||||
float Distance(const Ray& ray, const AABB& aabb) {
|
||||
// r.dir is unit direction vector of ray
|
||||
Vec3f dirFrac = {
|
||||
1.0f / ray.direction.x,
|
||||
1.0f / ray.direction.y,
|
||||
1.0f / ray.direction.z,
|
||||
};
|
||||
|
||||
// lb is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
|
||||
// r.org is origin of ray
|
||||
float t1 = (aabb.from.x - ray.origin.x) * dirFrac.x;
|
||||
float t2 = (aabb.to.x - ray.origin.x) * dirFrac.x;
|
||||
float t3 = (aabb.from.y - ray.origin.y) * dirFrac.y;
|
||||
@@ -24,17 +34,19 @@ float Intersects(const Ray& ray, const AABB& aabb) {
|
||||
float tmax = std::min(std::min(std::max(t1, t2), std::max(t3, t4)), std::max(t5, t6));
|
||||
|
||||
// if tmax < 0, ray (line) is intersecting AABB, but the whole AABB is behind us
|
||||
if (tmax < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// if tmin > tmax, ray doesn't intersect AABB
|
||||
if (tmin > tmax) {
|
||||
return -1;
|
||||
if (tmax < 0 || tmin > tmax) {
|
||||
return -1.0f;
|
||||
}
|
||||
|
||||
return tmin;
|
||||
}
|
||||
|
||||
bool Intersects(const Ray& ray, const AABB& aabb) {
|
||||
float distance = Distance(ray, aabb);
|
||||
utils::LOG(std::to_string(distance));
|
||||
return distance >= 0.0f;
|
||||
}
|
||||
|
||||
} // namespace maths
|
||||
} // namespace blitz
|
||||
|
||||
Reference in New Issue
Block a user