2
\$\begingroup\$

I am writing a 3D math library for general game development and want to know what type of matrix classes I should include. Is just a 4x4 matrix enough, or must I provide 3x4 and dedicated rotational matrices?

According to this

In order to use the vector registers of modern CPUs (SSE, Altivec, SPUs) you have to pass 4x 32 bit floats anyway (its a 128 bit register), plus you have to take care of the alignment, usually 16 bytes. So you don't have the chance to safe the space for the 4th component anyway.

and from Game Engine Architecture

The rightmost column of an affi ne 4 × 4 matrix always contains the vector [ 0 0 0 1 ]T . As such, game programmers oft en omit the fourth column to save memory. You’ll encounter 4 × 3 affine matrices frequently in game math libraries.

Since 4x3 matrices are just created for convenience and since you need 128 bits to use the vector register, would it be best to just have a single 4x4 matrix class handle all needed matrix operations?

\$\endgroup\$
2
\$\begingroup\$

Everything is an optimization of the 4x4 matrix when it comes to 3D math. A 3x4 is about saving memory because the last column/row for non-projection matrices is [ 0 0 0 1 ]. Pure rotational 3x3 matrices are extremely convenient because you can invert them by just transposing them.

For animation and camera systems, quaternions are ideal for lots of reason, and they are essentially just 4-vectors.

There are similar trade-offs with Vector2, Vector3, and Vector4 types.

If you are working on Microsoft platforms at all, take a look at DirectXMath and SimpleMath.

BTW, if you are writing a 3D math library as an educational exercise, that's great. If you are writing it for production use, consider not doing that and using something that already exists. Math libraries are a dime-a-dozen and they are often terrible in terms of performance. Some games end up shipping with 5 different math libraries being used at once, and they are not particularly efficient when used that way.

RE: Updated questions about 128-bit registers

DirectXMath's design, for example, is intended to address this. The 'work-horse' types are XMVECTOR which is always a 4-vector, and XMMATRIX which is always a 4x4 matrix, both of which directly map to SIMD registers on the platform. However, there are a rich set of types for use in data-structures including XMFLOAT2, XMFLOAT3, XMFLOAT4, XMFLOAT3X3, XMFLOAT4X4, XMFLOAT4X3, etc. to facilitate the right trade-offs in storing the data in memory, serializing it from disk, etc. So yes, when the computations are actively going on, you are dealing with 4x4 matrices, but it's not a particularly efficient way to store say 10,000 animation key-frames in memory across an entire game.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the answer. I was working on an engine, hopefully working to create a game. I am using D, which doesn't seem to have many math libraries out there. I updated my question, does it have any relevance? \$\endgroup\$ – Rishub Nagpal Jul 25 '14 at 18:38
  • \$\begingroup\$ Odd language choices can be fun, but it makes life really hard when it comes to finding existing solutions you can adopt. That's a large part of why modern Microsoft platforms have reemphasized C++, although C# and JavaScript+HMLT5 are supported as well. \$\endgroup\$ – Chuck Walbourn Jul 25 '14 at 18:41
  • \$\begingroup\$ You are right, but the team I am working with is more than capable of overcoming these challenges. I am just trying to find a way to minimize the number of classes we need for matrix. I think the best solution would be a 4x4, 4x3 and 3x3 class. Would you agree? \$\endgroup\$ – Rishub Nagpal Jul 25 '14 at 18:45
  • \$\begingroup\$ I'd be concerned about writing a game in D based purely on the statement "While D is fully capable of interfacing to C, its ability to interface to C++ is much more limited.". The game industry is full of people who are convinced that they will write a bespoke solution to 100% of the game, only to find that it's not only not possible to write a high-quality solution to every single problem facing game-creators, but it's not a good use of time or money. A 100% custom engine written in a non-standard language is more an art project than a retail game. \$\endgroup\$ – Chuck Walbourn Jul 25 '14 at 18:48
  • \$\begingroup\$ Yage3D is written in D, and the developer created remarkable tech demos before development stopped. While it is difficult, it is achievable \$\endgroup\$ – Rishub Nagpal Jul 25 '14 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.