CFrame.fromMatrix does not normalize vectors passed into it

When using CFrame.fromMatrix, it does not normalize the vectors passed in. I’m not sure if it is intentional or not, in which case, it should still be documented, since the current documentation does not mention that vectors passed into fromMatrix are not normalized. If you only pass in vX, and vY, then vZ is normalized, but vX and vY are not.

For example, given:
CFrame.fromMatrix(Vector3.new(), Vector3.new(2, 0, 0), Vector3.new(0, 3, 0)), you get the following CFrame:

0, 0, 0
2, 0, 0
0, 3, 0
0, 0, 1

I ran into this issue when over time, using the fromMatrix function led to multiple floating point errors accumulating over time and leading to either infinite numbers, or zero.

Expected behavior

I would expect:
CFrame.fromMatrix(Vector3.new(), Vector3.new(2, 0, 0), Vector3.new(0, 3, 0))
to give the CFrame:

0, 0, 0
1, 0, 0
0, 1, 0
0, 0, 1

If it is the case that this is the intentional behaviour, it should be clearly documented on the CFrame documentation page.

And should read:
“Returns a CFrame from a translation and the columns of a rotation matrix. If vZ is excluded, the third column is calculated as vX:Cross(vY).Unit. The vectors vX and vY are not normalized when passed into this function.”

Or, if it is unintended behaviour, should be fixed and read:
“Returns a CFrame from a translation and the columns of a rotation matrix. If vZ is excluded, the third column is calculated as vX:Cross(vY).Unit. The vectors vX and vY are normalized when passed into this function.”

Either way, it should be better documented what the internal and intended behaviour of this function does and should do.

2 Likes

This is intentional: CFrames are not necessarily orthonormal rotation matrices. You can use this to e.g. apply screen-space camera offsets.

We will update the docs.

6 Likes

Also note, you don’t have to roll the solution yourself, CFrame has an Orthonormalize method on it.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.