Rotate CFrame with CFrame

Hello!

I would like to know how to rotate a CFrame rotation matrix ( orientation ) using another CFrame.

For example : [ 0, 90, 0 ] rotated by [ 90, 0, 0 ] produces [ 0, 0, 90 ].

I’m conviced that it is done with either ToWorldSpace or ToObjectSpace but I couldn’t get it to work. For those wondering, I am in fact converting the degrees to radians.

Thanks !

1 Like

None of the examples you’ve given are actually cframes but rather Vector3s. To rotate by a CFrame all you do is multiply by that CFrame.

1 Like

The example is a simplified version. In my actual code, I am dealing with CFrames.

Just multiply the CFrame with another

Part.CFrame *= CFrame.Angles(
	math.rad(0),
	math.rad(90),
	math.rad(0)
)
1 Like

I use something in my game that deals with CFrames, so I pasted a snippet from there:

blade:SetPrimaryPartCFrame(hand.CFrame * CFrame.new(0, -0.2, 0) * CFrame.Angles(math.rad(-90), 0, math.rad(90)))

The blade is positioned relative to the hand, moved offset, then rotated on the Y and Z axis 90 degrees. This is all accomplished with a * operator.

I tried doing that before but it doesn’t work

script.Parent.CFrame *= (CFrame.Angles(0, math.rad(90), 0) * CFrame.Angles(math.rad(90), 0))

In this case the part CFrame doesn’t even change.

I’m unsure if this is right, but it might be the fact that you use *=. Try this:

script.Parent.CFrame = script.Parent.CFrame * (CFrame.Angles(0, math.rad(90), 0) * CFrame.Angles(math.rad(90), 0))

This isn’t the problem ( I did test it anyways ).

The next thing I notice is that you are missing argument 3 at the second CFrame.Angles:

script.Parent.CFrame *= (CFrame.Angles(0, math.rad(90), 0) * CFrame.Angles(math.rad(90), 0, 0))

It wasn’t working because of the missing argument but it still doesn’t give me what I wanted. Doing * acts as a + giving me [ 90, 90, 0 ]. I will try using ToWorldSpace().

In that case, combine it to CFrame.Angles(math.rad(90), math.rad(90), 0)

My point is that it is oversimplified then and lost meaning. What are the cframes you are dealing with?

I’m making a custom camera using the gyroscope on the phone. The gyroscop returns a CFrame which is used to rotate the camera. Tough, I’m having issue rotation the head so it matches the phone orientation as it follows the torso. What I’m trying to do is to rotate the phone “CFrame” by the camera CFrame so the axis aligns correctly when the torso orientation isn’t [ 0, 0, 0 ].

If not aligned, the axis will be “swapped” which makes the X be Z and so on.

Surely it is the torso CFrame that needs to be involved in some way? I think if you start with the torso CFrame, multiple by an offset to get to the head position and then multiply by the gyroscrope CFrame that should sort it out

1 Like

Is the CFrame in ObjectSpace or WorldSpace. Just like the CFrame.Angles example, try playing around with that (like CFrame.Angles(0, 90, 0):ToObjectSpace(Character.HumanoidRootPart.CFrame), or whatever the CFrame you are revolving around.

I tried doing that before but it always led to the axis being swapped if the torso rotation was not 0. If it is on any axis then, the axis works perfectly. I came to the conclusion that just rotation the gyroscope CFrame by the camera CFrame would fix the issue and make it not follow the camera.

It doesn’t seem clear to me exactly what you’re trying to do still so I’ll just make one last suggestion.

If you are just trying to rotate one axis to another then some combination of:

CFrame.Angles(math.pi/2, 0, 0) * CFrame.Angles(0, math.pi/2, 0) * CFrame.Angles(0, 0, math.pi/2) 

Adjusting with + or - signs for the angles where necessary.

Sending a video might help. Press F12.

I’m trying to take a rotation and rotate it by another rotation.

Here are 2 images to show what I mean. It should be clearer.

In this example, I’m rotating [ 0, 90, 0 ] by [ 90, 0, 0 ] which would give me [ 0, 0, 90 ].

The green circle ( Y ) is equal to 90.

If I take the blue circle ( X ) and turn it by 90 degree, you will see that the green circle has taken the place of the red circle ( Z ).

In the end, by Y it switchs positions with the Z.
This is the effect I’m trying to achieve.

So, I’m gathering you want to know the current angle of the CFrame and to swap Y and Z. However, doing that constantly will “glitch” the part. I’m not capable of achieving this, but one thing you can try is seeing if ObjectSpace is being used.