I use CFrame frequently, and I'd like to fully comprehend it. One day, I hope to be as good with it as BlueTaslem (it's a long shot, but good to have goals, no?).
A couple of things that I'm struggling with: *CFrame.p, .x, .y, .z, -- I can rationalize .p through .z, but I can't seem to use them. *lookVector *The rotation matrix
If you could help with anything you see there, or maybe something that isn't well known about CFrame, please do so. I will appreciate it!
Hi there!
CFrame
s are stored as matrices, but that detail is rarely actually important. Their purpose is to store (uniquely) position and rotation.
CFrame.p
is a Vector3
giving the position of the CFrame (so a part with a CFrame
of C
will have a Position
of C.p
)
CFrame.lookVector
is also a Vector3
, specifically a unit vector (a vector of magnitude one). It specifies which direction the CFrame is facing. A Part whose lookVector
is in a direction has its front face facing in that direction.
For most manipulations, understanding just these two is sufficient, because the last piece of information is just twisting around the axis defined by the lookVector (facing direction) -- that is, which way is "up".
Because of how CFrames are matrices, multiplication corresponds to rotation (when the second CFrame is at the origin). So C *CFrame.Angles(0,1,0)
is C
rotated 1 radian around the normal of its top surface.
I find the methods cframe:toObjectSpace(cframe)
and cframe:toWorldSpace(cframe)
to be very useful as well! They define the relationship of two CFrames.
Here's the general idea of how those work:
If part A is three studs in front of part B's position in the direction of B's front face, then
B.CFrame:toObjectSpace(A.CFrame)
will be (0,0,-3)
. :toWorldSpace simply undoes this. So
Part.CFrame:toWorldSpace(CFrame.new(0,0,-3))
will correspond to a CFrame 3 studs in the direction of Part
's front face from its Position.
Hopefully this is helpful. If you want me to expand or clarify on anything else let me know and I will edit this answer.
EDIT1: I forgot to add a few things. You can make CFrame objects in a large number of ways.
CFrame.new(vec)
produces a CFrame at the position of vec
(a Vector3) with a default rotation.
CFrame.new(x,y,z)
also produces a CFrame at the position (x,y,z).
CFrame.new(vecAt,vecFacing)
produces a CFrame at the position of vecAt
(a Vector3) with its front face pointing towards vecFacing
(another Vector3) with its top facing up.
CFrames can be multiplied, as I said above, but usually you will only multiply by the result of CFrame.Angles(xangle,yangle,zangle)
which produces a CFrame to rotate another CFrame by those angles.
A Vector3 can be added to a CFrame to translate the CFrame by that amount (just like adding Vector3's). Note that in an addition of a CFrame and a Vector3 the CFrame has to come first.