So I have a system where you Click one point, a preview block scales and anchors from one corner as the other follows your mouse until you click again, and places the block. The idea is you can create this part and scale it simply by clicking two points which will represent bottom left and top right corners(if looking from top down)
The problem is when the block is rotated, the preview gets all messed up and starts mirroring and acting weird. The final product is still as expected and works, but the live preview is what is messing up.
I will show a couple pictures to visualize and then share some code. In all pictures I will be moving the mouse four studs right and four studs up relative to the camera:
CORRECT:
The blue line is where my mouse currently is, the grey box is the first spot i clicked. I have this on a grid system. This is with no rotations.
INCORRECT:
Rotation of 90 degrees
Rotation of 180 Degrees
I will show one more picture here, at some angles(90 and 270 it seems) the scaling of the part doesn’t even anchor the main corner unless all sides are equal size.
This is not the case for the 0 rotation though it works perfect as intended. One corner stays on the light square where clicked and it scales as I move the mouse.
Here is the code for the preview block. This is not the first iteration, I have been fighting with this for hours.
startPosition and endPosition are vector3’s for the clicks of the mouse. End position represents where the mouse is currently.
local function updatePreviewBlock()
if startPosition and endPosition then
-- Calculate the delta vector in global space
local deltaVector = Vector3.new(endPosition.X - startPosition.X, 0, endPosition.Z - startPosition.Z)
-- Rotate the delta vector based on the current rotation
local rotatedDeltaVector
if currentRotation == 0 then
rotatedDeltaVector = deltaVector
elseif currentRotation == 90 then
rotatedDeltaVector = Vector3.new(-deltaVector.Z, 0, deltaVector.X)
elseif currentRotation == 180 then
rotatedDeltaVector = Vector3.new(-deltaVector.X, 0, -deltaVector.Z)
elseif currentRotation == 270 then
rotatedDeltaVector = Vector3.new(deltaVector.Z, 0, -deltaVector.X)
end
-- Calculate the new size and position
local sizeX = math.abs(rotatedDeltaVector.X)
local sizeZ = math.abs(rotatedDeltaVector.Z)
local sizeY = bottomHeight.Value
-- The block's new position
local blockPosition = startPosition + rotatedDeltaVector / 2
-- Update the preview block's position and size
bottomPreviewBlock.Size = Vector3.new(sizeX, sizeY, sizeZ)
bottomPreviewBlock.Position = Vector3.new(blockPosition.X, initialY + sizeY / 2, blockPosition.Z)
bottomPreviewBlock.Orientation = Vector3.new(0, currentRotation, 0)
end
end
Any help would be really appreciated, bonus points if you can explain the solution as i struggle a bit with this kind of math stuff when programming and its more of a trial and error thing for me. Thank you