From ee3d9b2a88438bcc8e3c41d7710d615e4ebd7abb Mon Sep 17 00:00:00 2001 From: Kumikaya <3411015214@qq.com> Date: Tue, 27 May 2025 01:22:14 +0800 Subject: [PATCH] Reduce operations in `RayCast2d::circle_intersection_at` using cross product (#19103) # Objective - Using the cross product to compute the perpendicular distance reduces one multiplication and two additions. --- crates/bevy_math/src/bounding/raycast2d.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_math/src/bounding/raycast2d.rs b/crates/bevy_math/src/bounding/raycast2d.rs index e1def01936..c767c6e3fd 100644 --- a/crates/bevy_math/src/bounding/raycast2d.rs +++ b/crates/bevy_math/src/bounding/raycast2d.rs @@ -78,8 +78,8 @@ impl RayCast2d { pub fn circle_intersection_at(&self, circle: &BoundingCircle) -> Option { let offset = self.ray.origin - circle.center; let projected = offset.dot(*self.ray.direction); - let closest_point = offset - projected * *self.ray.direction; - let distance_squared = circle.radius().squared() - closest_point.length_squared(); + let cross = offset.perp_dot(*self.ray.direction); + let distance_squared = circle.radius().squared() - cross.squared(); if distance_squared < 0. || ops::copysign(projected.squared(), -projected) < -distance_squared {