Model movement inverted on rotated plane?

I am attempting to fix an urgent bug for my module Placement Service where the model movement becomes inverted when the plot is rotated. It occurred after I released a major update that added a bunch of new things as well as correcting offsets based off the plots position (made the movement relative to the plot). I believe this is the issue as it didn’t happen before this and have no idea how to correct it. Here’s an example of what’s going on:

Proper movement

normal.wmv (474.1 KB)

90 Degree rotation

90.wmv (317.9 KB)

180 Degree rotation

180.wmv (239.8 KB)

The code that I believe is creating this problem is:

-- Calculates the correct position
pos = cframe(x, 0, z)
pos = snapCFrame(plot.CFrame:Inverse()*pos)
finalC = pos*plot.CFrame*cframe(cx, 0, cz)
Entire function of the code above
-- Calculates the position of the object
local function calculateItemLocation()
	if currentRot then
		cx = primary.Size.X*0.5
		cz = primary.Size.Z*0.5
		
		x, z = mouse.Hit.X - cx, mouse.Hit.Z - cz
	else
		cx = primary.Size.Z*0.5
		cz = primary.Size.X*0.5
		
		x, z = mouse.Hit.X - cx, mouse.Hit.Z - cz
	end
	
	-- Clamps y to a max height above the plot position
	y = clamp(y, initialY, maxHeight + initialY)
	
	-- Changes y depending on mouse target
	if stackable and mouse.Target and mouse.Target:IsDescendantOf(placedObjects) or mouse.Target == plot then
		y = calculateYPos(mouse.Target.Position.Y, mouse.Target.Size.Y, primary.Size.Y)
	end

	if moveByGrid then
		-- Calculates the correct position
		pos = cframe(x, 0, z)
		pos = snapCFrame(plot.CFrame:Inverse()*pos)
		finalC = pos*plot.CFrame*cframe(cx, 0, cz)
	else
		finalC = cframe(x, y, z)*cframe(cx, 0, cz)
	end
	
	finalC = bounds(finalC)
	
	return finalC	
end

I have no idea what the solution to this would be so any help is be appreciated!
Thank you!

1 Like

As far as I know, I just solved this issue. If anyone’s interested, the solution was:

-- Calculates the correct position
local pltC = cframe(plot.CFrame.X, plot.CFrame.Y, plot.CFrame.Z)
pos = cframe(x, 0, z)
pos = snapCFrame(pltC:Inverse()*pos)
finalC = pos*pltC*cframe(cx, 0, cz)

Basically I just made sure no rotation was being taken into account by creating a custom CFrame.