Placement system rotation help

Trying to make objects position themself right up to a wall even when rotated. Currently only works when not rotated

The first thing I tried:

-- item size is model:GetExtentsSize()
connections.runConnect = runs.RenderStepped:Connect(function()
	local ray = rayFunction(params) -- raycasts from player mouse if computer, and camera if mobile
	if ray == nil then return end

	local toGrid = module.Snap(ray) -- returns a rounded ray.Position 
	local cf = CFrame.new(toGrid + (ray.Normal * itemSize / 2)) * module.Pivot -- the rotation of the obj in CFrame.Angles
	selectedItem:PivotTo(cf)
end)

This is what it looks like normally:
image

This is what it looks like when rotated
image
image

PS: Objects can also be placed on floors

This is the best I’ve got so far, it adjusts based on rotation but not the correct distance:

local function RotateVector(vector, rotationCFrame)
	return rotationCFrame:PointToWorldSpace(vector)
end

connections.runConnect = runs.RenderStepped:Connect(function()
	local ray = rayFunction(params)
	if ray == nil then return end

	local toGrid = module.Snap(ray)
	local rotationCFrame = selectedItem:GetPivot()
	local rotatedSize = RotateVector(itemSize, rotationCFrame)

	local adjustedPosition = toGrid + ray.Normal * (rotatedSize.magnitude / 2)
	adjustedPosition = adjustedPosition - Vector3.new(0, rotatedSize.y / 2, 0)

	selectedItem:PivotTo(CFrame.new(adjustedPosition) * module.Pivot)
end)

Looking for anyones help if you’ve got ideas. Cheers

1 Like

The object has a small cube primary part, which when removing it, will place the table halfway into the ground, which is why I have it.

1 Like

I’d also like to have 15-degree rotation if possible. Is this only possible with 90-degree rotation? I see Build A Boat for Treasure has what I’m looking for

1 Like

I think what you would do is something along this type of math

x2 = x0+(x-x0)*cos(theta)+(y-y0)*sin(theta)
y2 = y0-(x-x0)*sin(theta)+(y-y0)*cos(theta)

theta would be the relative rotation from the wall you are trying to attach it to, and then you would use the offset to figure it out. I might play around in studio and see if i can get you a clearer answer. x2,y2 are the sizes of it rotated.

1 Like