check if plane intersect with ray

This commit is contained in:
Shuo Feng 2024-02-20 23:32:29 -05:00
parent 0b38f93655
commit bd99b001bb
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1

View file

@ -1,5 +1,7 @@
#include "Geometry.h"
#include <Eigen/Dense>
void Geometry::setTransform(const Matrix4f &transform) {
this->transform = transform;
}
@ -13,4 +15,18 @@ bool Sphere::intersect(const Ray &r) const {
return b * b - 4 * a * c >= 0;
}
bool Rectangle::intersect(const Ray &r) const { return false; }
bool Rectangle::intersect(const Ray &r) const {
Vector3f normal = (p2 - p1).cross(p3 - p1).normalized();
float d = normal.dot(p1);
float denom = normal.dot(r.getDirection());
if (abs(denom) < 1e-6f)
return false;
float t = (normal.dot(r.getOrigin() - p1) - d) / denom;
if (t <= 0)
return false;
Vector3f p = r.getOrigin() + t * r.getDirection();
return true;
}