optimize hitSphere()
This commit is contained in:
parent
b6c85bcb91
commit
4c271103b3
2 changed files with 9 additions and 5 deletions
|
@ -18,14 +18,14 @@ const fps = 60;
|
|||
|
||||
fn hitSphere(center: Point3, radius: f32, ray: Ray) f32 {
|
||||
const ssr = ray.origin.sub(center); // Sphere-space ray, (A - C) in book
|
||||
const a = Vec3.dot(ray.direction, ray.direction);
|
||||
const b = 2.0 * Vec3.dot(ssr, ray.direction);
|
||||
const c = Vec3.dot(ssr, ssr) - (radius * radius);
|
||||
const discriminant = b * b - 4 * a * c;
|
||||
const a = ray.direction.length_squared();
|
||||
const half_b = Vec3.dot(ssr, ray.direction);
|
||||
const c = ssr.length_squared() - (radius * radius);
|
||||
const discriminant = half_b * half_b - a * c;
|
||||
if (discriminant < 0) {
|
||||
return -1;
|
||||
} else {
|
||||
return (-b - std.math.sqrt(discriminant)) / (2 * a);
|
||||
return (-half_b - std.math.sqrt(discriminant)) / a;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue