What should be taken into consideration when choosing a math library for games?

Martin Foot
  • What should be taken into consideration when choosing a math library for games? Martin Foot

    I'm currently selecting a C++ math library to use for a project. There are several questions here and on SO concerning the 'best' library available and many answers with different suggestions, but I would like to run some tests on each of them before I make a decision.

    I would like to select a library that will cater for my current needs but also for any needs in any projects I may work on in the future. My problem is that due to being fairly new at this, I'm not entirely sure what those needs will be yet.

    Aside from the points that I can think of listed below, and assuming that I will only test libraries with a suitable license, what other commonly used features should I test/look for before making a selection?

    • Matrix operations - including addition, subtraction, multiplication, inversion, transposition, translation, rotation, scale
    • Vector operations - cross/dot product, normalisation, zeroing, magnitude, length2, scalar operands, normal calculation
    • Euler angle + Quarternion operations - rotation, interpolation, conversion, inversion, multiplication
    • Coordinate system conversion (Cartesian, spherical)
    • Random numbers - generator speed, period, statistical randomness
    • Possibly noise generation
    • Ease of integration with gl functions like glTranslate()

    Also, assuming these libraries have been tested for mathematical accuracy, are there factors other than ease of use (documentation, mailing list/developer access), speed (vectorisation, SIMD), and memory usage to consider?

  • This may not be the best answer for this question but during game development you may encounter many more greater challenges than the performance and stability of math library you are using. I'm not a man of c++ but before a math library what you have to decide is

    1 ) Your game, your needs

    2 ) The most suitable framework for your game or no framework if you want to let you develop it asap. And is consistent at least with its graphics and maths.

    The most common mistake around programmers including me is to lost your way around frameworks and libraries and spent great amount of time on what to use instead of real usable code production.

  • The library should not be a problem for your project. If it is your case that means that you are coupling your system with a library: this generally is not good idea. What if another better library emerges? And if the library you choose dies?

    Try to stay orthogonal to the implementation of every basic library (unless you are coding a thin wrapper around it) and focus on your needs: this will decrease the amount of work need when/if you will need to change library.

  • Since you mentioned noise generation and random numbers I'd like to answer with a slight different response: portability.

    Most math libraries are going to target specific systems such as little endian win32 platforms. This is fine if you are only planning on meeting those targets but is really going to hold you back if you ever plan on porting to a portable device or - and this is the main reason for my response - porting your algorithms to shaders.

    Perlin noise is a great example but a random number generator could also be ported as well as vector operations to shader work. GPUs are pretty much designed for bulk vector operations and if you plan on using the result for GPU work you now have the result in GPU memory.

    Additionally you should also consider threading whether or not you port to other devices. With threading becoming a larger and more important I highly recommend looking for or designing libraries with support for multithreading. For example if a 4x4 Matrix class can be shared across threads you probably should have some locks around common operations so you don't read it in a half modified state. This really applies to random number generators more than anything though it isn't an issue if you aren't looking for determinism.

  • Your list of functions looks pretty exhaustive, possibly containing more than you actually need.

    Given the number of libs out there, my personal priority list when picking which one to use is:

    • license (GPL is out, unless project is GPL to begin with)
    • portability
    • comfortability of use - this is mostly personal opinion, and cause for the multitude of libs out there
    • documentation
    • capabilities
    • clarity of source code (so when docs run out, you still can figure it out)
    • performance

    Most of the time, math lib performance isn't your bottleneck. This naturally depends on what you're expecting to do with it.. First make it work, then make it fast. Profile your code. Premature optimization, etc.

    Also, if you're integrating some physics package, it's bound to have a math lib integrated. You might want to check it out before adding another one.

c++ mathematics software-engineering
Related questions and answers
  • Are you aware of a complete (or almost complete) cross platform math library for use in OpenGL ES 2.0 games? The library should contain: Matrix2x2, Matrix 3x3, Matrix4x4 classes Quaternions Vector2, Vector3, Vector4 Classes Euler Angle Class Operations amongh the above mentioned classes, conversions, etc.. Standardly used math operations in 3D graphics (Dot Product, Cross Product, SLERP, etc...) Is there such Math API available either standalone or as a part of any package? Programming Language: Visual C++ but planned to be ported to OS X and Android OS.

  • not seem to have joystick input support, which would require that SDL or some other library also be used. So my question can be summed up as this: What is the best way to get SVG and joystick...I'm looking into building a cross-platform opensource 2D RPG style game engine for ChaiScript. I want to be able to do all of the graphics with SVG and need joystick input. I also need the libraries I use to be opensource and compatible with the BSD license. I'm familiar with allegro, ClanLib, and SDL. As far as I can tell, none of these libraries have built in or obvious integration for SVG

  • Sorry for the ackward title, but i don't know what to call it exactly. I'm looking for code or libraries that allow easy creation of commonly used user input devices, and code/libs that make gaming controls easy to set up (i'm using SFML and Box2D at the moment) I don't want to re-invent things like buttons, arrows being placed wherever the mouse is located, and more game specific items... that.... Allows easy setup of controls for a game Makes events like clicking and holding on an object easy to setup, as so objects can be dragged around afterward Are there any simple libraries

  • I have a simple question. For people that know and built ogre3D from source as a Static library, what is the order of which the libraries should be linked? The libraries I need to be organized are: Ogre Plugins 'libOgreMain.a' Ogre RenderSystems Boost(version 1.47)link Ogre's Dependencies The reason I'm asking is because in the Ogre forums, I have asked about this and didn't get a good reply... yet. The other reason is because even though I link to the boost library, I get this error: undefined reference to '_imp___ZN5boost6thread20hardware_concurrencyEv' My compiler is MinGW

  • GameSubsystems) can implement registerComponent(Component*). pro. Components and GameSubystems know nothing about each other. con. In C++ it would look like ugly and slow typeid-switch. Questions: Which approach is better and mostly used in component-based design? What Practice says? Any suggestions about implementation of Approach 4? Thank you. ... Components. 3: Component registers itself in GameSubsystem(s). We know at compile-time that there is a GameSubsystemRenderer, so let's ComponentImageRender will call something like

  • positions Normals UVs Texturing information Animation information Collision mesh Maybe some other things... I am aware, that I could and (maybe should) create my own file format that brings these data from..., that will let me import 3d meshes into my game, ready to use? The solution/middleware should be: easy to use easy to port efficient not consuming too much memory with unnecessary things containing all the above mentioned features plus things I haven't considered yet Programming language is: C++. Currently, I'm developing under Windows 7, Visual Studio 2010, and OpenGL ES 2.0 emulator. Later

  • hey so I've decided to Code my own 2D soft-body physics engine in C++ since apparently none exist and I'm starting only with a general idea/understanding on how physics work and could be simulated: by giving points and connections between points properties such as elasticity, density, mass, shape retention, friction, stickiness, etc. What I want is a starting point: resources and helpful examples/sites that could give me the specifics needed to actually make this such as equations and required physics knowledge. It would be great if anyone out there also would give me their attempts

  • ( 10.0f, 0.0f, 0.0f, 1.0f ); // z rotation Are my maths and approach correct, or am I completely wrong? Finally, I'm using the glm library with OpenGL / C++ for this. Is the order of x rotation...I'm attempting to create a 3d tree procedurally. I'm hoping that someone can check my vector rotation maths, as I'm a bit confused. I'm using an l-system (a recursive algorithm for generating... by +10 degrees in the X axis and a similar amount in the Z axis, relative to the trunk. I know that I should keep a rotation matrix at each branch, so that it can be applied to child branches, along

  • for looking for a library for logging (like, I don't have time or I'm not confident in my ability to write one correctly for my case). Assuming that I need : performance ease of use (allow streaming or formating or something like that) reliable (don't leak or crash!) cross-platform (at least Windows, MacOSX, Linux/Ubuntu) Wich logging library would you recommand? Currently, I think...I know a lot of logging libraries but didn't test a lot of them. (GoogleLog, Pantheios, the coming boost::log library...) In games, especially in remote multiplayer and multithreaded games, logging