Physics switching off in tilting a ball through a maze when switching scenes.

@ChrisKarpinski I just tried some code similar you yours above and when you set tab2[counter]=nil, that entry is now the end of the table. So you’ve been destroying only the first entry of the table and leaving all the other physics bodies as live objects.

@Ignatz Actually, the bodies will hang around, even if there is no value to store them…just setting them to nil doesn’t do anything, because the physics engine has its own cache of bodies in the simulator (and that list can only be affected by the functions physics.body() and body:destroy())

@SkyTheCoder - Not true, as this code proves. The floor variable is set to nil after 2 seconds. The floor disappears as soon as garbage is collected, either by pressing the button, or by waiting for Codea to do it itself. (It used to collect garbage every 60 seconds or so, but now it seems to only do so every five minutes or so, so be patient).

function setup()
    a=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0)) --floor
    a.restitution=1
    b=physics.body(CIRCLE,50)
    b.x=WIDTH/2
    b.y=HEIGHT/2
    parameter.text("Time","")
    parameter.action("CollectGarbage",function() collectgarbage() end)
end

function draw()
    background(50)
    fill(255,0,0)
    ellipse(b.x,b.y,100)
    if ElapsedTime>2 then a=nil end
    Time=math.floor(ElapsedTime)
end

@dave1707 I just tried starting from the end of tab2 and doing what you showed and it worked, I tested it using some text and once the ball reached the end, #tab2 was 0. But when I went back to the maze after switching scenes, the text of #tab2 still showed 0, so for some reason new edges are not being created.

@ChrisKarpinski Are the edges being created correctly when the program first starts. If so, now the fun begins. Start putting a print statements in your code and try to see if your code is executing how you think it’s supposed to. Start where you build tab2 with the edges.

@dave1707 I tested the way that the edges are being created and it seems correct to me, the edges are created through a pb function with this code:


function MazeScene:pb(x,y,x1,y1) 
    
    local z
    if not done then
        for z=1,#tab1 do
            if tab1[z].x==x and tab1[z].y==y and
                tab1[z].z==x1 and tab1[z].w==y1 then
                    return
            end
        end
        table.insert(tab1,vec4(x,y,x1,y1))
        table.insert(tab2,physics.body(EDGE,vec2(x,y),vec2(x1,y1)))
        tab2[#tab2].sleepingAllowed=false
        
    end
        end

But the pb function only seems to work at the first time you go through the maze, then it does not make any more edges in tab2 and #tab2 still remains 0 at all times in the program after all of the original edges and ball are destroyed. I used print statements to check how many edges were in tab2 and I’m pretty sure that the problem lies in the pb function because even if I try to make new edges in setup, it creates them and adds them to tab2 at first but they are not created after the original edges are destroyed and after I first change scenes and go back to the Maze scene. Do you know why the pb function only seems to create the edges before the original set of edges is destroyed, or is something else wrong?

The most obvious thing I can think of is that you are destroying the edges in table 2 but not clearing table 1, but maybe that is too obvious

@Ignatz Yeah I tried that, tab1 contains just the walls of the maze, no physics bodies, and new walls of the maze were being created anyway, so it didn’t change much.

@ChrisKarpinski I don’t know why, but I enjoy debugging programs. It’s like detective work. Add the line I indicate below to your code.

    if c1.x > 800 then
        Scene.Change("Screen")
        done=false          -- add this line to your code.
    end

@dave1707 Wow it works, you are a genius! I can’t believe how just that one simple line of code was causing the whole problem. It’s amazing how just one line of code can change so much. Thanks so much Dave and to everyone that helped!

@ChrisKarpinski After I loaded your code, it took all of 5 minutes to find the problem. The last couple of years before I retired, I was in support and did nothing but debug programs. I developed a pretty good routine for finding problems and correcting them.