Skip to content

Commit 828d273

Browse files
authored
Add Vector3 Projecting and Rejection to Raymath (#3263)
* Update raymath.h * formatting
1 parent 21f5482 commit 828d273

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/raymath.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,32 @@ RMAPI Vector3 Vector3Normalize(Vector3 v)
713713
return result;
714714
}
715715

716+
//Calculate the projection of the vector v1 on to v2
717+
RMAPI Vector3 Vector3Project(Vector3 v1, Vector3 v2)
718+
{
719+
float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
720+
float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z);
721+
722+
float mag = v1dv2/v2dv2;
723+
724+
Vector3 result = { v2.x*mag , v2.y*mag, v2.z*mag };
725+
726+
return result;
727+
}
728+
729+
//Calculate the rejection of the vector v1 on to v2
730+
RMAPI Vector3 Vector3Reject(Vector3 v1, Vector3 v2)
731+
{
732+
float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
733+
float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z);
734+
735+
float mag = v1dv2/v2dv2;
736+
737+
Vector3 result = { v1.x - (v2.x*mag) , v1.y - (v2.y*mag), v1.z - (v2.z*mag) };
738+
739+
return result;
740+
}
741+
716742
// Orthonormalize provided vectors
717743
// Makes vectors normalized and orthogonal to each other
718744
// Gram-Schmidt function implementation

0 commit comments

Comments
 (0)