From 03a532db682d8bd1e2ef639a91f108576c690d88 Mon Sep 17 00:00:00 2001 From: Brandon Poythress Date: Sat, 19 May 2018 15:11:11 -0400 Subject: [PATCH] working on intersection --- Triangle.js | 3 + lib/IntersectionMath.js | 137 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 129 insertions(+), 11 deletions(-) create mode 100644 Triangle.js diff --git a/Triangle.js b/Triangle.js new file mode 100644 index 0000000..af61fc7 --- /dev/null +++ b/Triangle.js @@ -0,0 +1,3 @@ +function Triangle(point1, point2, point3){ + +} \ No newline at end of file diff --git a/lib/IntersectionMath.js b/lib/IntersectionMath.js index 8726f7b..9f91cf5 100644 --- a/lib/IntersectionMath.js +++ b/lib/IntersectionMath.js @@ -1,13 +1,128 @@ //find the intersection of a vector and a plane -//pLX, pLY, pLZ are the components of the home point of the vector. -//this will normally be the eye point but not necessarily. +//linePointVector is the line point (Vector3 object) //u is a constant -//dX, dY, dZ are the unit vector components of the line -//pPX, pPY, pPZ are the components of the home point of the trianglular plane -//nX, nY, nZ are the unit vector components of the triangle -function getIntersection(pLX, pLY, pLZ, dX, dY, dZ, pPX, pPY, pPZ, nX, nY, nZ){ - var crossProduct = crossProduct(dX, dY, dZ, nX, nY, nZ); - - var point = []; - return point; -} \ No newline at end of file +//dVector is the vector for the line (Vector3 object) +//planePointVector is a point on the triangluar plane (Vector3 object) +//nVector is the plane normal vecor (Vector3 object) +function getIntersection(linePointVector, dVector, planePointVector, nVector){ + + //calculate the cross product vector of the line vector and the + //plane normal. + var crossProduct = vector3CrossProduct(dVector, nVector); + + //calculate the magnitude of that vector + var magnitude = vector3Magnitude(crossProduct); + + //If the two are close to parallel, then it is a miss. + //if the are not parallel, it will be a hit and the intersection point + //will be calculated. + var e = 0.01; + + if (mag < e){ + return null; + } else{ + var pMinusL = vector3Subtract(planePointVector, linePointVector); + var u = vector3DotProduct(pMinusL, nVector) / vector3DotProduct(dVector, nVector); + + var intersect = vector3Add(linePointVector, vector3MultiplyByConst(u, dVector)); + + return intersect; + } + +} + + + + +function vector3MultiplyByConst(constant, vector){ + var i = vector.elements[0] * constant; + var j = vector.elements[1] * constant; + var k = vector.elements[2] * constant; + + var result = new vector3(); + result.elements[0] = i; + result.elements[1] = j; + result.elements[2] = k; + + return result; +} + +function vector3Add(startVector, addVector){ + var i = startVector.elements[0] + addVector.elements[0]; + var j = startVector.elements[1] + addVector.elements[1]; + var k = startVector.elements[2] + addVector.elements[2]; + + var result = new Vector3(); + result.elements[0] = i; + result.elements[1] = j; + result.elements[2] = k; + + return result; +} + + +function vector3Subtract(startVector, minusVector){ + var i = startVector.elements[0] - minusVector.elements[0]; + var j = startVector.elements[1] - minusVector.elements[1]; + var k = startVector.elements[2] - minusVector.elements[2]; + + var result = new Vector3(); + result.elements[0] = i; + result.elements[1] = j; + result.elements[2] = k; + + return result; +} + +function vector3DotProduct(startVector, dotVector){ + var iTerm = startVector.elements[0] * dotVector.element[0]; + var jTerm = startVector.elements[1] * dotVector.element[1]; + var kTerm = startVector.elements[2] * dotVector.element[2]; + + var result = iTerm + jTerm + kTerm; + return result; +} + +function vector3CrossProduct(startVector, crossVector){ + var a = startVector.elements; + var b = crossVector.elements; + + var aX = a[0]; + var aY = a[1]; + var aZ = a[2]; + + var bX = b[0]; + var bY = b[1]; + var bZ = b[2]; + + var product = new Vector3(); + var result = product.elements; + + var cI = (aY*bZ) - (aZ*bY); + var cJ = (aZ*bX) - (aX*bZ); + var cK = (aX*bY) - (aY*bX); + + result.push(cI); + result.push(cJ); + result.push(cK); + + return product; +} + +function vector3Magnitude(vector){ + //the vector components + var a1 = vector.elements[0]; + var a2 = vector.elements[1]; + var a3 = vector.elements[2]; + + //the squares of each component + var b1 = Math.pow(a1, 2); + var b2 = Math.pow(a2, 2); + var b3 = Math.pow(a3, 2); + + //the magnitude + var mag = Math.sqrt(b1 + b2 + b3); + + return mag; +} +