Grid + Rotation system? CFRAME HELP

Hey!

I’m creating a build system where you can select a grid to build on and then use a GUI to select an item from my BuildAssets and place it on the grid, usign R to rotate. My plan is:

ON EACH RENDER / RENDERSTEPPED

  1. Evaluate a rough position / target from the mouse or Infront of the player.
  2. Apply the current rotation integer to the model.
  3. Snap the rotated CFrame to the grid.
  4. PivotTo / SetPrimaryPartCFrame

Here’s my code snippet (part of a much bigger script):

	RunService:BindToRenderStep("PlacementSession", 1, function()
		local CFramePosition = Player.Character.PrimaryPart.CFrame
		local GridRotation = self.Grid.CFrame.Rotation
		local FinalRotation = self.Rotation - GridRotation
            
        -- My desired rotation is self.Rotation (0- 360, it goes up by 90 each time0. 
		-- I need to change CFramePosition so that it is properly rotated.
		self.TransparentModel:SetPrimaryPartCFrame(CFramePosition)
	end)

Rotation:

function PlacementSession:rotateDegrees(degreesRotation) : {}
	local proposedRotation = self.Rotation + degreesRotation
	if proposedRotation > 360 then
		local degreesCompensation = math.floor(proposedRotation / 360)
		degreesCompensation = degreesCompensation * 360
		proposedRotation = proposedRotation - degreesCompensation
	end
	self.Rotation = proposedRotation
	return self
end

-- Literally just grows self.Rotation by the argument and makes sure it's below 360 or it will compensate to make it below 360.

Grid BasePart is accessible through self.Grid, I appreciate any help as I’ve been on this for hours across multiple servers / sites!

2 Likes

The problem is each grid has a different rotation, so a local furniture rotation of 0 means a different world space rotation for each plot / grid.

1 Like

Slightly confused by your question: is your issue that you’re not able to load it back with the same relative position and rotation? Or is it that you’re struggling to place the item relative to the grid origin?

If it’s the former, alongside finding out more about that here, just save the object space coordinates, e.g.

local someCoordinates = CFrame.new(0, 0, -5) * CFrame.Angles(0, math.rad(25), 0) -- i.e. the cframe at which you placed the object

local coordsToSave = placementGridOriginPart.CFrame:ToObjectSpace(someCoordinates) -- get the coords in object space

-- ... load data back in somehow

local someLoadedCoordinates = placementGridOriginPart.CFrame * coordsToSave
model:SetPrimaryPartCFrame(someLoadedCoordinates)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.