Need help understanding all parameters of CFrame.new()

  1. What do you want to achieve? Keep it simple and clear!
    I want to have a more understanding of CFrame’s

  2. What is the issue? Include screenshots / videos if possible!
    I don’t understand every parameter in CFrame.new() I only know the first 3 values (x,y,z)
    But I think there are more parameters, and I don’t understand it

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried looking at stuff in the API Refrence and DevForum and still can’t understand it

If you can help. Thank you :slight_smile:

1 Like

CFrames are made of a x,y,z position as well as a rotation matrix. The rotation matrix is a 3x3 matrix containing 3 vector3s.

e.g.
1,0,0 - xVector
0,1,0 - yVector
0,0,1 - zVector

the first vector is the xVector, or RightVector. This is a unit vector that is facing right. If you ever select a part in roblox studio, then use the Move tool, this would be the red arrow that appears.

the second vector is the yVector, or UpVector. This is a unit vector that is facing upwards. The color of this arrow is green.

the third vector is a little more confusing. Generally speaking, forward is negative on the Z axis. However, if you ever see CFrame.LookVector it is actually inverted. positive Z is forward. However, in our rotation matrix this shouldn’t be the case. The LookVector is the facing direction of the part, but in the rotational matrix zVector is the real forward direction. This arrow will appear blue.

Rotation matrices should be orthonormalized. This basically means every vector discussed aboved is 90 degrees of one another. The up vector is 90 degrees of the rightvector and lookvector, etc etc. Roblox, by default, orthanormalizes rotation matrices. So you dont need to worry about this. However, the rule that every vector should be 90 degrees of one another lets us use the cross product.

For example, lets say we have a rotational matrix like so

1,0,0
0,1,0
but the last one is unknown

luckily, since its orthanormalized, we can use the cross product of the first two vectors to get the final one.

(1,0,0):cross(0,1,0) = 0,0,1

so our rotational matrix is
1,0,0
0,1,0
0,0,1

tada! If we ever want to construct a CFrame from a rotational matrix, we can use the CFrame.fromMatrix operator.

Whenever you use CFrame.Angles, you aren’t using a rotational matrix, you’re using euler angles. Generally speaking, you want to stick to using rotational matrices to avoid issues with angular space, e.g gimble lock. Sticking to rotational matrices also is more performant, since computers are much better at running matrix multiplication than trig functions ontop of matrix multiplication.

If you’re more curious, I suggest you look into axis angle theorem and other really cool stuff.
if you have any questions, please ask!

2 Likes

so bascially.
First 3 parameters are Position
4 5 6 are Vectors

4 is X Vector
5 is Y Vector
6 is Z Vector?

1 Like

no, there are 12 parameters, the first 3 is the position, second 3 is the xVector, the next 3 are the yVector, and the last 3 are the zVector. Vectors in this case are Vector3s with a X Y and Z coordinate

3 Likes

Ah I understand it now.

Would there be any use of the Vector Paramters when scripting?

If we wanted a part to face another part for example. You could do this:

local zVector = -(part1.Position - part2.Position).Unit
local yVector = part.CFrame.UpVector
local xVector = zVector:Cross(yVector)

part1.CFrame = CFrame.fromMatrix(part1.Position, xVector, yVector, zVector)

we could use CFrame.lookAt so is there even advantages instead of using CFrame.lookAt?

Yes, CFrame.lookAt can be used as well but on an internal level this is exactly what it does. CFrame.lookAt isnt perfect either, there is an optional UpVector argument as the 3rd parameter. Remember, you need atleast 2 vector3s to construct an orthonormalized rotational matrix. When you leave the UpVector parameter blank, it assumes (0,1,0) or straight up which isnt always correct. If you flipped a part 180 degrees upside down then used cframe.lookat without defining the UpVector, it will flip back upwards.

How to rotate a part using CFrame.new?

1 Like