From 828d2736987c280883c9a5082bfd4d1a834de8a9 Mon Sep 17 00:00:00 2001 From: Ethan Simpson Date: Sun, 27 Aug 2023 00:44:52 +1200 Subject: [PATCH] Add Vector3 Projecting and Rejection to Raymath (#3263) * Update raymath.h * formatting --- src/raymath.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/raymath.h b/src/raymath.h index 86d2c8eb8..def8bfd42 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -713,6 +713,32 @@ RMAPI Vector3 Vector3Normalize(Vector3 v) return result; } +//Calculate the projection of the vector v1 on to v2 +RMAPI Vector3 Vector3Project(Vector3 v1, Vector3 v2) +{ + float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z); + float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z); + + float mag = v1dv2/v2dv2; + + Vector3 result = { v2.x*mag , v2.y*mag, v2.z*mag }; + + return result; +} + +//Calculate the rejection of the vector v1 on to v2 +RMAPI Vector3 Vector3Reject(Vector3 v1, Vector3 v2) +{ + float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z); + float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z); + + float mag = v1dv2/v2dv2; + + Vector3 result = { v1.x - (v2.x*mag) , v1.y - (v2.y*mag), v1.z - (v2.z*mag) }; + + return result; +} + // Orthonormalize provided vectors // Makes vectors normalized and orthogonal to each other // Gram-Schmidt function implementation