Make object snap to surface with rotation

Trying to get this table to always snap to the surface of a wall, or floor etc, even when rotated. Similar to these, but allow rotation instead

Regular:
Screenshot_323

Rotated:
Screenshot_322
Screenshot_321

Currently this is what I have. Cant figure out a way to adjust this depending on the rotation of the table

-- 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)

You need to make sure your taking into account the scale properly. the offset for scale has to change based in the scale on that direction.

1 Like

How would I go about doing this?? ray.Normal * size?

1 Like

If its a model do GetExtentsSize() get the scale on each axis and depending on how the table is oriented use the correct axis as a cframe. (By just adding it to the cframe). Keep in mind you will have do add scale/2

1 Like

This is the bit I’m unable to solve. How would I do something like this?. Also forgot to mention, in the script “itemSize” is model:GetExtentsSize().

Well im not 100% sure how you would go about it but the most straightforward approach for me is: checking the difference in rotation on object from the place you want it to attach to. And depending on that difference you change the offset. Does your rotation system only rotate in 90 degrees increments?

15 degree increments. I’d have a rough idea of how to do it if it was 90 degrees but 15, no clue

Then you would have to look into sine and cosine, how you would use it to determine the correct position.

1 Like

I will look if i have some articles or more information to give you.

1 Like

Could you provide some sample code of how I would figure out the position doing this?? Still unsure what you mean

Still looking for assistance with this if possible anyone. Thabks

Sort of onto something using ChatGPT. It does change depending on the rotation, but it is too far from the wall / floor. It also shouldn’t be doing this for the floor as the rotation doesn’t effect it

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)

It looks like it’s 1 table length wrong or something?? Sorta working. Still looking for assistance if anyone out there has any ideas

Anyone got ideas? stuck on this