Element in table, Width of body

Here is what I’m trying to do; I’m trying to draw all bodies in a table and if the body drops out of vision, destroy it and remove it from the table. So I need to get the position of the body (in the table) which I’m having trouble with and if possible get the body width to check specific to each body to check if it has dropped off the screen. Ideally I would add more bodies to the table in the future.

function setup()
    
    r = {}
    
    rekt = physics.body(POLYGON,vec2(10,10),vec2(-10,10),vec2(-10,-10),vec2(10,-10))
    rekt.x = WIDTH/2
    rekt.y = HEIGHT/2
    rekt.type = STATIC
    
    table.insert(r,rekt)
    
end

function draw()
 
    background(40, 40, 50)
    
    for k,body in pairs(r) do
        
        if body.x + 100 < 0 then
            --I need to change the 100 above to the bodies width if possible
            body:destroy()
            --table.remove(r,(body position?))
        end
        
        if body.x < WIDTH*2 then
            drawBody(body)
        end
        
        body.x = body.x - 4
        
    end
    
end

function drawBody(body)
   
    pushStyle()
    pushMatrix()
    
    strokeWidth(5)
    stroke(255, 255, 255, 255)
    translate(body.x, body.y)
    rotate(body.angle)
    
    if body.shapeType == POLYGON then
        strokeWidth(3.0)
        local points = body.points
        for j = 1,#points do
            a = points[j]
            b = points[(j % #points)+1]
            line(a.x, a.y, b.x, b.y)
        end
        
    elseif body.shapeType == CHAIN or body.shapeType == EDGE then
        strokeWidth(3.0)
        local points = body.points
        for j = 1,#points-1 do
            a = points[j]
            b = points[j+1]
            line(a.x, a.y, b.x, b.y)
        end
        
    elseif body.shapeType == CIRCLE then
        noFill()
        stroke(255, 255, 255, 255)
        strokeWidth(1)
        ellipse(0,0,body.radius*2)
    end

    popMatrix()
    popStyle()
end


@bhob12 An easy way to know what body needs to be destroyed is to set a flag for any body that needs to be destroyed. Then in the draw function, create a loop from the end of the body table to the beginning. Any body that has the flag set can be destroyed and removed from the table.

@bhob12 - the table position is k, in the code you have written, ie you want

table.remove(r,k)
--or
body=nil

Wrt the width, I would calculate it in setup while you are creating your physics object, and store it in that object (which is just a table), ie

--after this line
rekt.type = STATIC
--add this 
rekt.width = --insert your calculated figure here

--then in the draw function, you can write
if body.x + body.width < 100 then

I’m trying to draw all bodies in a table and if the body drops out of vision, destroy it and remove it from the table
I would prefer clip(), this old already locks the vision, how to destroy them idk.

@TokOut - ??? Huh? Clip has nothing to do with this.

Thanks guys I really appreciate the help

Yup I just tried that and it works great thanks again!!