Unexpected CFrame behavior when controlling camera orientation

Hey there,

I’m working on a script that involves controlling the orientation of the camera in my game. However, I’ve encountered a strange issue where applying rotations in different orders seems to produce different results.

Here’s a snippet of the code:

print("X:", CFrame.Angles(0, math.rad(CameraController.Delta.Y), 0))
print("Y:", CFrame.Angles(math.rad(CameraController.Delta.X), 0, 0))

print("1:", CFrame.Angles(math.rad(CameraController.Delta.X), math.rad(CameraController.Delta.Y), 0))
print("2:", CFrame.Angles(0, math.rad(CameraController.Delta.Y), 0) * CFrame.Angles(math.rad(CameraController.Delta.X), 0, 0))

In theory, the values printed should be identical, as they represent the same final orientation of the camera.

However, I’m seeing different results printed in the console:

X: 0, 0, 0, 0.857167304, 0, 0.515038073, 0, 1, 0, -0.515038073, 0, 0.857167304
Y: 0, 0, 0, 1, 0, 0, 0, 0.992546141, -0.121869348, 0, 0.121869348, 0.992546141

1: 0, 0, 0, 0.857167304, 0, 0.515038073, 0.0627673566, 0.992546141, -0.104462422, -0.511199057, 0.121869348, 0.850778103
2: 0, 0, 0, 0.857167304, 0.0627673566, 0.511199057, 0, 0.992546141, -0.121869348, -0.515038073, 0.104462422, 0.850778103

If anyone can offer insight on why this is happening, I’d really appreciate that.

Thanks!

1 Like

Thought experiment: look ahead imagine that is looking down the the z axis. Turn head 90 deg left around the y axis. Turn your head 90 deg counter clockwise around z axis. You are looking down the length of your left arm.

Look straight ahead. Turn your head 90 deg counter clockwise around z axis. Turn head 90 deg left around y axis. You are now looking sideways away from your shoulder.

Caveat: Do NOT try this in real life.

Rotating around different axis seem to be … non-commutative? Which is a little confusing as normal multiplication is commutative. 3 * 2==2 * 3.

1 Like

The normal rules of commutativity that we’re used to in regular arithmetic don’t seem to apply.