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

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

    h1_min, h1_max = GetJointLimits(hinge1)
    h2_min, h2_max = GetJointLimits(hinge2)


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

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

    localPos = TransformToLocalPoint(bt, pos)

    release = false
end

function tick()

    bt = GetBodyTransform(body)
    worldPos = TransformToParentPoint(bt, localPos)
    
    QueryRequire("physical dynamic")
    QueryRejectShape(clamp1)
	QueryRejectShape(clamp2)
	QueryRejectShape(base)
    local hit, p, n, s = 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

