raymath - a simple 3d math library

raymath v1.1

Well, actually I created this simple 3d math library for raylib 1.0 about 4 years ago.

As many other raylib modules it can be used as an standalone library. Lately one user tell me he was using it as an alternative to the amazing GLM. He just found some issues on calculations and I reviewed the library to align it in usage and results with GLM.

Just wanted to share it in case someone could find it useful.



Edited by Ray on
Thanks raysan5 was really looking forward to this.
And thanks for making this great game framework (raylib).
in QuatSlerp you don't need to get the sinHalfTheta testing if cosHalfTheta>0.95 is enough to fallback to nlerp. Also you may want to add nlerp as an explicit option.

in QuaternionToMatrix you should divide xx, xy, xz, ... with the length squared. Or replace the 1.0f with the length squared so the calculation works with non-normalized quaternions.

I'm also not a fan of only having axis angle as a way to create quaternions. A way to create a quaternion based on the rotation from one vector to another will allow the user to avoid the acos -> cos roundtrip:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
RMDEF Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to)
{
	Quaternion q = { 0 };

	float cos2Theta = VectorDotProduct(from, to);
        Vector3 cross = VectorCrossProduct(from, to);
	
	q.x = cross.x;
	q.y = cross.y;
	q.z = cross.y;
	q.w = 1 + cos2Theta;

        QuaternionNormalize(&q);

        //inlined nlerp(q, QuaternionIdentity(), 0.5f);
    
	return q;
}
Thank you very much for your feedback!

ratchetfreak, thanks for the proposed improvements, the Quaternions functionality of the library hasn't been the most used one at this point so probably some functions are missing or buggy. I'll work on that.

Any proposed improvement is very welcomed! Please, just let me know!
I'm just thinking out loud but wouldn't it be useful to have a second set of functions that works with arrays of floats instead of your VectorX types for people that already have their own vector types ? If so how would you handle the return value ? (output parameter ?)
1
2
??? Vector2AddF( float* a, float* b );
void Vector2AddF( float* a, float* b, float* result );

Edited by Simon Anciaux on Reason: Typo
ratchetfreak, just implemented proposed reviews and additional functions. Thanks!

mrmixer, it will require a big rewrite/addition of functions, it's simpler to just use proposed structs. In any case, if user has its own data structs for vector3/matrix/quaternion, they can easily be redefined as raymath Vector3/Matrix/Quaternion structs.