Skip to content

Commit

Permalink
optimize ray_sphere_intersection by assuming direction vector is norm…
Browse files Browse the repository at this point in the history
…alized
  • Loading branch information
makeecat committed Aug 12, 2024
1 parent 8c70d44 commit 62110c4
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ impl Camera {
/// If the intersection is outside the near and far clipping planes, it is ignored
/// # Arguments
/// * `origin` - The origin of the ray
/// * `direction` - The direction of the ray
/// * `direction` - The direction of the ray, should be normalized
/// * `center` - The center of the sphere
/// * `radius` - The radius of the sphere
/// # Returns
Expand All @@ -1120,15 +1120,13 @@ impl Camera {
radius: f32,
) -> Option<f32> {
let oc = origin - center;
let a = direction.dot(direction);
let b = 2.0 * oc.dot(direction);
let b = oc.dot(direction);
let c = oc.dot(&oc) - radius * radius;
let discriminant = b * b - 4.0 * a * c;

let discriminant = b * b - c;
if discriminant < 0.0 {
None
} else {
let t = (-b - discriminant.sqrt()) / (2.0 * a);
let t = -b - discriminant.sqrt();
if t > self.near && t < self.far {
Some(t)
} else {
Expand Down

0 comments on commit 62110c4

Please sign in to comment.