Creating a Circuit

My application is to have a circuit board and use wires, batteries, and light bulbs to create a circuit, I have been working hard figuring out spawning objects, deleting them, rotating them, etc., but now I am stuck on how to determine whether I have a complete circuit or not. Firstly, I would need to do something along the lines of collision, I assume, to see whether the objects are connecting. I need to pass a variable through the objects that are part of the circuit, and if it returns to the power source, then it is completed and should play a sequence. Here is some of my code. If you have any advice, I would love to hear from you (you might need to put it in simple terms since I am a newb). Here is a picture of what I am trying to do as well.

circuit.png

[lua]

    local container1 = display.newRect(x1,y1,sizeX,sizeY)

    container1.strokeWidth=2

    container1:setFillColor(200,200,200)

    container1:setStrokeColor(0,0,0)

etc

    

    function activate(event)

        scene.activeObject = event.target

        return true

    end

    function rotateActive(event)

    local activeObject = scene.activeObject

        if activeObject ~= nil then

            activeObject.rotation = activeObject.rotation + 90

        end

    return true

    end

    function delete(event)

    local activeObject = scene.activeObject

        if activeObject ~= nil then

        activeObject:removeSelf()

        end

    return true

    end

    

    function battery1vMove( event )

        local battery1v = event.target

        if event.phase == “began” then

            battery1v.markX = battery1v.x    – store x location of object

            battery1v.markY = battery1v.y    – store y location of object

        elseif event.phase == “moved” then

            local x = (event.x - event.xStart) + battery1v.markX

            local y = (event.y - event.yStart) + battery1v.markY

            battery1v.x, battery1v.y = x, y    – move object based on calculations above        

        elseif event.phase == “ended” then

            local x = (event.x - event.xStart) + battery1v.markX

            local y = (event.y - event.yStart) + battery1v.markY

            – main condition: I calculated 3 areas to attract the object to the target container, 2 areas that attract it when it’s 1/3 in the target and 1 area that attract it when it’s 1/4 in the target

            if (((x >= ((sizeX/2) + x1 - sizeX)) and (y >= ((sizeY/2) + y1 - sizeY)) and (x <= ((sizeX/2) + x1 + sizeX)) and (y <= ((sizeY/2) + y1 + sizeY))) or ((x >= ((sizeX/2) + x1 - sizeX)) and (y >= ((sizeY/2) + y1 - sizeY)) and (x <= ((sizeX/2) + x1 + sizeX)) and (y <= ((sizeY/2) + y1 + sizeY))) or ((x >= ((sizeX/2) + x1 - sizeX)) and (y >= ((sizeY/2) + y1 - sizeY)) and (x <= ((sizeX/2) + x1 + sizeX)) and (y <= ((sizeY/2) + y1 + sizeY)))) then

                battery1v.x, battery1v.y = x1 + (sizeX * 1.07), y1 + (sizeY * 1.3);

    local function spawn1v(params)

    local battery1v = display.newImage(params.image)

        --Set the objects table to a table passed in by parameters

        battery1v.objTable = params.objTable

        --Automatically set the table index to be inserted into the next available table index

        battery1v.index = #battery1v.objTable + 1

        

        --Give the object a custom name

        battery1v.myName = "battery1v : " … battery1v.index

        --If the object should have a body create it, else dont.

        if params.hasBody then

            --Allow physics parameters to be passed by parameters:

            battery1v.density = params.density or 0

            battery1v.friction = params.friction or 0

            battery1v.bounce = params.bounce or 0

            battery1v.isSensor = params.isSensor or false

            battery1v.bodyType = params.bodyType or “dynamic”

            physics.addBody(battery1v, battery1v.bodyType, {density = battery1v.density, friction = battery1v.friction, bounce = battery1v.bounce, isSensor = battery1v.isSensor})

        end

        --The battery1vs group

        battery1v.group = params.group or nil

        --If the function call has a parameter named group then insert it into the specified group

        if params.group then

            battery1v.group:insert(battery1v)

        end

        --Insert the battery1v into the table at the specified index

        battery1v.objTable[battery1v.index] = battery1v

        if (battery1v.index > 1) then

            battery1v:addEventListener(“touch”, activate)

            battery1v:addEventListener(“touch”, battery1vMove)        

        end

        startGroup:insert(battery1v)

        return battery1v

    end

    local localGroup = display.newGroup()

    --Create a table to hold our spawn1vs

    local spawn1vTable = {}

    i = 0

    local spawn1vs = spawn1v(

    {

    image = “battery1v.png”,

    objTable = spawn1vTable,

    hasBody = true,

    friction = 0.4,

    bounce = 0.4,

    bodyType = “static”,

    group = localGroup,

    }

    )

    i = i +1

    

    local function new1v

    (event) i = 0

    

    local spawn1vs = spawn1v(

    {

    image = “battery1v.png”,

    objTable = spawn1vTable,

    hasBody = true,

    friction = 0.4,

    bounce = 0.4,

    bodyType = “static”,

    group = localGroup,

    }

    )

    i = i +1

        for i = 1, #spawn1vTable do

        print(spawn1vTable[i].myName)

        end

    end

    

    spawn1vTable[i]:addEventListener(“tap”, new1v)

    spawn2vTable[i]:addEventListener(“tap”, new2v)

    spawnWireTable[i]:addEventListener(“tap”, newWire)

    spawnelbowTable[i]:addEventListener(“tap”, newElbow)

    spawnbulbTable[i]:addEventListener(“tap”, newBulb)

    btnRotate:addEventListener(“tap”, rotateActive)

    btnDelete:addEventListener(“tap”, delete)

end

scene:addEventListener( “createScene”, scene )

return scene

[/lua]

Hi @nthndgrt,

There are a few non-physics ways to accomplish this, but if you want to use physics for it, I’d suggest that you explore raycasting as a means to sense some object(s) in the line “path” cast out from another object. You can start by reading this tutorial:

http://www.coronalabs.com/blog/2013/05/07/physics-raycasting-and-reflection/

Best regards,

Brent Sorrentino

Hi @nthndgrt,

There are a few non-physics ways to accomplish this, but if you want to use physics for it, I’d suggest that you explore raycasting as a means to sense some object(s) in the line “path” cast out from another object. You can start by reading this tutorial:

http://www.coronalabs.com/blog/2013/05/07/physics-raycasting-and-reflection/

Best regards,

Brent Sorrentino