Cframe roblox что это

Обновлено: 16.05.2024

A CFrame, or Coordinate Frame, is a DataType containing positional and rotational data. It can be used to accurately position BaseParts through their CFrame property, which unlike Position, allows the part to be rotated.

You can position a part just like you would using the Position property.

To create a rotated CFrame at the origin, use:

It is worth noting that the rotations are not applied simultaneously, but sequentially. So the previous command rotates in the part's X-axis, then the part's Y-axis, and then the part's Z-axis.

For an explanation of the parameters, see BasePart object, is hidden in the properties panel. The "Value" property of a CFrameValue is also hidden, thus needing to be modified from a Script, a Plugin, or through the Command Line.

CFrame + or - Vector3

Adding or subtracting Vector3s to CFrames is very straight forward. We simply add/subtract the vector x, y, and z to the CFrame x, y, and z and the rotation aspect remain unchanged.

And of course a test.

The Inverse of a CFrame

This is one of the more challenging aspects of the CFrames for most people. In this article we will not be covering how to actually calculate the inverse but rather how to use it.

Near the end of the section on CFrame against CFrame multiplication it was mentioned that multiplication is not always commutative. This is NOT true for the inverse of a CFrame multiplied against the CFrame is was derived from. No matter if you pre or post multiply a CFrame by its inverse it will ALWAYS return the identity CFrame!

The trick to using the inverse of a CFrame is to write out an equation and then to use what we know about the identity CFrame and the non-commutative property of CFrame multiplication. Let’s do some examples.

Reverting to Original Values

Let’s say we have two CFrames and we multiply them together to get a new CFrame.

Say we are given only cf and cf1, but we want to find cf2. How can we do that? To start let’s look at the equation for cf.

We can then apply what we know about inverses to solve for cf2.

Sure enough when we test we can verify this.

note the slight variation in output is due to floating point math imprecision

Say we had cf2 and cf, but not cf1. To solve for that we follow a similar procedure.

Once again testing to verify.

note the slight variation in output is due to floating point math imprecision

You might be asking why does the pre/post multiplication matter? To see why let’s purposefully go through the steps where we pre-multiply cf by cf2:inverse() and see where that leads us.

The lesson here is that order matters and that what we do to one side we must do to the other and that includes whether or not we pre or post multiply!

Rotating a Door

Let’s say we want to CFrame a door opening. This might be difficult to someone learning CFrames because when we use the CFrame.Angles function on a part’s CFrame and update, it spins from the center.

Ideally we want to have our door spin around a hinge of some sort. This means we need to find a way to get our hinge to act as the center of rotation. We we know we can rotate the hinge in a similar way to how we rotated the door earlier.

If we could somehow calculate the offset of the door from the un-rotated hinge we could apply that offset to the rotated hinge and get the rotated door CFrame. In other words we need to solve offset in the following:

The key to finding the offset value is to use inverses! Remember, if we do something to one side of an equation we have to do it to the other.

Now that we have the offset it’s just a matter of applying it to the rotated hinge!

Try Yourself: Welds

Welds are subject to the following constraint.

Using what you know about inverses try to solve for Weld.C0 and Weld.C1. Try not to look at the answer til you’ve tried yourself.

Components

Positional

The positional component is available as a datatype/Vector3 in the Position property. In addition, the components of a CFrame’s position are also available in the X, Y and Z properties like a Vector3. A CFrame placed at a specific position without any rotation can be constructed using CFrame.new(Vector3) or CFrame.new(X, Y, Z) .

Rotational

CFrame stores 3D rotation data in a 3-by-3 rotation matrix. These values are returned by the CFrame:GetComponents function after the X, Y and Z positional values. This matrix is used internally when doing calculations involving rotations. They use radians as their unit (for conversion to degrees, use math.rad / math.deg ).

The matrix below represents the components of a CFrame’s rotation matrix and their relationship with the various vector properties available (LookVector, RightVector, etc). Although the individual components of the rotation matrix are rarely useful by themselves, the vector properties which derive from them are much more useful.

RightVectorUpVector–LookVector †
XVectorR00R10R20
YVectorR01R11R21
ZVectorR02R12R22

†Unlike RightVector and UpVector, LookVector represents the negated right/third column components.

Properties

The 3D position of the CFrame

The x-coordinate of the position

The y-coordinate of the position

The z-coordinate of the position

The forward-direction component of the CFrame’s orientation. Equivalent to: Vector3.new(-r20, -r21, -r22)

Adding a CFrame’s LookVector to itself would produce a CFrame moved forward in whichever direction the CFrame is facing by 1 unit:

The right-direction component of the CFrame’s orientation. Equivalent to the first/left column of the rotation matrix, or Vector3.new(r00, r01, r02)

The up-direction component of the CFrame’s orientation. Equivalent to the second/middle column of the rotation matrix, or Vector3.new(r01, r11, r12)

Equivalent to the first column of the rotation matrix, or Vector3.new(r00, r01, r02)

Equivalent to the second/middle column of the rotation matrix, or Vector3.new(r10, r11, r12)

Equivalent to the third/bottom row of the rotation matrix, or Vector3.new(r20, r21, r22)

CFrame

This page isn’t a tutorial! Here, you will find a technical description of the CFrame data type. To learn the basics in friendly manner, see articles/Understanding CFrame|Understanding CFrames instead!

CFrame, short for coordinate frame, is a data type that describes a 3D position and orientation. It is made up of a positional component and a rotational component. It includes essential arithmetic operations for working with 3D data on Roblox.

The CFrameValue object can be used to store exactly one CFrame value in a Roblox object.

Contents

CFrame Methods

In this last section we will go over each of the transformation methods and some of the intuition you can apply to them.

CFrame:ToObjectSpace()

Equivalent to CFrame:inverse() * cf

We actually already know what this method does from when we got the offset when we were trying to rotate the door. This method calculates the offset CFrame needed to get from CFrame to get to cf

This can be easily verified in the following:

CFrame:ToWorldSpace()

Equivalent to CFrame * cf

Since this method simply does CFrame multiplication it’s not super exciting. However, it’s name might help give us a bit more intuition in regards to what the multiplication operation is actually doing. We saw from the CFrame:toObjectSpace(cf) method that it returns the offset between two CFrames. We also noted that when we multiplied CFrame by the offset we ended with cf . So what is actually happening when we multiply two CFrames is that we are treating the second CFrame as an offset.

CFrame:PointToObjectSpace()

Equivalent to CFrame:inverse() * v3

This method takes a point in 3D space, makes it relative to CFrame.p , and then converts it to an offset.

An alternative equivalent of this method would be (CFrame - CFrame.p):inverse() * (v3 - CFrame.p)

CFrame:PointToWorldSpace()

Equivalent to CFrame * v3

Since we already covered the intuition behind CFrame * v3 there’s not much more to say about this method other than what you already know. However, we will once again note that this method is the equivalent to applying an offset without the rotation aspect.

note the slight variation in output is due to floating point math imprecision

CFrame:VectorToObjectSpace()

Equivalent to (CFrame-CFrame.p):inverse() * v3

This method is very similar to the alternate form of the CFrame:pointObjectSpace(v3) method. The main difference is that the v3 no longer subtracts CFrame.p . This means the steps are very similar with one difference it does not make v3 relative to the CFrame.p , it assumes our input v3 is already relative.

note the slight variation in output is due to floating point math imprecision

We can see this is equal almost equal to Vector3.new(1, 0, 0), the identityCFrame’s rightVector. In a perfect world these two would be exactly equal, but as mentioned above the slight differences are do to floating point math imprecision.

CFrame:VectorToWorldSpace()

Equivalent to (CFrame-CFrame.p) * v3

An alternative equivalent of this method would be CFrame * v3 - CFrame.p which basically tells us this pretty much is the same as the the CFrame:pointWorldSpace(v3) method except that it does not add the CFrame.p (as we learned in the CFrame * v3 section).

CFrame * Vector3

Since we now know that CFrames are actually 4x4 matrices we can now get a look at how they multiply against vectors. The operation of multiplying a CFrame against a Vector3 looks like this in matrix form.

CFrameMath_cfv3Multiply.jpg

Thus we can write a function as such

Once again we can test.

Now unlike the CFrame * CFrame multiplication the CFrame * Vector3 multiplication can be broken down into something that is a bit more intuitive. Let’s slightly adjust the notation.

CFrameMath_cfv3Multiply2.jpg

Notice anything about the vectors we are multiplying against vx, vy, and vz? They’re the right, up, and back vectors we learned about earlier! We can rewrite our function to represent this.

This also helps us visualize what the operation is actually doing.

CFrameMath_cfv3MultiplyVisual.jpg

CFrame * CFrame

CFrames are actually 4x4 matrices of the following form:

This means we can easily multiply two CFrames together by simply multiplying two 4x4 matrices together!

Thus we can write a function to multiply two CFrames!

Alternatively a solution using loops:

Finally, a test to verify.

Something very important to note from all this. CFrame multiplication is not commutative. This means that a * b is not necessarily equal to b * a.

There are a few exceptions to this rule one of them is inverses, which we will talk about later, and the other is the identity CFrame which we will talk about now.

The Identity CFrame is as follows:

If we pre or post multiply a CFrame by the identity CFrame we simply get the original CFrame as if the multiplication never happened.

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:

Magic Square
6 1 8
7 5 3
2 9 4

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.

CFrame Matrix
p px py pz
x xx yx zx
y xy yy zy
z xz yz zz

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.

<\displaystyle xx=\cos y\times \cos z></p>
<p>

<\displaystyle yx=-\sin z\times \cos y></p>
<p>

<\displaystyle xy=\cos z\times cosx-\sin z\times \sin y></p>
<p>

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.

CFrame Math Operations

A CFrame is made up of 12 separate numbers, we call these components. We can simply find out what these numbers are by calling the CFrame:components() method which returns said numbers.

We can also input these 12 numbers directly when defining a CFrame.

The first three of the 12 numbers are the x, y, and z components of the CFrame, in other words the position. The rest of the numbers make up the rotation aspect of the CFrame. These numbers may look daunting, but if we organize them a bit differently we can see that the columns represents the rightVector, upVector, and negative lookVector respectively.

Having these vectors to visualize helps us see what the rotation numbers of our CFrame are actually doing. We can see that they represent three orthogonal vectors that all trace a 3D sphere of rotation.

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 <i,j,k>. So, row X's numbers are the numbers in the part's RightVector, row Y for the UpVector, and z for LookVector.

Functions

Returns the inverse of this CFrame

Returns a CFrame interpolated between this CFrame and the goal by the fraction alpha

Returns a CFrame transformed from Object to World space. Equivalent to [CFrame * cf]

Returns a CFrame transformed from World to Object space. Equivalent to [CFrame:inverse() * cf]

Returns a Vector3 transformed from Object to World space. Equivalent to [CFrame * v3]

Returns a Vector3 transformed from World to Object space. Equivalent to [CFrame:inverse() * v3]

Returns a Vector3 rotated from Object to World space. Equivalent to [(CFrame - CFrame.p) *v3]

Returns a Vector3 rotated from World to Object space. Equivalent to [(CFrame:inverse() - CFrame:inverse().p) * v3]

Returns the values: x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22, where R00-R22 represent the 3x3 rotation matrix of the CFrame, and xyz represent the position of the CFrame.

Returns approximate angles that could be used to generate CFrame, if angles were applied in Z, Y, X order

Returns approximate angles that could be used to generate CFrame, if angles were applied in Z, X, Y order

Returns approximate angles that could be used to generate CFrame, if angles were applied in Z, X, Y order (Equivalent to toEulerAnglesYXZ)

Returns a tuple of a Vector3 and a number which represent the rotation of the CFrame in the axis-angle representation

Returns all 12 numerical components of the CFrame in the following order:

(See the Rotational Component section above)

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.

Math Operations

Returns the composition of two CFrames.
Proceeding CFrames are offset in relative object space by preceding CFrames when multiplied together.

Returns the Vector3 transformed from Object to World coordinates.

Returns the CFrame translated in world space by the Vector3.

Returns the CFrame translated in world space by the negative Vector3.

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.

Contents

Constructors

CFrames are one of the many DataTypes available in Roblox Lua. A CFrame can be created by using one of the many constructors. However, note that unlike a BasePart's orientation property, it takes rotational arguments in radians instead of degrees.

Constructors

Creates a blank identity CFrame.

Creates a CFrame from a Vector3

This constructor overload has been replaced by CFrame.lookAt , which accomplishes a similar goal. It remains for the sake of backward compatibility.

Creates a new CFrame located at pos and facing towards lookAt , assuming that (0, 1, 0) is considered “up”.

At high pitch angles (around 82 degrees), you may experience numerical instability. If this is an issue, or if you require a different up vector, it’s recommended you use CFrame.fromMatrix instead to more accurately construct the CFrame. Additionally, if lookAt is directly above pos (pitch angle of 90 degrees) the up vector switches to the X-axis.

Creates a CFrame from position (x, y, z).

Creates a CFrame from position (x, y, z) and quaternion (qX, qY, qZ, qW)

Creates a CFrame from position (x, y, z) with an orientation specified by the rotation matrix [[R00 R01 R02] [R10 R11 R12] [R20 R21 R22]]

Creates a new CFrame located at at and facing towards lookAt , optionally specifying the upward direction (by default, (0, 1, 0)).

This function replaces the CFrame.new(Vector3, Vector3) constructor (see above) which accomplished a similar task. This function allows you to specify the up Vector, using the same default as the old constructor.

Creates a rotated CFrame using angles (rx, ry, rz) in radians. Rotations are applied in Z, Y, X order.

Creates a rotated CFrame using angles (rx, ry, rz) in radians. Rotations are applied in Y, X, Z order.

Equivalent to fromEulerAnglesXYZ

Equivalent to fromEulerAnglesYXZ

Creates a rotated CFrame from a Unit Vector3 and a rotation in radians

Creates a CFrame from a translation and the columns of a rotation matrix. If vz is excluded, the third column is calculated as [vx:Cross(vy).Unit].

Returns an orthonormalized copy of the DataType/CFrame|CFrame . The BasePart/CFrame property automatically applies orthonormalization, but other APIs which take CFrames do not, so this method will occasionally be necessary when when incrementally updating a CFrame and using it with them.

Читайте также: