From d4850f99bbcf0776e011a74117ab8a07fd230e68 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 18 Aug 2023 13:27:49 +0900 Subject: [PATCH] Minor fixes to 3d_helpers.cc --- src/3d_helpers.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/3d_helpers.cc b/src/3d_helpers.cc index 9f413da..0f98c11 100644 --- a/src/3d_helpers.cc +++ b/src/3d_helpers.cc @@ -127,7 +127,7 @@ Matrix operator*(const Matrix &a, const Matrix &b) { } bool ray_to_xz_plane(const Ray &ray, float &x_out, float &z_out) { - if (ray.direction.y == 0.0F) { + if (FloatEquals(ray.direction.y, 0.0F)) { return false; } // y = 0 -> amount to set ray.dir to set ray.pos to zero @@ -147,7 +147,7 @@ bool ray_to_xz_plane(const Ray &ray, float &x_out, float &z_out) { std::optional ray_to_plane(const Ray &ray, const Ray &plane) { // Ray dir and plane normal. float rd_pn = Vector3DotProduct(ray.direction, plane.direction); - if (rd_pn == 0.0F) { + if (FloatEquals(rd_pn, 0.0F)) { return std::nullopt; } @@ -155,6 +155,11 @@ std::optional ray_to_plane(const Ray &ray, const Ray &plane) { Vector3DotProduct(ray.position, plane.direction)) / rd_pn; + // Negative amount means collision in opposite direction of ray. + if (amount < 0.0F) { + return std::nullopt; + } + // Amount * ray_dir + ray_pos == plane intersection. return Vector3{ray.position.x + ray.direction.x * amount, ray.position.y + ray.direction.y * amount,