How would I fix this rotation offset

Hey DevForum! Getting ready for the holidays? :gift:

I’ve been looking at a piece of code for almost a week and I can’t seem to be finding a solution.

I’m making something similar to a Placement System but this rotation problem is confusing me… :crazy_face::dizzy_face:

local function SnapToGrid(Items,Base,ItemHolder,Rotations,OrigRotation)
	local Coordinates = {}
	local HitCFrame = Hit({unpack(Items)},Base) -- Ray
	
	if HitCFrame then
		local ModelSize, ModelCFrame = GetBoundingBox(Items)
		local HitRelativeToBase = Base.PrimaryPart.CFrame:ToObjectSpace(HitCFrame)
		
		HitRelativeToBase = CFrame.new(Round(HitRelativeToBase.X,3),HitRelativeToBase.Y,Round(HitRelativeToBase.Z,3))
		ModelSize = CFrame.new(math.abs(ModelSize.X),math.abs(ModelSize.Y),math.abs(ModelSize.Z)) * CFrame.Angles(0,OrigRotation,0)
		
		ModelCFrame = Base.PrimaryPart.CFrame:ToObjectSpace(ModelCFrame)
		ModelCFrame = CFrame.new(Round(ModelCFrame.X,3),ModelCFrame.Y,Round(ModelCFrame.Z,3)) * CFrame.Angles(0,OrigRotation,0)
		
		local GridExtents = CFrame.new((Base.PrimaryPart.Size - Vector3.new(ModelSize.X,0,ModelSize.Z))/2) * CFrame.Angles(0,OrigRotation,0)
		
		HitRelativeToBase = CFrame.new(Round(HitRelativeToBase.X,3),0,Round(HitRelativeToBase.Z,3))
		HitRelativeToBase = CFrame.new(Round(math.clamp(HitRelativeToBase.X,-GridExtents.X,GridExtents.X),3),HitRelativeToBase.Y,Round(math.clamp(HitRelativeToBase.Z,-GridExtents.Z,GridExtents.Z),3))

		for Index, Item in ipairs(Items) do
			local RotationalY = Rotations[Index]-OrigRotation -- Table of older rotations subtracted by the rotation given here.
			local ItemCFrameOffset = ModelCFrame:ToObjectSpace(Item.PrimaryPart.CFrame)
			local ItemCoordinates = (HitRelativeToBase ) * ItemCFrameOffset
			ItemCoordinates = CFrame.new(ItemCoordinates.X,((Base.PrimaryPart.CFrame.Y + Base.PrimaryPart.Size.Y/2) + Item.PrimaryPart.Size.Y/2),ItemCoordinates.Z) * CFrame.Angles(0,RotationalY,0)
			Coordinates[Index] = ItemCoordinates:ToWorldSpace(Base.PrimaryPart.CFrame)
		end
		
		return Coordinates
	end
end 

This specific part of the code seems to be rerunning the rotation.

for Index, Item in ipairs(Items) do
	local RotationalY = Rotations[Index]-OrigRotation -- Table of older rotations subtracted by the rotation given here.
	local ItemCFrameOffset = ModelCFrame:ToObjectSpace(Item.PrimaryPart.CFrame)			local ItemCoordinates = (HitRelativeToBase ) * ItemCFrameOffset
	ItemCoordinates = CFrame.new(ItemCoordinates.X,((Base.PrimaryPart.CFrame.Y + Base.PrimaryPart.Size.Y/2) + Item.PrimaryPart.Size.Y/2),ItemCoordinates.Z) * CFrame.Angles(0,RotationalY,0)
	Coordinates[Index] = ItemCoordinates:ToWorldSpace(Base.PrimaryPart.CFrame)
end

Trying to explain it but if OrigRotation was set to 90 (in radians), it would continue reoffsetting it every RunService.RenderStepped

I can’t even figure out a solution to the repeating offset. If anybody has any way to make my code not repeat the rotation multiple times or make it more efficient. Please leave something down. I’ll explain what else I can if asked for.

Have a happy holiday.

2 Likes

What is your issue? Is it that it isn’t rotating the full 90 degrees or that it keeps rotating?

2 Likes

From what I seen, it just keep rotating non stop unless on 0 degrees.

Still looking for help. (Bump)

Here is a GIF thats shows the models when Rotation is at 90 degrees.

https://gyazo.com/41b509c94b8b1bca822ba84dbe5a696c

90 degree rotation increments. Any ideas?

Happy Christmas Eve,

I would probably just rotate the original PrimaryPart CFrame by the given rotation each time, but it looks like you could fix things in your case by updating the rotations table after you rotate the object.

			local RotationalY = Rotations[Index]-OrigRotation -- Table of older rotations subtracted by the rotation given here.
			local ItemCFrameOffset = ModelCFrame:ToObjectSpace(Item.PrimaryPart.CFrame)
			local ItemCoordinates = (HitRelativeToBase ) * ItemCFrameOffset
			ItemCoordinates = CFrame.new(ItemCoordinates.X,((Base.PrimaryPart.CFrame.Y + Base.PrimaryPart.Size.Y/2) + Item.PrimaryPart.Size.Y/2),ItemCoordinates.Z) * CFrame.Angles(0,RotationalY,0)
			Coordinates[Index] = ItemCoordinates:ToWorldSpace(Base.PrimaryPart.CFrame)
			Rotations[Index] = OrigRotation  ---Added this line here
1 Like

Thanks for the reply. I think I might be doing something wrong. Also OrigRotation is the rotation given by the user. It’s set at 0 and basically if the player rotates an item, it would turn 90 degrees and items would be based on that. It isn’t related to the Item’s rotation. I can show more code if you’d like?