Documentation on BOX2D Locks

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
EliterScripts
Citizen
Posts: 85
Joined: Sat Oct 25, 2014 7:07 pm

Documentation on BOX2D Locks

Post by EliterScripts »

I submitted a bug report here: https://bitbucket.org/rude/love/issues/ ... et-me-weld and the basics is that I didn't know BOX2D locks the world during the collision callback functions. I wanted to help the project by adding the following code to various parts of the Wiki:

{{notice|Making changes to a [[World]] is not allowed inside of the [[beginContact]], [[endContact]], [[preSolve]], and [[postSolve]] callback functions, as BOX2D locks the world during these callbacks.}}

in various pages within love.physics and wanted to ask the community if you could copy and paste that notice where it is most relevant, and accurate, and make sure that I'm not spreading false information. It would suck if I was the cause of false information to someone else.

Thanks!
User avatar
slime
Solid Snayke
Posts: 3131
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Documentation on BOX2D Locks

Post by slime »

I think only the World:setCallbacks page should have it, since almost every love.physics object method that modifies the object can't be used (that's a lot of things to document), whereas World:setCallbacks is what affects those functions, so people who don't use that method won't care about those notices.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Documentation on BOX2D Locks

Post by ivan »

Not sure if this is considered off-topic, but what are the work-arounds for making welds after a collision?
There are 2 ways to make changes based on collisions.
First, you can use external flags:

Code: Select all

local marked = {}

function beginContact(fixture1, fixture2)
  local body1 = fixture1:getBody()
  local body2 = fixture2:getBody()
  if body1 == trigger then
    marked[body2] = true
  elseif body2 == trigger then
    marked[body1] = true
  end
end

function love.update(dt)
  for body in pairs(marked) do
    -- make changes here
    marked[body] = nil
  end
end
Alternatively you can use contact lists:

Code: Select all

function love.update(dt)
  local bodies = world:getBodies()
  for _, body in ipairs(bodies) do
    if body:isTouching(trigger) do
      -- make changes here
    end
  end
end
With the latter method, very brief bumps and collisions may be missed.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 44 guests