Help with understanding the rotation matrix of CFrame

Hello I am learning CFrames but I have trouble grasping the rotation side of them. How does rotation around an axis work and how can I manipulate it?.

To help express my thoughts imagine a part with a position and rotation that is of it’s origin. When using the CFrame:GetComponents() function a list is returned as so:

Position) Rotation)
(x, y, z) (x, y, z) (x, y, z) (x, y, z)
0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1

Each of the values which are “1” represent a coordinate of a unit vector with normal direction to the parts face in it’s respective vector, but how can I manipulate the rotation matrix of a part to make it face in a random direction. For instance, how could I rotate this part 90 degrees by hard coding it into the matrix?. Sorry if that does not make complete sense :slight_smile: .

1 Like

Okay, so let me clear up a few things.
You will need to have a basic understanding of Vectors to know CFrame

  • Unit Vectors mean direction
  • Normal means perpendicular

As you know there are 4 Vectors that represents 12 numbers in CFrame. The first three represent Position.

The next three represent the direction the object is facing. Commonly known as the LookVector

The next three represent the UpVector and the last three represent the RightVector.

realize that they are all perpendicular to each other One thing you might actually want to realise is that we don’t even need the last three.

If I had a block facing a point there is only one way that block can move right? Around the point.

Rotation

If you want to rotate a CFrame then you can easily use the functions.

However, if you want to hardcode your rotations by rotating the vectors then you will have to know about Quaternions. I won’t do into detail about it now.

Put simply a Quaternions is a Vector and a Rotation. The Vector represents the and of rotation and the rotation represent how much it rotated

If you want to rotate a CFrame it is much easier.

local RotatedCFrame = CurrentCFrame * Rotation CFrame

CFrames are not commutative meaning ab does not equal ba.
We combine a Cframe and then we can multiply it with a rotational one to rotate that CFrame.

For extra points you can even rotate relative to itself if you use this method

local New frame = CurrentCFrame:ToWorldSpace(RotatedCFrame)

Let me know if I explained things wrong hopefully this helps :smiley:

1 Like

Thank you a lot. Could you please elaborate on “We don’t even need the last three.” Why don’t we need the RightVector?. One thing that has also been confusing me is CFrame:ToWorldSpace. In the understanding CFrames article it shows use of this by offsetting the position of a cube by 2 in the y axis relative to another cube, putting the first cube 2 units in front of the second cube while keeping rotation. But since the cube is using object space wouldn’t it make more sense for it to be called CFrame:ToObjectSpace ?

1 Like

The reason why we don’t need the last three is simply because all UnitVectors in CFrame are perpendicular to it.

You can see this with your left :raised_hand: hand.

If you open your Thumb Index and Mdle finger. Then make them all perpendicular to each other.

images

You can see that if you rotate your hand in one axis 2 Fingers will move with it.

The reason why is because your fingers have to be perpendicular.

If you took physics and seeen this before you might also know that the last directon can be calculated.

To position and rotate a CFrame we only need to directions

It is the same with CFrame. Because all the Directions are perpendicular you have to move 2 or more Directional Vectors at a time. The final Vector is calculated internally so we don’t need to calculate it ourselves.

If you read CFrame.fromMatrix you can also see the same thing.

I’m not really good at explaining but if you don’t understand don’t worry. You will be able to understand if you practice and experiment.

ToWorldSpace is actually “FromObjectSpace”. That’s how I like to think of it.

1 Like