clamp1 = nil
clamp2 = nil

hinge1 = nil
hinge2 = nil

body = nil
bt = nil

localPos = nil
release = nil

function init()
	clamp1 = FindShape("clamp1")
	clamp2 = FindShape("clamp2")

	hinge1 = FindJoint("hinge1")
	hinge2 = FindJoint("hinge2")

	local loc = FindLocation("center")
	local t = GetLocationTransform(loc)
	local pos = t.pos

	body = FindBody("body")
	bt = GetBodyTransform(body)

	localPos = TransformToLocalPoint(bt, pos)

	release = false
end

function tick()
	bt = GetBodyTransform(body)
	local worldPos = TransformToParentPoint(bt, localPos)
    
	QueryRequire("physical dynamic")
	QueryRejectShape(clamp1)
	QueryRejectShape(clamp2)
	local hit, _, _, _ = QueryClosestPoint(worldPos, .3)
	if hit then
		--Point p of shape s is closest
		release = true
	end

	if release then
		SetJointMotor(hinge1, 8)
		SetJointMotor(hinge2, -8)
		--SetJointMotorTarget(hinge1, h1_min-1, 8)
		--SetJointMotorTarget(hinge2, h2_min-1, -8)
	end
end

