Page 1 of 1

Issue with simple physics removal test.

Posted: Sun Jul 07, 2013 5:57 am
by thyrgle
In my game I am trying to have it so I can remove items when a ball touches them. I have issues trying to remove the items. I have attached a small test case and tried implementing an updated-to-0.8.0 (by making shapes->fixtures) version of this: https://love2d.org/wiki/Remove_Workaround. The ball should fall through the block in this test case and destroy the block, yet it crashes and gives an error, which is also what happens in my much bigger game, saying:
Attempt to use destroyed fixture
Thanks.

Re: Issue with simple physics removal test.

Posted: Mon Jul 08, 2013 2:43 am
by seanmd
You should probably make sure the object you're trying to draw actually has a body before you ask it anything about its body. It'll get self conscious and throw an error like that one.

Once you destroy the fixture the body becomes unavailable, and you're trying to draw it by using its body. No biggie. Keep pluggin'

Re: Issue with simple physics removal test.

Posted: Mon Jul 08, 2013 2:57 am
by thyrgle
I have tried your suggestion and changed my draw code to prevent anything from happening if there is no body by checking if any body is nil:

Code: Select all

function love.draw()
    love.graphics.setColor(255,0,0)
    if ball.body ~= nil then
        love.graphics.circle("line", ball.body:getX(), ball.body:getY(), ball.shape:getRadius())
    end
    love.graphics.setColor(0,255,0)
    if block.body ~= nil then
        love.graphics.polygon("line", block.body:getWorldPoints(block.shape:getPoints()))
    end
end
It does not work still unfortunately. :(

Re: Issue with simple physics removal test.

Posted: Mon Jul 08, 2013 3:23 am
by seanmd
you're also not removing it from the toRemove list.

Re: Issue with simple physics removal test.

Posted: Mon Jul 08, 2013 3:58 am
by thyrgle
Ahh, now I can get it to sort of work. It now runs if I change the update function to this:

Code: Select all

function love.update(dt)
    world:update(dt)

    for _, fixture in ipairs(toRemove) do
        fixture:destroy()
    end

    for i = #toRemove,1,-1 do
        table.remove(toRemove,i)
    end
end
But for some reason the square doesn't disappear... Replacing fixture:destroy() with fixture:getBody():destroy() also gives an error. Thanks for all your help so far! I'm just not quite getting it. :?

Re: Issue with simple physics removal test.

Posted: Mon Jul 08, 2013 10:30 pm
by thyrgle
Ok, I was able to figure it out by clearing the block table in the "toRemove" loop, destroying the body and not the fixture, and checking if block.body is nil it worked. I don't know if that results in a memory leak though. :shock: I hope it doesn't. Thanks for the help!