Wow, it did not take long to localize the problem. On my end replacing the second to last callback with nil makes the crashes go away. Please try to confirm this. To be clear, the line that sets the callbacks should be:
Code: Select all
TheWorld:setCallbacks(Coll_Add, Coll_Persist, nil, Coll_Result)
With this code in place it does not segfault. How about you?
ADDED LATER: I think the problem is with the Remove callback causing a crash when a shape still in contact with something is destroyed. Here is a workaround that might work. Please report your results.
The way this works is that when a shape is to be destroyed, the code sets it to not collide with nothing at all. Then after the next physics update is run, this shape is no longer in contact with anything, and can then be destroyed without triggering a segfault. And by it working I am really just saying that it did not seem to crash for me.
This seems like an issue that should be on tracker. EDIT: And it si now, because I added it.
Code: Select all
function love.load()
math.randomseed(os.time())
math.random()
love.graphics.setBackgroundColor(50, 80, 120, 255)
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
--Create a world in Box2d:
TheWorld = love.physics.newWorld(10000, 10000)
TheWorld:setGravity(0, 700)
--COMMENT THIS LINE OUT AND THE PROGRAM WILL NOT CRASH!
TheWorld:setCallbacks(Coll_Add, Coll_Persist, Coll_Rem, Coll_Result)
-------------------------------------------------------
--Create a floor box for things to rest on:
TheWorldBody = love.physics.newBody(TheWorld, width/2, height/2, 0, 0)
TheWorldShape = love.physics.newRectangleShape(TheWorldBody, 0, width/3, width*2, 100, 0)
--Create the table that will hold the rects:
RectTable = {}
end
function love.update(dt)
TheWorld:update(dt)
if GetRid then
GetRid.shape:destroy()
GetRid.body:destroy()
GetRid = nil
end
end
function love.draw()
DrawRects()
DrawFloor()
DrawBodyCount(25, 25, 0)
end
-------------EMPTY CALLBACK FUNCTIONS--------
function Coll_Add(a, b, coll)
--Empty.
end
function Coll_Persist(a, b, coll)
--Empty.
end
function Coll_Rem(a, b, coll)
--Empty.
end
function Coll_Result(a, b, coll)
--Empty.
end
---------------------------------------------
function love.mousepressed(x, y, button)
if(button == "l") then
AddRect(x, y, math.random(5, 50), math.random(5, 50), math.random(0, 2*math.pi))
elseif(button == "r") then
KillRect(1) --Removes the oldest existing rectangle.
end
end
function AddRect(x, y, w, h, rot) --Adds a new rectangle to RectTable.
local addTable = {}
addTable.body = love.physics.newBody(TheWorld, x, y, 1, 1)
addTable.shape = love.physics.newRectangleShape(addTable.body, 0, 0, w, h, rot)
addTable.body:setMassFromShapes()
table.insert(RectTable, addTable)
end
function KillRect(index) --Removes the rectangle at position 'index' in RecTable.
local rect = RectTable[index]
if(rect) then
rect.shape:setMask(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
GetRid = rect
table.remove(RectTable, index)
end
end
function DrawRects() --This draws the rectangles in the table 'RectTable'
for i=#RectTable, 1, -1 do
love.graphics.setColor(255, 255, 255, 255)
love.graphics.polygon("fill", RectTable[i].shape:getPoints()) --white inside.
love.graphics.setColor(0, 0, 0, 255)
love.graphics.polygon("line", RectTable[i].shape:getPoints()) --black outline.
end
end
function DrawBodyCount(x, y, rot) --Prints the current number of bodies as seen by Box2d.
love.graphics.setColor(255, 255, 255, 255)
local num = (TheWorld:getBodyCount() - 1) --I'm subracting one from the body count because Box2d counts the world as a body.
local t = "Bodies: " .. tostring(num)
love.graphics.print(t, x, y, rot, 1, 1)
end
function DrawFloor()
love.graphics.setColor(255, 255, 255, 255)
love.graphics.polygon("fill", TheWorldShape:getPoints()) --white inside.
love.graphics.setColor(0, 0, 0, 255)
love.graphics.polygon("line", TheWorldShape:getPoints()) --black outline.
end