How do I detect if 2 springs are in the same place?

I have a script that generates springs between EVERY point in a folder, and this means that if there’s 2 blocks, then there should be just one spring, but there’s 2. How do I detect if a spring is in the same place as another spring, to delete unnecessary springs?
Screenshot (882)
One spring’s attachment0 is the left, and the other spring’s attachment0 is the right.

I’d suggest tweaking your generation script to not generate both in the first place. Could you share that script so we can tweak it?

local folder = Instance.new("Folder")
folder.Name = "Springs"
folder.Parent = script.Parent
for i,v in pairs(script.Parent:GetChildren()) do
	if v:IsA("BasePart") then
		task.spawn(function()
			local atta = Instance.new("Attachment")
			atta.Parent = v
			local antirotate = Instance.new("AngularVelocity")
			antirotate.Parent = atta
			antirotate.Attachment0 = atta
			antirotate.MaxTorque = math.huge
			for j,x in pairs(script.Parent:GetChildren()) do
				if x:IsA("BasePart") then
					local mag = (v.Position-x.Position).Magnitude
					if mag > 0.01 then
						task.spawn(function()
							local spring = Instance.new("SpringConstraint")
							spring.Parent = folder
							spring.Attachment0 = atta
							spring.Attachment1 = x:WaitForChild("Attachment")
							spring.FreeLength = spring.CurrentLength
							
							spring.Coils = 0
							spring.Radius = 0
							spring.Thickness = 0.1
							spring.Color = BrickColor.new("Dark stone grey")
							--spring.Visible = true
							
							spring.LimitsEnabled = true
							spring.MinLength = spring.CurrentLength / 10
							spring.MaxLength = spring.CurrentLength * 2
							
							spring.MaxForce = (v.Mass + x.Mass) * 1000
							spring.Stiffness = (v.Mass + x.Mass) * 500
							spring.Damping = (v.Mass + x.Mass)
						end)
						task.wait()
					end
				end
			end
			task.wait(1)
			v.Anchored = false
		end)
	end
end

The folder is where the springs go
ignore the antirotate part

the mag thing is so that a part doesn’t generate a spring thats just connected to itself

the whole thing works alright, it’s just laggy because theres like twice as many as there should be

It’s also slow because of all the tasks

Here’s what I’d recommend:

Two separate loops.

  1. First loop generates all the attachments

  2. Seconds loop generates all the springs, knowing there’s already attachments everywhere

Then your second loop keeps track of which attachments are connected already, and if they are it skips that attachment.

Something like this:

local folder = Instance.new("Folder")
folder.Name = "Springs"
folder.Parent = script.Parent

local function CreateSpring(a0: Attachment, a1: Attachment): SpringConstraint
	local p0 = a0.Parent
	local p1 = a1.Parent

	local spring = Instance.new("SpringConstraint")
	spring.Attachment0 = a0
	spring.Attachment1 = a1
	spring.FreeLength = spring.CurrentLength

	spring.Coils = 0
	spring.Radius = 0
	spring.Thickness = 0.1
	spring.Color = BrickColor.new("Dark stone grey")
	spring.Visible = true

	spring.LimitsEnabled = true
	spring.MinLength = spring.CurrentLength / 10
	spring.MaxLength = spring.CurrentLength * 2

	spring.MaxForce = (p0.Mass + p1.Mass) * 1000
	spring.Stiffness = (p0.Mass + p1.Mass) * 500
	spring.Damping = (p0.Mass + p1.Mass)
	
	return spring
end

-- create attachments

-- we'll make a simple list of attachments to keep track of them
local attachments: {Attachment} = {} 

-- and for each one, we make a set of attachments that it's attached to
local links: {[Attachment]: {Attachment}} = {}

for _, v in pairs(script.Parent:GetChildren()) do
	if v:IsA("BasePart") then
		local a = Instance.new("Attachment")
		a.Parent = v
		table.insert(attachments, a)
		links[a] = {}
	end
end

-- e.g. links will look something like this after/during the next loop:
-- {
--    A = { B = true },
--    B = { A = true, C = true },
--    C = { B = true },
-- }
-- So we can easily look up if something is already attached to something
-- by doing like "if links[A][B] then"

-- create springs
for _, a0 in pairs(attachments) do
	local antirotate = Instance.new("AngularVelocity")
	antirotate.Parent = a0
	antirotate.Attachment0 = a0
	antirotate.MaxTorque = math.huge
	
	for _, a1 in pairs(attachments) do
		if a0 ~= a1 and not links[a0][a1] then -- check for self and existing connections
			local s = CreateSpring(a0, a1)
			s.Parent = folder
			links[a0][a1] = s
			links[a1][a0] = s
		end
	end
end
1 Like

I have no idea what magician magic you did with the loops and tables, but it works PERFECTLY! Thanks so much!

Oh wait a minute one thing though, for some reason the springs current length is 0, therefore making the max and min AND free length 0, so it basically smashes all the parts into one small ball.

Edit: I fixed it with a simple magnitude distance thingy

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