luaconstraintsrobloxcloth-simulation

Connecting a grid up with rope constraints


I'm trying to make a kind of "cloth simulation" by using ROBLOX's new rope constraints and a grid of parts.

Currently, I've made a 10x10 grid of .4x.4x.4 blocks and now I want to connect each one up with rope constraints.

I've named each part in the grid after their row and column (eg: first part in the grid being 1 1, last one being 10 10)

and then I get the parts around each individual grid part using their name and string manipulation.

I then insert 4 attachments into each part and 4 rope constraints. Here's the code (ab stands for above, be stands for below, etc) :

for i2 = 1, #gParts do
    local ab = tostring(tonumber(gParts[i2].Name:match("^(%S+)"))-5).."     "..tostring(tonumber(string.sub(gParts[i2].Name,-1))-1)
    local be = tostring(tonumber(gParts[i2].Name:match("^(%S+)"))+5).."     "..tostring(tonumber(string.sub(gParts[i2].Name,-1))+1)
    local le = tostring(tonumber(gParts[i2].Name:match("^(%S+)"))-1).."     "..tostring(tonumber(string.sub(gParts[i2].Name,-1)))
    local ri = tostring(tonumber(gParts[i2].Name:match("^(%S+)"))+1).."     "..tostring(tonumber(string.sub(gParts[i2].Name,-1)))
    for i3 = 1, 4 do    
        local atchm = Instance.new("Attachment",gParts[i2])
        local ropeconst = Instance.new("RopeConstraint",gParts[i2])
    end
end

Rope constraint has 2 main properties I need to use; attachment 1 and attachment 2.


Solution

  • I've never really messed with the new constraints, but I believe this should work.

    Do keep in mind that the constraints are a new Instance in Roblox, and that they are likely still experimental.

    X = 10;
    Y = 10;
    spread = 4; 
    --Spread is the Length of the Constraint. You may have to increase this, especially if it's stiff.
    
    function createAttachments()
        --This is under the assumption that gParts is a table filled with the Part Instances
        for i,v in pairs(gParts) do
            local atch = Instance.new("Attachment",v);
        end;
    end;
    
    function connectConstraints(part,x,y)
        if x ~= X then
            connectRight = x+1.." "..y;
        end;
        if y ~= Y then
            connectDown = x.." "..y+1;
        end;
        if connectRight ~= nil then
            local ropeconst = Instance.new("RopeConstraint",part);
            ropeconst.Length = spread;
            ropeconst.Attachment0 = part.Attachment;
            ropeconst.Attachment1 = connectRight.Attachment;
        end;
        if connectLeft ~= nil then
            local ropeconst = Instance.new("RopeConstraint",part);
            ropeconst.Length = spread;
            ropeconst.Attachment0 = part.Attachment;
            ropeconst.Attachment1 = connectLeft.Attachment;
        end
    end
    
    createAttachments();
    connectConstraints();
    

    If this does not work for you, please let me know. I can contact you from the site itself if needed.