Which math library do you recommend?

I’m working on cleaning up my skeletal animation test program. I had planned to stop using LWJGL’s built-in matrix and vector classes but I just haven’t bothered to do so, but when I started with animation I realized how many lacking features there were, especially in the Quaternion class. Now I’m out hunting for a good math library.

Here is a list of features that I need. Functions in red are missing from LWJGL’s math lib.

  • Matrix4x4 operations: translate by vector, multiply, invert.
  • Vector4 functions: transform by matrix
  • Quaternion functions: quat-quat multiplication, rotate vector by quaternion, slerp, quaternion to matrix conversion.

Performance related features:

  • No allocations. I’ll be doing a lot of math for each bone, so being forced to allocate new objects all the time is going to increase GC stuttering quite a bit.
  • Everything is going into FloatBuffers in the end, so something similar to LWJGL’s Matrix4f.load(floatBuffer) would be nice but not necessary.
  • I only need float precision these classes. Double precision would reduce performance and double the GPU bandwidth needed for an unnoticeable increase in precision.

Obviously I can survive with the LWJGL math lib, but I’d really like to start using a better one if possible. What do you guys use and recommend?

I know of none I consider usable for gaming purposes. I’d publish mine, but even I hate it. I can give you sketches of the missing functionality if you desire (hey you already have direct rot vector by quat!) WRT: SLERP - there are multiple ways to implement depending on how it’s going to be used. Also I assume that by “quat to matrix” conversion that you really mean “quat rotation function to matrix”. Also exp & log are very useful for design purposes if you want to play around with curves in quaternion space (used for blending between animations…say walk -> run, etc.)

WRT: Bones I’d consider using quaternion/vector pairs…in that manner you can choose to perform the rotation in the most efficient manner depending on the number of attachments at a given joint. Also (maybe not worth the effort) one degree of freedom joints have a simpler quaternion formulation. It’s possible to use dual-quaternions (AKA Study quaternions, normalize Plucker coordinates) for generalized screw motions, wrenches, etc. Or better yet find a working library (and tell me about it :wink: )

The javax.vecmath isn’t a bad choice.

javadoc
http://download.java.net/media/java3d/javadoc/1.5.0/javax/vecmath/package-summary.html

binary builds
http://java3d.java.net/binary-builds.html

javax quaternions are pretty broken, both for performance and accuracy…don’t recall about the other types.

why not build our own math libary, just copy past available code from different packages and make one big math libary for games.
I have published my math code in the showcase section, which has some more functions then the lwjgl libary but isn’t that good on some other aspects.

We could just think of some best practises we what to have in our libary, like useing floatbuffers and having not many object allocations and so on and refactor exsiting code.

libgdx :smiley:
It is actively developed. Everyone can always contribute to improve it!

Thanks for the suggestions everyone! I’ll have to check up the ones you suggested more thoroughly before I make a decision though…

How is libgdx a math library? Does it have an util library that I can add to my game?

It has a maths package which can easily be ripped out, seems really neat and clean too.

Another option you could try is OpenMaLi (Open Math Library) used by engines like Xith3D.

My math library avoids allocations and took its quaternion implementation from Bullet. ATM it might have too many abstraction layers to allow for read-only types, but I’m considering changing it to have public fields and no interface/abstract types to emphasize performance.

Link is here: http://code.google.com/p/ferox-gl/source/browse/#svn%2Ftrunk%2Fferox-math