Remove Workaround

Workaround for a Physics module issue

There is an unresolved issue with the third callback of World:setCallbacks, the remove callback. If there are active contacts when a shape is destroyed, the next physics update will cause a crash when LÖVE attempts to call the callback. This issue appears at least in LÖVE version 0.6.2.

To work around this problem, a shape must first leave from all the active contacts before it can be destroyed safely. This can be done by moving the shape into a special area, or more simply by setting the shape to not collide with anything.

-- remove a shape from all contact categories
shape:setMask(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
-- add it into the pending destroy list
table.insert(removals, shape)
-- either remove the shape from the list of drawn objects, or mark
-- it with a flag so it won't be no longer drawn

After this is done, the shape will no longer have an affect on the world. After the next physics update is performed, it will also have lost all of its contacts, and can be safely destroyed.

-- world update happens usually in love.update callback
world:update(dt)
-- you can follow it by destroying any shaped listed to be removed
for _, shape in ipairs(removals) do
	shape:destroy()
end

Remember that updating the world before removing the shape makes sure it will no longer be in contact with anything.

See also

Other Languages