I have a deadline in 2 days and the guy who promised me to help isn’t replying so I need quick help.
The system works like this: You have a baseplate where you can place stuff where you move your mouse (grid). To calculate the boundries I use this function:
local function bounds(cf, offsetX, offsetZ) --cf is the items current position, offsetX is the size.X of the item and the offset Z is the Size.Z of the item
local LOWER_X_BOUND
local LOWER_Z_BOUND
local UPPER_X_BOUND
local UPPER_Z_BOUND
LOWER_X_BOUND = plot.Position.X - (plot.Size.X*0.5) + offsetX -- plot is the baseplate
UPPER_X_BOUND = plot.Position.X + (plot.Size.X*0.5) - offsetX
LOWER_Z_BOUND = plot.Position.Z - (plot.Size.Z*0.5) + offsetZ
UPPER_Z_BOUND = plot.Position.Z + (plot.Size.Z*0.5) - offsetZ
local newX = clamp(cf.X, LOWER_X_BOUND, UPPER_X_BOUND) -- clamp = math.clamp
local newZ = clamp(cf.Z, LOWER_Z_BOUND, UPPER_Z_BOUND)
return cframe(newX, posY, newZ) --cframe = CFrame.new
end
But when the baseplate is rotated it doesn’t work. Any idea how to fix this? (there is no need to work with this function. If there is a btter way to work with that it would also be very good. For example: something with raycasting)
Basically, you’re going to want to convert your cframe into the object space of the base part, do your clamping there, then convert it back to world space.
It’s been a while since I’ve really done it so I’m probably going to have done it wrong somehow, but here it goes
local function bounds(cf, offsetX, offsetZ) --cf is the items current position, offsetX is the size.X of the item and the offset Z is the Size.Z of the item
local objCF = plot.CFrame:ToObjectSpace(cf)
local LOWER_X_BOUND
local LOWER_Z_BOUND
local UPPER_X_BOUND
local UPPER_Z_BOUND
LOWER_X_BOUND = - (plot.Size.X*0.5) + offsetX -- plot is the baseplate
UPPER_X_BOUND = (plot.Size.X*0.5) - offsetX
LOWER_Z_BOUND = - (plot.Size.Z*0.5) + offsetZ
UPPER_Z_BOUND = (plot.Size.Z*0.5) - offsetZ
local newX = clamp(objCF.X, LOWER_X_BOUND, UPPER_X_BOUND) -- clamp = math.clamp
local newZ = clamp(objCF.Z, LOWER_Z_BOUND, UPPER_Z_BOUND)
local newCF = cframe(newX, posY, newZ)
return plot.CFrame:ToWorldSpace(newCF)
end
But basically toObjectSpace just transforms the cframe to make it be the cframe from the perspective of the baseplate. So we can treat the baseplate as unrotated at the origin and do the calculations since we rotated the cf to compensate. It also translated it for us so I got rid of your translation.
If you’re still here and you could help me with the Y pos then I 'd really appricatne it.
local function calculateYPosition(toP, toS, oS)
return (toP + toS * 0.5) + oS * 0.5
end
posY = calculateYPosition(mouse.Target.Position.Y, mouse.Target.Size.Y, primary.Size.Y) -- primary = model.primary part (hitbox)
Ok nvm. Chat GPT helped me to solve it. (it wasn’t written by it, but it helped me)
local function calculateYPosition(toP, toS, oS)
local objSpace = plot.CFrame:ToObjectSpace(CFrame.new(toP.X, toP.Y, toP.Z)) -- Use toP directly
local newY = (objSpace.Y + toS * 0.5) + oS * 0.5
return newY
end
posY = calculateYPosition(mouse.Target.Position, mouse.Target.Size.Y, primary.Size.Y) -- primary = model.primary part (hitbox)