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