Destroying a physics object in collide function crashes Codea

It seems that if you destroy a physics object in the collide function (when it is one of the colliding bodies), Codea crashes or hangs, as shown by the code below. Turn the Crash parameter on to see the behaviour.

If this is intended, can we put a warning in the documentation somewhere?


function setup()
    physics.gravity(0,0)
    parameter.boolean("Crash",false)
    
    circle1= physics.body(CIRCLE, 50)
    circle1.x,circle1.y=100,400
    circle1.type=DYNAMIC
    circle1.linearVelocity=vec2(80,0)
    
    circle2=physics.body(CIRCLE,50)
    circle2.x,circle2.y=600,400
    circle2.linearVelocity=vec2(-50,0)
    circle2.type=DYNAMIC
    print("I want to delete the left hand circle when they collide")
    print("With Crash turned OFF, circle is destroyed in draw function")
    print("With Crash turned ON, circle is destroyed in collide function")
end

function draw()
    background(200)
    fill(0,0,255,100)
    ellipseMode(CENTER)
    if destroy then
        circle1:destroy()
        circle1=nil
        destroy=nil
    end
    if circle1 then ellipse(circle1.x,circle1.y,100) end
    ellipse(circle2.x,circle2.y,100)
end

function collide(contact)
   if Crash then --destroy circle here
        circle1:destroy()
        circle1=nil
   else
       destroy=true --tell draw to destroy circle
   end
end

I think this is a box2d problem, you need to put the objects to be destroyed in a stack and destroy them later. See here
http://www.iforce2d.net/b2dtut/removing-bodies

ah, thank you.

This needs to be in the documentation for the destroy function and the collide function.

These pages on the Codea Wiki here and here may help.

ah, i take that back

Thank you! This helps me fix my bug.