May someone please review and optimize my CFrame math?

Here’s my function:

local function MoveParts(vector3)
	
	local centerPartCframe = centerOriginalCFrame * CFrame.new(vector3)
	local movedDistance = centerOriginalCFrame * centerPartCframe:Inverse()
	
	table.insert(parts, CenterPartWithArrows)
	table.insert(cframes, centerPartCframe)
	
	for part, selection in pairs(boxes) do
		
		local cframe = CFrame.new(originalPartCFrames[part].Position + movedDistance:Inverse().Position) * originalPartCFrames[part].Rotation

		table.insert(parts, part)  
		table.insert(cframes, cframe) 
	end

	workspace:BulkMoveTo(parts, cframes, Enum.BulkMoveMode.FireCFrameChanged)
	table.clear(parts)
	table.clear(cframes)

end

It currently works perfectly, but I think it could be potentially simpler.
Thanks!

3 Likes

Seems redundant to do movedDistance:Inverse() within the loop there, probably would move it outside of the loop. Something like:

local function MoveParts(vector3)
	
	local centerPartCframe = centerOriginalCFrame * CFrame.new(vector3)
	local movedDistance = CFrame.new((centerOriginalCFrame * centerPartCframe:Inverse()):Inverse().Position)
	
	table.insert(parts, CenterPartWithArrows)
	table.insert(cframes, centerPartCframe)
	
	for part, selection in pairs(boxes) do
		local cframe = originalPartCFrames[part] * movedDistance

		table.insert(parts, part)  
		table.insert(cframes, cframe) 
	end

	workspace:BulkMoveTo(parts, cframes, Enum.BulkMoveMode.FireCFrameChanged)
	table.clear(parts)
	table.clear(cframes)
end
1 Like

With the old code:

With your new code:

Sorry… this didn’t work :c

1 Like

We can put the movedDistance:Inverse().Position outside the for loop like what @kingerman88 said, but in his code, the operation became different that’s why it didn’t work.

Another is we can take the CFrame of the part then just add the movedDistance instead of making a CFrame.new and multiplying the rotation.

local function MoveParts(vector3)
	
	local centerPartCframe = centerOriginalCFrame * CFrame.new(vector3)
	local movedDistance = centerOriginalCFrame * centerPartCframe:Inverse()
	local movedInverse = movedDistance:Inverse().Position

	table.insert(parts, CenterPartWithArrows)
	table.insert(cframes, centerPartCframe)
	
	for part, selection in pairs(boxes) do
		
		local cframe = originalPartCFrames[part] + movedInverse

		table.insert(parts, part)  
		table.insert(cframes, cframe) 
	end

	workspace:BulkMoveTo(parts, cframes, Enum.BulkMoveMode.FireCFrameChanged)
	table.clear(parts)
	table.clear(cframes)

end

Or you can just straight up add the vector3 to the part CFrame

local function MoveParts(vector3)
	
	table.insert(parts, CenterPartWithArrows)
	table.insert(cframes, centerPartCframe)
	
	for part, selection in pairs(boxes) do
		
		local cframe = originalPartCFrames[part] + vector3

		table.insert(parts, part)  
		table.insert(cframes, cframe) 
	end

	workspace:BulkMoveTo(parts, cframes, Enum.BulkMoveMode.FireCFrameChanged)
	table.clear(parts)
	table.clear(cframes)

end
2 Likes

Thank you, it works :smiley:

1 Like