[Solved] What is the lifetime of a Contact?

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
User avatar
idbrii
Prole
Posts: 34
Joined: Sat Jun 12, 2021 5:07 am

[Solved] What is the lifetime of a Contact?

Post by idbrii »

How do I get around "Attempt to use destroyed contact"?

I'm trying to react to collisions, but it seems like if I have too many contacts they are destroyed in the same frame they're created. Are they only valid in setCallbacks's `beginContact` function? The docs don't mention any limitations and sometimes they survive several frames.

I've boiled it down to a .love that draws normals for all contacts. It clears the list of contacts after every update, but it still gets "Attempt to use destroyed contact" when you try to stuff all the balls into a corner (which I assume is generating a lot more contacts).
physics.love
push balls into a corner to cause error
(5.17 KiB) Downloaded 141 times
The problem is essentially in this code:

Code: Select all

local physics = {
    normals = {},
    on_enter = {},
}
function physics.collisionOnEnter(fixture_a, fixture_b, contact)
    table.insert(physics.on_enter, {
            contact = contact,
        })
end

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

    for _,event in ipairs(physics.on_enter) do
        local dx,dy = event.contact:getNormal() -- error here
        local point = {event.contact:getPositions()}
        for i=1,#point,2 do
            local x,y = point[i], point[i+1]
            table.insert(physics.normals, {x,y, x+dx, y+dy})
        end
    end
    table_clear(physics.on_enter)
    ...
(I was originally using the contacts from love.draw, but that has the same problem.)

(Using love 11.3.)
Last edited by idbrii on Thu Jul 01, 2021 3:56 pm, edited 1 time in total.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: What is the lifetime of a Contact?

Post by ivan »

No, you can't store your contacts that way because they may be destroyed in the same frame. Try storing just the data you need (collision normal).
User avatar
idbrii
Prole
Posts: 34
Joined: Sat Jun 12, 2021 5:07 am

Re: What is the lifetime of a Contact?

Post by idbrii »

I guess the docs were just missing that info. I added it. Thanks!
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: What is the lifetime of a Contact?

Post by grump »

idbrii wrote: Thu Jul 01, 2021 3:55 pm I guess the docs were just missing that info.
It's right there at the top of the page:
The lifetimes of Contacts are short. When you receive them in callbacks, they may be destroyed immediately after the callback returns. Cache their values instead of storing Contacts directly.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: [Solved] What is the lifetime of a Contact?

Post by grump »

Well, crap. That was quick. How dare they?
Post Reply

Who is online

Users browsing this forum: No registered users and 74 guests