@Test public void testIntersectRaySphere() { Vector3 rayStart = new Vector3(0, 0, 0); Vector3 rayEnd = new Vector3(1, 1, 1); Vector3 sphereCentre = new Vector3(1, 1, 1); double sphereRadius = 1.0; Vector3 hitPoint = new Vector3(); Boolean result = Intersector.intersectRaySphere(rayStart, rayEnd, sphereCentre, sphereRadius, hitPoint); assertTrue(result); assertEquals("hitPoint.x", Math.sqrt(3) - 1, hitPoint.x, 1e-14); assertEquals("hitPoint.y", Math.sqrt(3) - 1, hitPoint.x, 1e-14); assertEquals("hitPoint.z", Math.sqrt(3) - 1, hitPoint.x, 1e-14); }
public static boolean intersectRaySphere(Vector3 rayStart, Vector3 rayEnd, Vector3 sphereCenter, double sphereRadius, Vector3 hitPoint) { rayStart = new Vector3(rayStart); rayEnd = new Vector3(rayEnd); Vector3 dir = Vector3.subtractAndCreate(rayEnd, rayStart); double len = dir.normalize(); sphereCenter = new Vector3(sphereCenter); double radius2 = sphereRadius * sphereRadius; double a = Vector3.dot(dir, dir); double b = 2.0f * Vector3.dot(dir, Vector3.subtractAndCreate(rayStart, sphereCenter)); double c = Vector3.dot(sphereCenter, sphereCenter) + Vector3.dot(rayStart, rayStart) - 2.0f * Vector3.dot(sphereCenter, rayStart) - radius2; double result = b * b - 4.0f * a * c; if (result < 0) return false; double distSqrt = Math.sqrt(result); double q; if (b < 0) q = (-b - distSqrt) / 2.0f; else q = (-b + distSqrt) / 2.0f; double t0 = q / 1; double t1 = c / q; if (t0 > t1) { double temp = t0; t0 = t1; t1 = temp; } if (t1 < 0) return false; if (t0 < 0) { hitPoint.setAll(rayStart.add(Vector3.scaleAndCreate(dir, t1*len))); return true; } else { hitPoint.setAll(rayStart.add(Vector3.scaleAndCreate(dir, t0*len))); return true; } }
Intersector { public static boolean intersectRaySphere(Vector3 rayStart, Vector3 rayEnd, Vector3 sphereCenter, double sphereRadius, Vector3 hitPoint) { rayStart = new Vector3(rayStart); rayEnd = new Vector3(rayEnd); Vector3 dir = Vector3.subtractAndCreate(rayEnd, rayStart); double len = dir.normalize(); sphereCenter = new Vector3(sphereCenter); double radius2 = sphereRadius * sphereRadius; double a = Vector3.dot(dir, dir); double b = 2.0f * Vector3.dot(dir, Vector3.subtractAndCreate(rayStart, sphereCenter)); double c = Vector3.dot(sphereCenter, sphereCenter) + Vector3.dot(rayStart, rayStart) - 2.0f * Vector3.dot(sphereCenter, rayStart) - radius2; double result = b * b - 4.0f * a * c; if (result < 0) return false; double distSqrt = Math.sqrt(result); double q; if (b < 0) q = (-b - distSqrt) / 2.0f; else q = (-b + distSqrt) / 2.0f; double t0 = q / 1; double t1 = c / q; if (t0 > t1) { double temp = t0; t0 = t1; t1 = temp; } if (t1 < 0) return false; if (t0 < 0) { hitPoint.setAll(rayStart.add(Vector3.scaleAndCreate(dir, t1*len))); return true; } else { hitPoint.setAll(rayStart.add(Vector3.scaleAndCreate(dir, t0*len))); return true; } } }
Intersector { public static boolean intersectRaySphere(Vector3 rayStart, Vector3 rayEnd, Vector3 sphereCenter, double sphereRadius, Vector3 hitPoint) { rayStart = new Vector3(rayStart); rayEnd = new Vector3(rayEnd); Vector3 dir = Vector3.subtractAndCreate(rayEnd, rayStart); double len = dir.normalize(); sphereCenter = new Vector3(sphereCenter); double radius2 = sphereRadius * sphereRadius; double a = Vector3.dot(dir, dir); double b = 2.0f * Vector3.dot(dir, Vector3.subtractAndCreate(rayStart, sphereCenter)); double c = Vector3.dot(sphereCenter, sphereCenter) + Vector3.dot(rayStart, rayStart) - 2.0f * Vector3.dot(sphereCenter, rayStart) - radius2; double result = b * b - 4.0f * a * c; if (result < 0) return false; double distSqrt = Math.sqrt(result); double q; if (b < 0) q = (-b - distSqrt) / 2.0f; else q = (-b + distSqrt) / 2.0f; double t0 = q / 1; double t1 = c / q; if (t0 > t1) { double temp = t0; t0 = t1; t1 = temp; } if (t1 < 0) return false; if (t0 < 0) { hitPoint.setAll(rayStart.add(Vector3.scaleAndCreate(dir, t1*len))); return true; } else { hitPoint.setAll(rayStart.add(Vector3.scaleAndCreate(dir, t0*len))); return true; } }  }
Intersector { public static boolean intersectRaySphere(Vector3 rayStart, Vector3 rayEnd, Vector3 sphereCenter, double sphereRadius, Vector3 hitPoint) { rayStart = new Vector3(rayStart); rayEnd = new Vector3(rayEnd); Vector3 dir = Vector3.subtractAndCreate(rayEnd, rayStart); double len = dir.normalize(); sphereCenter = new Vector3(sphereCenter); double radius2 = sphereRadius * sphereRadius; double a = Vector3.dot(dir, dir); double b = 2.0f * Vector3.dot(dir, Vector3.subtractAndCreate(rayStart, sphereCenter)); double c = Vector3.dot(sphereCenter, sphereCenter) + Vector3.dot(rayStart, rayStart) - 2.0f * Vector3.dot(sphereCenter, rayStart) - radius2; double result = b * b - 4.0f * a * c; if (result < 0) return false; double distSqrt = Math.sqrt(result); double q; if (b < 0) q = (-b - distSqrt) / 2.0f; else q = (-b + distSqrt) / 2.0f; double t0 = q / 1; double t1 = c / q; if (t0 > t1) { double temp = t0; t0 = t1; t1 = temp; } if (t1 < 0) return false; if (t0 < 0) { hitPoint.setAll(rayStart.add(Vector3.scaleAndCreate(dir, t1*len))); return true; } else { hitPoint.setAll(rayStart.add(Vector3.scaleAndCreate(dir, t0*len))); return true; } }  static boolean intersectRayPlane(Vector3 rayStart, Vector3 rayEnd, Plane plane, Vector3 hitPoint); static boolean intersectRayTriangle(Vector3 rayStart, Vector3 rayEnd, Vector3 t1, Vector3 t2, Vector3 t3, Vector3 hitPoint); static boolean intersectRaySphere(Vector3 rayStart, Vector3 rayEnd, Vector3 sphereCenter, double sphereRadius, Vector3 hitPoint); }
Intersector { public static boolean intersectRaySphere(Vector3 rayStart, Vector3 rayEnd, Vector3 sphereCenter, double sphereRadius, Vector3 hitPoint) { rayStart = new Vector3(rayStart); rayEnd = new Vector3(rayEnd); Vector3 dir = Vector3.subtractAndCreate(rayEnd, rayStart); double len = dir.normalize(); sphereCenter = new Vector3(sphereCenter); double radius2 = sphereRadius * sphereRadius; double a = Vector3.dot(dir, dir); double b = 2.0f * Vector3.dot(dir, Vector3.subtractAndCreate(rayStart, sphereCenter)); double c = Vector3.dot(sphereCenter, sphereCenter) + Vector3.dot(rayStart, rayStart) - 2.0f * Vector3.dot(sphereCenter, rayStart) - radius2; double result = b * b - 4.0f * a * c; if (result < 0) return false; double distSqrt = Math.sqrt(result); double q; if (b < 0) q = (-b - distSqrt) / 2.0f; else q = (-b + distSqrt) / 2.0f; double t0 = q / 1; double t1 = c / q; if (t0 > t1) { double temp = t0; t0 = t1; t1 = temp; } if (t1 < 0) return false; if (t0 < 0) { hitPoint.setAll(rayStart.add(Vector3.scaleAndCreate(dir, t1*len))); return true; } else { hitPoint.setAll(rayStart.add(Vector3.scaleAndCreate(dir, t0*len))); return true; } }  static boolean intersectRayPlane(Vector3 rayStart, Vector3 rayEnd, Plane plane, Vector3 hitPoint); static boolean intersectRayTriangle(Vector3 rayStart, Vector3 rayEnd, Vector3 t1, Vector3 t2, Vector3 t3, Vector3 hitPoint); static boolean intersectRaySphere(Vector3 rayStart, Vector3 rayEnd, Vector3 sphereCenter, double sphereRadius, Vector3 hitPoint);  }