@Override public boolean intersectsFrustum(Camera camera, TempVars vars) { // extract the scaled axis // this allows a small optimization. Vector3f axis1 = getScaledAxis(0, vars.vect1); Vector3f axis2 = getScaledAxis(1, vars.vect2); Vector3f axis3 = getScaledAxis(2, vars.vect3); Vector3f tn = vars.vect4; for (int i = 5; i >= 0; i--) { Plane p = camera.getWorldPlane(i); if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; } return true; }
@Override public boolean intersectsBox(BoundingBox box, TempVars vars) { Vector3f axis1 = getScaledAxis(0, vars.vect1); Vector3f axis2 = getScaledAxis(1, vars.vect2); Vector3f axis3 = getScaledAxis(2, vars.vect3); Vector3f tn = vars.vect4; Plane p = vars.plane; Vector3f c = box.getCenter(); p.setNormal(0, 0, -1); p.setConstant(-(c.z + box.getZExtent())); if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; p.setNormal(0, 0, 1); p.setConstant(c.z - box.getZExtent()); if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; p.setNormal(0, -1, 0); p.setConstant(-(c.y + box.getYExtent())); if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; p.setNormal(0, 1, 0); p.setConstant(c.y - box.getYExtent()); if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; p.setNormal(-1, 0, 0); p.setConstant(-(c.x + box.getXExtent())); if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; p.setNormal(1, 0, 0); p.setConstant(c.x - box.getXExtent()); if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; return true; }
private Vector3f getClosestPoint(TempVars vars, Vector3f point) { // non normalized direction Vector3f dir = vars.vect2.set(point).subtractLocal(transform.getTranslation()); // initialize the closest point with box center Vector3f closestPoint = vars.vect3.set(transform.getTranslation()); //store extent in an array float[] r = vars.fWdU; r[0] = transform.getScale().x; r[1] = transform.getScale().y; r[2] = transform.getScale().z; // computing closest point to sphere center for (int i = 0; i < 3; i++) { // extract the axis from the 3x3 matrix Vector3f axis = getScaledAxis(i, vars.vect1); // nomalize (here we just divide by the extent axis.divideLocal(r[i]); // distance to the closest point on this axis. float d = FastMath.clamp(dir.dot(axis), -r[i], r[i]); closestPoint.addLocal(vars.vect4.set(axis).multLocal(d)); } return closestPoint; }