Codea crashes when defining a phyics edge body.

Like the title says, Codea seems to crash when I add a wall or edge inside any function that eventually gets called in the draw method including functions from other classes. The alternative that I have found with this is to define the edges in the setup functions. However, I am presented a problem when I want to delete these bodies or add new ones later in my game. Does anyone know what is going on?

This is the code I am using when creating my edge boundary.

physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))

Also, the app crashes exactly when another physics body collides with it, and I have written my own code to handle such collisions, but I don’t feel like that would be the problem.

@YoloSwag You don’t create physics.body objects in the draw function or any function called from draw. That will create an object 60 times per second. Create the object in setup() or some other function called from setup(). The edge code you show is correct.

If you define a body in draw codea will try to create that body 60 times a second so it will crash as too much memory is used. To delete a body call body:destroy() and then do body=nil. If you could post the rest of your code that would help to solve the collision-crash issue.

Edit:posted just after dave1707

@YoloSwag Here’s an example. Everything is created in setup(). One object will be destroyed after 20 collisions, the other after 30.


--displayMode(FULLSCREEN)

function setup()
    count=0
    e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
    e2=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
    e3=physics.body(EDGE,vec2(WIDTH,HEIGHT),vec2(WIDTH,0))
    e4=physics.body(EDGE,vec2(WIDTH,0),vec2(0,0))
    
    b1=physics.body(CIRCLE,50)
    b1.x=300
    b1.y=HEIGHT-100
    b1.restitution=1.05
    
    b2=physics.body(CIRCLE,50)
    b2.x=300
    b2.y=HEIGHT-300
    b2.restitution=1.05
end

function draw()
    background(40, 40, 50)
    stroke(255)
    strokeWidth(2)
    line(0,0,0,HEIGHT)
    line(0,HEIGHT,WIDTH,HEIGHT)
    line(WIDTH,HEIGHT,WIDTH,0)
    line(WIDTH,0,0,0)
    if b1 then
        ellipse(b1.x,b1.y,100)
    end
    if b2 then
        ellipse(b2.x,b2.y,100)
    end
end

function collide(c)
    if c.state==BEGAN then
        count=count+1
        print(count)
        if count==20 then
            b1:destroy()
            b1=nil
        end
        if count==30 then
            b2:destroy()
            b2=nil
        end
    end
end



Thank you. @Coder and @dave1707

So how would I create an edge that adapts to width and height if it can only be called in the setup function. Because when I turn my device sideways the width from portrait mode still appears. @dave1707 and @Coder.

@YoloSwag I’m pretty sure you can’t create an edge that adjusts when the orientation changes only using the setup function. This is because the setup function is only called once when you start to run your code. However, you can setup your edge in the setup function and adjust it using the draw function. Here is an example of this.

function setup()
parameter.integer("lines",0,1)
edges = {}
top = HEIGHT
right = WIDTH
end

function draw()

if CurrentOrientation == 0 then --if the current orientation is portrait
top = HEIGHT
right = WIDTH
create()
elseif CurrentOrientation == 3 then --if the current orientation is landscape
top = HEIGHT
right = WIDTH
create()
end

if lines == 1 then
fill(255,255,255,255)
line(0,top,0,0)
line(right,top,right,0)
end
end

function create()
edgeLeft = physics.body(EDGE,vec2(0,top),vec2(0,0))
edgeRight = physics.body(EDGE,vec2(right,top),vec2(right,0))
table.insert(edges,edgeLeft)
table.insert(edges,edgeRight)
end

EDIT: I haven’t checked, but I don’t think this code works. Sorry for my misunderstanding.

@Staples I guess I didn’t say what I meant. You can’t “constantly” create a physics object from draw or a function called from draw. But you can create objects from draw or a function called from draw if you don’t do it constantly. Below is an example of re-creating the edge lines only when you rotate the screen.


displayMode(FULLSCREEN)

function setup()
    setup1()
    b1=physics.body(CIRCLE,50)
    b1.x=250
    b1.y=HEIGHT-100
    b1.restitution=1.1
    b2=physics.body(CIRCLE,50)
    b2.x=300
    b2.y=HEIGHT-300
    b2.restitution=1.1
    w=WIDTH
end

function setup1()
    if e1 then  -- if objects exits, then destroy them
        e1:destroy() e1=nil
        e2:destroy() e2=nil
        e3:destroy() e3=nil
        e4:destroy() e4=nil
    end
    -- recreate edge lines with new width and height
    e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
    e2=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
    e3=physics.body(EDGE,vec2(WIDTH,HEIGHT),vec2(WIDTH,0))
    e4=physics.body(EDGE,vec2(WIDTH,0),vec2(0,0))
end

function draw()
    background(40, 40, 50)
    stroke(255)
    strokeWidth(2)
        
    if w~=WIDTH then -- screen was rotated
        setup1() -- call routine to recreate edge lines.
        w=WIDTH
    end
    
    line(0,0,0,HEIGHT)
    line(0,HEIGHT,WIDTH,HEIGHT)
    line(WIDTH,HEIGHT,WIDTH,0)
    line(WIDTH,0,0,0)
    ellipse(b1.x,b1.y,100)
    ellipse(b2.x,b2.y,100)
end

@YoloSwag @Staples Sorry, I sent the above response to the wrong person.

@dave1707 I’m a little confused on what you are saying, but I think I understand. I forgot that you cannot change the location of a physics body by changing its x and y values like a sprite or ellipse. So, now I have to understand this, can you tell me if my new edited code will work?

@Staples The edge objects aren’t moving like the balls, so you don’t have to worry about their velocities. So when the screen is rotated, you can just destroy the current edges and recreate the new edges with the new width and height values.

You only need to redefine the edges in the

orientationChanged(newOrientation)

function as @dave1707 said

.

@YoloSwag …?

@SkyTheCoder He removed what he had posted.

@YoloSwag I already gave you an example above to handle rotating the screen. Your code is making it more complicated then it needs to be.