// // largeSphereCollider.js //-------------------- // Provides functions to detect collision against sets of triangles for swept ellipsoids and small rays (low cost, used for green shells). // by RHY3756547 // // includes: gl-matrix.js (glMatrix 2.0) // /formats/kcl.js // window.lsc = new (function() { this.raycast = raycast; this.sweepEllipse = sweepEllipse; this.pointInTriangle = pointInTriangle; //expose this because its kinda useful function raycast(pos, dir, kclO, error, ignoreList) { //used for shells, bananas and spammable items. Much faster than sphere sweep. Error used to avoid falling through really small seams between tris. var error = (error==null)?0:error; var t=1; var tris = getTriList(pos, dir, kclO); var colPlane = null; var colPoint = null; //can be calculated from t, but we calculate it anyway so why not include for (var i=0; i0 && newT t1) { //make sure t0 is smallest value var temp = t1; t1 = t0; t0 = temp; } if (!(t0>1 || t1<0)) { //we will intersect this triangle's plane within this frame. // // Three things can happen for the earliest intersection: // - sphere intersects plane of triangle (pt on plane projected from new position is inside triangle) // - sphere intersects edge of triangle // - sphere intersects point of triangle if (t0 < 0) { embedded = true; t0 = 0; } if (t1 > 1) t1 = 1; var newT = t0; //sphere intersects plane of triangle var pt = []; if (embedded) { vec3.sub(pt, pos, vec3.scale([], tri.Normal, dist)); } else { vec3.add(pt, pos, vec3.scale([], dir, newT)) vec3.sub(pt, pt, tri.Normal); //project new position onto plane along normal } if (pointInTriangle(tri, pt, 0) && newT= 0 && edgePos <= 1) { t = root; colPlane = oTri; colO = targ; colPoint = vec3.add([], vert, vec3.scale(distLine, distLine, edgePos)); //result! planeNormal = tri.Normal; edge = true; } } } } } } function getSmallestRoot(a, b, c, upperLimit) { var det = (b*b) - 4*(a*c); if (det<0) return null; //no result :'( else { det = Math.sqrt(det); var root1 = ((-b)-det)/(2*a) var root2 = ((-b)+det)/(2*a) if (root1 > root2) { //ensure root1 is smallest var temp = root1; root1 = root2; root2 = temp; } if (root1>0 && root10 && root2=-error && v>=-error && (u+v)<1+error); } function getTriList(pos, diff, kclO) { //gets tris from kcl around a line. currently only fetches from middle point of line, but should include multiple samples for large differences in future. var sample = vec3.add([], pos, vec3.scale([], diff, 0.5)) return kclO.getPlanesAt(sample[0], sample[1], sample[2]); } })();