Caution: Matrix4.IDENTITY


10-06-2015 09:44:14

Maybe it's clear to everyone else but me but as I just wasted quite some time on this Mogre behaviour, I thought I might as well share this:

When you assign Matrix4.IDENTITY to some variable and then alter this variable you alter Matrix4.IDENTITY as well!
Matrix4 is a class and you are only assigning a reference to Matrix4.IDENTITY instead of, for example, when you use constants of the Vector3 struct.

So when doing something like this...

Matrix4 myMatrix = Matrix4.IDENTITY;
myMatrix.MakeTransform(Vector3.NEGATIVE_UNIT_X, Vector3.UNIT_X, Quaternion.ZERO);

...every piece of code which uses Matrix4.IDENTITY afterwards is broken because it now returns the value of the myMatrix transformation.

To avoid this, simply use...

Matrix4 myMatrix = new Matrix4(Matrix4.IDENTITY);