# C Matrix and Vector Math Library for 2D game engine

Currently, I am in the process of writing a 2D game engine in Cython. One of the performance bottlenecks that I am facing is with performing 3D transformations every frame on the CPU to send to my OpenGL shaders. I was wondering if anyone could recommend some fast C libraries that can handle transformations/translations/rotations/scaling/etc. I would like to use such a library so that I could just make a wrapper to this faster code in cython instead. Ideally, it would support all of the data types that can be found in GLSL, such as mat2/3/4, vec2/3/4, and quaternions. I am aware of GLM, but that unfortunately is a C++ library. Is there any good equivalents for that in C? I looked at this reddit and saw some alternatives, such as GLKMath and kazmath, but I wanted to know if there were any others since that post was 3 years old. If you have used these or other libraries, what has been your experience with them?

• perrygeo.com/… may be of help Jul 9 '17 at 6:15
• @SteveBarnes Thanks, took a look at that link! It looks like it is mostly just a bunch of performance tips for using numpy in cython. While the parallel processing tricks are new to me, I have played around with most of the typing stuff already. My current Cython implementation, by virtue of being hardcoded for some fixed sizes of matrices, is actually slightly faster than my original attempt using numpy + python. Jul 9 '17 at 6:23
• I am a little confused why you need 3D transformations in a 2D game! Jul 9 '17 at 6:41
• @SteveBarnes Typically in OpenGL, you need to work with a vertex and a fragment shader. The vertex shader is used to position where the quads (my 2D sprites) show up on the screen. You first need to pass in the locations of the vertices of each quad. Then, you multiply each vertex on the GPU with a model, view, and projection matrix. These set up the size of the screen as well as do the rotation, scale, and positioning of the sprites away from the origin. It looks like people generally compute these model, view, and projection matrices on the CPU, which is why a library would be useful. Jul 9 '17 at 6:52
• @SteveBarnes Another use is for grouping sprites and performing animations in a scenegraph/hierarchy. Let's say I have a character with a body sprite and an arm sprite. The arm is attached to the body, so we can think of the body as a parent sprite and the arm as a child sprite. Whatever transformation happens to the body also happens to the arm. With 3D transformations, I can capture this parent-child relationship by multiplying the parent's transformation matrix with the child's, so the arm can say, swivel, while moving attached to the parent (body). Hope that clarifies it a bit. Jul 9 '17 at 6:58