From 186bd47c697c8166d50e1861510900eeb2e5ae7c Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 29 Oct 2022 21:19:02 +0300 Subject: [PATCH] added normal vector to raycast results --- ray.cpp | 7 ++++--- ray.h | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ray.cpp b/ray.cpp index dbb9048..5030b5d 100644 --- a/ray.cpp +++ b/ray.cpp @@ -26,8 +26,8 @@ namespace vb01{ for(int i = 0; i < numVerts / 3; i++){ Vector3 pointA = pos + rot * vertices[indices[i * 3]].pos, pointB = pos + rot * vertices[indices[i * 3 + 1]].pos, pointC = pos + rot * vertices[indices[i * 3 + 2]].pos; Vector3 hypVec = pointA - rayPos; - Vector3 perpVec = (pointB - pointA).cross(pointC - pointA); - float a1 = hypVec.norm().getAngleBetween(perpVec.norm()); + Vector3 perpVec = (pointB - pointA).cross(pointC - pointA).norm(); + float a1 = hypVec.norm().getAngleBetween(perpVec); if(a1 > PI / 2){ a1 = PI - a1; @@ -35,7 +35,7 @@ namespace vb01{ } float perpLine = hypVec.getLength() * cos(a1); - float a2 = perpVec.norm().getAngleBetween(rayDir.norm()); + float a2 = perpVec.getAngleBetween(rayDir.norm()); if(a2 <= PI / 2){ float distance = perpLine / cos(a2); @@ -55,6 +55,7 @@ namespace vb01{ if(withinBisecA && withinBisecB && withinBisecC){ CollisionResult result; result.pos = contactPoint; + result.norm = -perpVec; result.distance = distance; result.mesh = m; results.push_back(result); diff --git a/ray.h b/ray.h index 49877a8..5c7d40b 100644 --- a/ray.h +++ b/ray.h @@ -10,7 +10,7 @@ namespace vb01{ class Ray{ public: struct CollisionResult{ - Vector3 pos; + Vector3 pos, norm; float distance; Mesh *mesh = nullptr; };