From ebeb9148297b08554d0a2ba98460f17c02efc24f Mon Sep 17 00:00:00 2001 From: Mexicoco <146551569+Mexicoco@users.noreply.github.com> Date: Wed, 17 Dec 2025 23:57:03 +0800 Subject: [PATCH] fix: correct 2D cross product formula in MathUtils MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cross product formula was incorrect: - Before: p1x * p2x - p1y * p2y (wrong) - After: p1x * p2y - p1y * p2x (correct) The 2D cross product (perp dot product) should compute: A × B = Ax * By - Ay * Bx This returns the z-component of the 3D cross product when treating 2D vectors as 3D vectors with z=0. Also fixed the example comment to show correct result. --- src/utils/MathUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/MathUtils.ts b/src/utils/MathUtils.ts index 0c775e5..fc8a8d4 100644 --- a/src/utils/MathUtils.ts +++ b/src/utils/MathUtils.ts @@ -56,7 +56,7 @@ export const lerp = mix; * @param p2y 第二个向量的y分量 * @returns 叉积结果 (标量) * @example - * cross(1, 0, 0, 1) // 返回 0 + * cross(1, 0, 0, 1) // 返回 1 */ export function cross( p1x: number, @@ -68,7 +68,7 @@ export function cross( if (!isNumber(p1x) || !isNumber(p1y) || !isNumber(p2x) || !isNumber(p2y)) { throw new Error("Invalid parameters for cross function"); } - return p1x * p2x - p1y * p2y; + return p1x * p2y - p1y * p2x; } catch (error) { console.error("Error in cross function:", error); return 0;