I want to add cframes I found a way but is there already a way?

local A = workspace.A.CFrame
local B = workspace.B.CFrame
local C = A.Rotation*B.Rotation + (A.Position + B.Position)
workspace.C.CFrame = C

Why not add the angles together and then add the positions together?


[White part is Origin, first grey is A, second grey is B, and third grey part is C]

Is the C equation already used before somewhere?

  • [edit]
    No the equation is probably not used because it doesn’t work completely.
    Equation is accurate on 2 dimensions but not 3.

  • From AI:

In Roblox, you might have noticed that you can’t simply add two CFrames together using the + operator. Let’s delve into why this is the case:

  1. CFrames Contain Both Positional and Rotational Information:

    • A CFrame encapsulates both position and rotation data.
    • While it makes sense to add a position to another position (like adding two Vector3 values), it doesn’t make sense to add a 3D rotation to another 3D rotation.
    • Therefore, directly adding two CFrames using + would be nonsensical.
  2. Multiplying CFrames:

    • Instead of addition, you should multiply two CFrames to combine their transformations.
    • When you multiply two CFrames, you effectively concatenate their rotations and positions.
  3. Example:

    • Suppose you have two CFrames, A and B.
    • To combine them, use the * operator: C = A * B.
    • This results in a new CFrame C that represents the combined transformation of A followed by B.

Remember, when working with CFrames, use * for composition, not +. It’s a quirk of the system, but understanding this behavior will help you create accurate and predictable transformations in your Roblox games! :video_game::wrench:²³⁴

Source: Conversation with Bing, 2/25/2024
(1) Why is it CFrame * CFrame instead of CFrame + CFrame … - Roblox. Why is it CFrame * CFrame instead of CFrame + CFrame?.
(2) What can I do to help me practice with CFrames and calculate … - Roblox. What can I do to help me practice with CFrames and calculate things.
(3) Can’t add CFrames - Scripting Support - Developer Forum | Roblox. Can't add CFrames.
(4) I want to add cframes I found a way but is there already a way … - Roblox. I want to add cframes I found a way but is there already a way?.

1 Like

Can’t you just use CFrame.new()?

No.
A CFrame represents a position and rotation.
To add them results in an error:


^ In this case it expects a Vector3 because it only wants to add to the position property, which is a Vector3.
But in my case I want to combine the rotations and the positions.

Now if you where to multiply the CFrames, you would end up with something like this:


This is literally C = A*B = A:ToWorldSpace(B)
The way I think of this is that it takes the position and orientation of B (second grey part) with respect to the Origin (White part) and then treats A (first grey part) as if it was the origin.

I’m having trouble adding them consistently. It’s like the rotation gets off after reversing the addition.

Before Tween:

After Tween:

I’m pretty sure this is the same as

local C = A * B
1 Like
local C = A * B

Does not work, it just zero’s the CFrame

You get an identity CFrame (a “zero” CFrame) if one CFrame is the inverse of the other (CFrame2 = CFrame1:Inverse()), otherwise, I cannot see how you’d get an identity CFrame from a CFrame multiplication…

You can read the documentation about CFrame multiplication here, though it doesn’t really go in depth…
(btw CFrame1:ToWorldSpace(CFrame2) is the same as CFrame1 * CFrame2)

1 Like

@Tomi1231

Is the same as

local C = A.Rotation*B.Rotation + A.Rotation*B.Position + A.Position


code used in above place:

local A = workspace.A.CFrame
local B = workspace.B.CFrame

local C = A*B
local C_Eq = A.Rotation*B.Rotation + A.Rotation*B.Position + A.Position

workspace.C.CFrame = C
workspace.C_Eq.CFrame = C_Eq

The way I think of A*B is this:
View the positions and rotations of B with respect to the origin and A with respect to the origin.
Now imagine moving the origin toward the position and rotation of A until you reach it.
Now imagine B maintaining it’s position and rotation with respect to the origin as you move the origin towards A.

Download this place and select “Run” instead of Play. Then use the Move and Rotate tools to move and rotate A and B to see how C and C_Eq move.
Equivalent Statement 1.rbxl (58.0 KB)

It’s VERY unclear what you’re trying to do in the post, can you explain simply?

2 Likes

In Roblox, adding CFrames directly to combine position and orientation isn’t possible. Here’s why, and how to achieve the desired result…

You can’t add two CFrames in that approach.
If we see in the Roblox Documentation, CFrame is a datatype that has two components which are position and orientation.

Position is a Vector3 component, whereas rotation data (orientation) is maintained by storing it in matrices, and Roblox uses Euler’s angles for the rotation data, because it lets you describe the orientation of any object with respect to a fixed coordinate system in (In this case, 3D Space)

I believe this is why you can’t add two CFrames in the way you’re trying to do:

workspace.Part.CFrame 
-- We asked the datatype CFrame, and it returns two components, 
-- one is Vector3 type; which is position, and the second is the orientation, in Euler Angles.

In the runtime, behind the scenes, since we’re asking that datatype and retrieving those two components, here’s how it might look:

CFrame = Vector3.new(5, 5, 5) * CFrame.fromEulerAngles(math.rad(30), 0, 0)
-- Let's suppose, the part's position (Type Vector3), is 5, 5 ,5, and the Euler angles to be something...

Well, as you see, it is Euler Angles here, whereas in your OP, you were asking why it is not possible to do it in that approach, simply; you are getting part A and part B Rotation, and the orientation is returned in degrees, which can’t be added to the CFrame that uses Euler Angles (which is in radians)

This is the point, the CFrame combines two components, one is position which is a Vector type, the second one is Euler Angles which is done in radians and NOT degrees.

But in that equation, you retrieving the rotation of the Part A and B, and that would just return the rotation in degrees and not radians.

Which wouldn’t work for maths, but thankfully, you can convert that rotation value you got to radians using the math.rad() function.

In the paranthesis of math.rad() , you can a number in degrees, and it will convert that into radians, and for all intents and purposes regarding the CFrames, this simple trick is all that’s needed to be done!

And regarding, why you can’t add two CFrames, it’s very simple to think of, just add a part into your workspace, and try printing its CFrame, and it should give something like this in the output:

-57, 19.5, 62, 1, 0, 0, 0, 1, 0, 0, 0, 1

The first three numbers are position (Vector3) values, and the remaining numbers are related to the orientation in radians.

Those remaining 9 numbers are arranged in a matrix, as you can see in CFrame Documentation.

And the output just printed them when you used:
print(workspace.Part.CFrame)

So, if you try to add two CFrames, it’s like adding two sets of the numbers I showed you above, and it fails to do so, because you are adding two sets of numbers!

But let’s see it this way, Roblox actually DOES manage both of them for you, so it is handled in the background and if you use the PLUS sign, it understands you are trying to add a value to the existing CFrame and it adds it to the first three numbers which are Vector3, so it expects Vector3 when YOU use the PLUS ( + ) sign.

Because when you say:
workspace.Part.CFrame + CFrame.new(50, 20 ,30)
Behind the scenes of the CFrame.new() function, you are also giving an empty values of orientation i.e., not giving any values of the rotation data, in other words, you are giving the numbers of zeroes and the number ONE!

Because, as I said if you add a part into your workspace, and print its CFrame, using print(workspace.Part.CFrame) without rotating it at all, since Euler Angles are relative to something, the remaining 9 numbers:

-57, 19.5, 62, 1, 0, 0, 0, 1, 0, 0, 0, 1
There’s three 1s… However, let me put this simply for you.

You are doing addition operation with the plus sign operator, with a CFrame and a CFrame.new()

But let’s think of this like this, when you try to use CFrame.new, it expects Vector3 of the position property. This is because CFrame.new() is specifically for Vector3 values and not orientation.

BUT in another case where you are not using the CFrame.new(), but getting the value of both or
OR, if you try to add two CFrames (Part1.CFrame + Part2.CFrame), it’s like adding two sets of the numbers I showed you above as well, so…

There’s a workaround to overcome this, since when you use the PLUS sign, it expects Vector3, when you use multiplication it expects orientation.

The “IT” here is the CFrame you are doing the equation with.

Since you said you wanted to combine both position and orientation, just do this, this is what I did and it worked for me:

Player.Character:SetPrimaryPartCFrame((Player2.PrimaryPart.CFrame) * CFrame.fromEulerAngles(math.rad(20), math.rad(0), math.rad(20)) + Vector3.new(3, 0, 3))

So, basically, setting the Player’s CFrame to the Player2’s CFrame, but I wanted to keep the Player to stay a bit away, so I used the plus sign operator, and the CFrame expects a Vector3 value in that case.

I also use multiplication sign, and the CFrame expects that it is for orientation, however you must convert, let’s say 45 degrees to radians, use the math.rad() function, and that’s it.

I hope this was helpful, I tried to give some of my insights behind why it works like this, I might’ve got a bit carried away with details, sorry about that.