How to rotate a CFrame based on pivot within code

Hello, I’m trying to make a system that detects rooms based on a to-be placed room’s size, and to do this, I need to pivot the detection CFrame that will be utilized for a workspace:GetPartBoundsInBox() detection, by the primary part of the room’s position, all within the code.

The problem is, I’m not sure of how to preform such an operation, and I haven’t found any helpful topics about this within the devforum. Any help would be appreciated.

So, if you have a pure rotation CFrame like this rotation of 45 degrees around the Y axis:

local rot = CFrame.fromOrientation(0, math.rad(45), 0)

Then doing

part.CFrame = rot * part.CFrame

will rotate your part 45 degrees around the world Y-axis, around the world origin 0,0,0.

If you want to do this same rotation, but around some pivot point other than the world origin, it’s as simple as shifting your box by that point (as an offset) rotating around the origin, then shifting back. So, if the pivot is a Vector3 position, it could look like this:

local pivot = CFrame.new( 10, 0, 40 ) -- Pivot as a CFrame, a pure position in this case
local rot = CFrame.fromOrientation(0, math.rad(45), 0) -- Pure y rotation, same as before
local rotAroundPivot = pivot  * rot * pivot:Inverse()

part.CFrame = rotAroundPivot * part.CFrame

This code, as is, will rotate the part 45 degrees around the world Y axis, but with the rotation centered at the point ( 10, 0, 40 ) instead of ( 0, 0, 0 ). Because the pivot is a pure position, this multiplication is exactly the same as doing:

local pivot = Vector3.new( 10, 0, 40 )
part.CFrame = part.CFrame - pivot
part.CFrame = rot * part.CFrame
part.CFrame = part.CFrame + pivot

The CFrame multiplication version generalizes though… if the pivot is a CFrame with a non-identity orientation, the first version would rotate your part 45 degrees around the pivot CFrame’s UpVector instead of the world Y axis.

All of this assumes that you want the orientation of the box to change, not just rotate it’s position around a point. If you want to rotate just the position, you need to just do the above, plus addtionally counter rotate the part in place (around it’s own position)

I tried to apply it although it didnt work. Is there anything wrong here?

local Size = NewRoomModel:GetExtentsSize()
local Displacement = OutNode.Position + (OutNode.CFrame.LookVector * (OutNode.Size.Z + (Size.Z/2)))
local Pivot = CFrame.new(NewRoomModel.PrimaryPart.Position)
local RotPivot = Pivot * CFrame.fromOrientation(
	math.rad(OutNode.Rotation.X),
	math.rad(OutNode.Rotation.Y),
	math.rad(OutNode.Rotation.Z)
) * Pivot:Inverse()
local Placement = CFrame.new(Displacement) * RotPivot

Your ‘RotRivot’ CFrame is a rotation around the pivot point, but this last line appears to be multiplying in the wrong order. But it’s not really clear to me what you’re code is meant to do, without a visual.

This is the code I added before the pivot, it places the CFrame after an OutNode of the room. The OutNode is what is used to spawn more rooms from a previous room instance, while the InNode (Or the room’s primary part) is what is placed after the OutNode to connect the rooms. The code described below works correctly, although when adding the pivot into the equation, I can’t seem to get it right.

local Size = NewRoomModel:GetExtentsSize()
local Displacement = OutNode.Position + (OutNode.CFrame.LookVector * (OutNode.Size.Z/2 + Size.Z/2))
local Placement = CFrame.new(Displacement) * OutNode.CFrame.Rotation

This is the detection module without the pivot.


This is the detection module with it.

The CFrames go in seemingly random places when the OutNode does not face along the positive X axis. (Which may have to do with me using the position of the InNodes of the new rooms original position, but even then they seem to go way farther than said positions)

Fixed it, figured out I wasn’t applying the offset after fixing the other miscellaneous problems. Either way, thanks for your help.

local Pos, Size = NewRoomModel:GetBoundingBox()
local Offset = Pos * NewRoomModel.PrimaryPart.CFrame:Inverse()
local NodeDisplacement = OutNode.Position + (OutNode.CFrame.LookVector * OutNode.Size.Z)
local Pivot = CFrame.new(NodeDisplacement)
local RotPivot = Pivot * OutNode.CFrame.Rotation * Pivot:Inverse()
local Placement = RotPivot * (Offset * CFrame.new(NodeDisplacement))