Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

What is the difference between CFrame and Vector3?

Asked by 7 years ago

How do I use CFrame and Vector3? and what does CFrame * CFrame is really mean?

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

You seem to have a grasp on what a position vector is (and what vector addition is), so I'll briefly touch on CFrames here. Note that it is likely that this will be unusable or virtually impossible to understand. CFrames are hard to use.

What are CFrames?

As SeptorHD said, CFrames are coordinate frames, and describe a position and rotation. There are multiple ways to create a CFrame, as outlined here (http://wiki.roblox.com/index.php?title=CFrame#Constructors), but that is beyond the scope of this answer.

CFrames describe spatial transformations, and every CFrame relates to a certain transformation. For example, the CFrame (1,0,0) relates to a translation 1 unit in the x direction.

The difference between CFrames and Vector3s comes in when a rotation is attached. For example, the CFrame (0,0,0,0,-1,0,1,0,0,0,0,1) refers to a rotation 90 degrees clockwise around the z-axis. It's pretty complicated to explain, so just see it as an example for now.

CFrame Composition

CFrame A * CFrame B refers to the composition of CFrame A by CFrame B. This is not commutative in general: CFrame A * CFrame B is not equal to CFrame B * CFrame A, unless at least one of them does not have a rotation.

Remember how I said that each CFrame refers to a spatial transformation? It takes the first CFrame and then transforms it by the transformation in the second CFrame, relative to the first.

For anyone who knows some linear algebra, this refers to the addition of the position vectors and the multiplication of the rotation matrices (or quaternions, which I don't know anything about).

Example:

Let's say A is a CFrame with a position of (3,4,5) and a rotation of 90 degrees clockwise around the x-axis, and B is a CFrame with a position of (5,6,7) and a rotation of 90 degrees clockwise around the z-axis.

The 90 rotation around the x-axis makes the relative y-axis of CFrame A in the direction (0,0,1) and the relative z-axis in the direction (0,-1,0). So the transformation of B will move it (relative to world) 5 units in the direction (1,0,0) [relative x-axis], 6 units in the direction (0,0,1) [relative y-axis], and 7 units in the direction (0,-1,0) [relative z-axis]. It will then rotate by 90 degrees clockwise around the axis (0,-1,0) [relative z-axis].

ToWorldSpace and ToObjectSpace

At this point, you are now completely lost, and regretting ever thinking about CFrames, which are terrible to use. Here we go on the last major topic: Roblox has some helpful functions to make CFrames a little more intuitive to use.

cframe A:toObjectSpace(cframe B) is a member function of all CFrames. This function converts a CFrame B (which is relative to world) into its equivalent CFrame relative to A. This is equivalent to composing the inverse of A (which is the CFrame that "undoes" the transformation of A) with B.

cframe A:toWorldSpace(cframe B) is also a member function, and converts CFrame B (relative to A) into its equivalent CFrame relative to world. This is equivalent to composing A with B.

This makes it much easier to use CFrames since you can just describe a relative transformation and get the worldspace cframe, or vice versa.

Apologies for any mistakes in advance. I think most of this is true, but I'm not certain, so please take everything in this answer with a massive grain of salt. Incidentally, if you're interested in learning more, look up linear algebra, particularly linear mapping.

Ad
Log in to vote
-1
Answered by 7 years ago

Think of Vector3 as a position. It's hard for me to explain CFrame, but it means CoordinateFrame.

0
What is CoordinateFrame then? MinHoang 49 — 7y

Answer this question