Matrices: Arrays or separate member variables?
Posted
by
bjz
on Game Development
See other posts from Game Development
or by bjz
Published on 2012-07-07T14:15:30Z
Indexed on
2012/07/07
15:23 UTC
Read the original article
Hit count: 228
I'm teaching myself 3D maths and in the process building my own rudimentary engine (of sorts). I was wondering what would be the best way to structure my matrix class. There are a few options:
Separate member variables:
struct Mat4 { float m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44; // methods }
A multi-dimensional array:
struct Mat4 { float[4][4] m; // methods }
An array of vectors
struct Mat4 { Vec4[4] m; // methods }
I'm guessing there would be positives and negatives to each.
From 3D Math Primer for Graphics and Game Development, 2nd Edition p.155:
Matrices use 1-based indices, so the first row and column are numbered 1. For example,
a12
(read “a
one two,” not “a
twelve”) is the element in the first row, second column. Notice that this is different from programming languages such as C++ and Java, which use 0-based array indices. A matrix does not have a column 0 or row 0. This difference in indexing can cause some confusion if matrices are stored using an actual array data type. For this reason, it’s common for classes that store small, fixed size matrices of the type used for geometric purposes to give each element its own named member variable, such asfloat a11
, instead of using the language’s native array support with something like floatelem[3][3]
.
So that's one vote for method one.
Is this really the accepted way to do things? It seems rather unwieldy if the only benefit would be sticking with the conventional math notation.
© Game Development or respective owner