Tutorial:All About CFrames

Hello! This tutorial will hopefully give you a deeper understanding of CFrames.

Note that this tutorial will get very technical and require a basic understanding of mathematics.

What is a CFrame?
A CFrame, or coordinate frame, is a set of 12 numbers defining the position and orientation of a part. You will notice that the CFrame property of a part is not in the Properties window, but are replaced with Position and Orientation to make it easier to move and rotate a part.

The CFrame Matrix
We arrange the 12 numbers in a CFrame into a matrix. What is a matrix? In math, it is basically a way of organising a bunch of numbers into a neat array. A good example of a matrix is this: Usually, in a matrix, each row/column should have some sort of relationship. The above case is an example of a "magic square," which means that every horizontal, vertical and diagonal line adds up to 15.

A CFrame has a matrix too, only it's a 3x4 matrix, therefore making 12 values. The bolded values below are just the titles of the rows. Just one note that you cannot get values by multiplying two numbers. Do not attempt to find xz by multiplying x and z. Note that each value has different names, like instead of xx, others would call it r11 or m01. For this tutorial, I will be using xx, yx, and so on.

Let's start with row p. Row P represents the position in the form of (x,y,z). It's as easy as that.

The Rotational Aspects
The x, y, and z rows represent the rotation of the object. Here is a sample of three of these values and how they are calculated.

$$xx = \cos y \times \cos z$$

$$yx = -\sin z \times \cos y$$

$$xy = \cos z \times cos x - \sin z \times \sin y$$

You can see that these equations are complicated and require a complex knowledge of trig. You do NOT need to calculate these values, this is just a tutorial to show you how the matrix works.

Vectors
Now we move onto vectors.

What is a Vector?
By definition, a vector is an object with direction and magnitude. For the purpose of this tutorial, we will assume that a vector is like a ray; a line that starts at one point (the part) and goes infinitely in one direction. Of course, they are not the same, but this is to help understand what a vector is.

Now, if you use the Move Tool on a part, you will see six arrows appear: two red, two green and two blue. A more inexperienced developer may think that each arrow colour represents an axis, but this is not true, as if you rotated the part, the arrows would rotate with it. No. Each arrow represents a vector.

You see, each part has six faces, all of which are assigned a name: front, back, left, right, top or bottom. A vector is like the arrows in the Move Tool; it is a line that starts at the centre of a face and extends outwards infinitely.

Each part has three named vectors:

A LookVector is the one that extends from the front face and is represented by the blue arrow.

A RightVector is the vector that extends from the Right face and is represented by the red arrow.

An UpVector extends from the Top face and is represented by the green arrow.

Relationship with the Matrix
How does all this tie in with the matrix? Well, a vector is represented by three numbers in the form of . So, row X's numbers are the numbers in the part's RightVector, row Y for the UpVector, and z for LookVector.