I am making a system that restrains a mesh inside a specified zone. I am using EditableMeshes to change my mesh’s shape. I have tried a couple methods, but they all seem to fail and the vertex’s local position seem out of place as if they don’t follow the linked mesh’s resize. For determining the zones, I use some parts that act as borders that shouldn’t let any vertext go past them (any vertex reaching past those borders should be moved back inside the allowed zone at the closest point to the border).
Here’s my code:
-- Creating the Editable Mesh & its linked physical mesh:
local EditableMesh = AssetService:CreateEditableMeshAsync("rbxassetid://106639371837966")
local Mesh= AssetService:CreateMeshPartAsync(Content.fromObject(clotEditableMesh))
-- Setting the Mesh's initial Size & Initial properties
Mesh.CFrame = meshCFrame
Mesh.Size = initialSize
Mesh.Anchored = true
Mesh.Parent = Workspace.Folder
-- I tween the mesh's size and simultaniously restrain its vertices
-- Calling the function that makes sure the mesh's shape is within the borders
-- BoundaryZones are Parts positioned as borders around the mesh
local constrainConnection = KeepContained(Mesh, EditableMesh, boundaryZones)
-- Calling a tween function that will make the mesh expand (Yields)
phaseFunctions.spread(Mesh, time, finalSize)
-- After the tween finished, disconnect the RunService that upadted the vertices positions
constrainConnection:Disconnect()
-- Function that displays the vertices when they go past the border (Clot is the Mesh, boundaryWalls are the 3d parts' CFrames facing the center of the Mesh)
local function KeepContained(clot, clotEditableMesh, boundaryWalls)
local connection = RunService.Heartbeat:Connect(function()
-- Iterate through vertices
for i, vertexId in ipairs(clotEditableMesh:GetVertices()) do
local vertexPosition = clotEditableMesh:GetPosition(vertexId)
for _, wallCFrame in ipairs(boundaryWalls) do
-- Calculate vector from wall's origin to vertex
local wallToVertex = vertexPosition - wallCFrame.Position
-- Calculate dot product with wall's look vector
local dotProduct = wallToVertex:Dot(wallCFrame.LookVector)
-- If vertex is in front of the wall (dot product positive)
if dotProduct > 0 then
-- Project vertex back to the wall plane
local constrainedPosition = vertexPosition - wallCFrame.LookVector * dotProduct
-- Update vertex position
clotEditableMesh:SetPosition(vertexId, constrainedPosition)
end
end
end
end)
return connection
end
Some of the EditableMesh’s vertices’ positions. As you can see they are locally ofsetted by up to 20 studs, which contradicts the actual size of the Mesh. The Mesh never exceeds 20 studs in size on any axis.
Video Example of Borders behavior: Watch Mesh Deformation Testing - Roblox Studio 2024-12-05 05-43-31 | Streamable
I am open to other methods if it’s not possible with EditableMeshes (it should be, though). I have tried CSG, but it was too loggy & it couldn’t handle the system due to limited mesh vertices (unoptimized).