Issue with simple physics removal test.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
thyrgle
Prole
Posts: 10
Joined: Wed Jun 26, 2013 6:30 pm

Issue with simple physics removal test.

Post 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.
Attachments
PhysicsColTest.love
(1.78 KiB) Downloaded 99 times
User avatar
seanmd
Prole
Posts: 35
Joined: Sat Jun 22, 2013 7:47 am

Re: Issue with simple physics removal test.

Post 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'
thyrgle
Prole
Posts: 10
Joined: Wed Jun 26, 2013 6:30 pm

Re: Issue with simple physics removal test.

Post 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. :(
User avatar
seanmd
Prole
Posts: 35
Joined: Sat Jun 22, 2013 7:47 am

Re: Issue with simple physics removal test.

Post by seanmd »

you're also not removing it from the toRemove list.
thyrgle
Prole
Posts: 10
Joined: Wed Jun 26, 2013 6:30 pm

Re: Issue with simple physics removal test.

Post 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. :?
thyrgle
Prole
Posts: 10
Joined: Wed Jun 26, 2013 6:30 pm

Re: Issue with simple physics removal test.

Post 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!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 2 guests