destroy a physics object when it collides with other ?
Posted: Fri Jun 23, 2017 4:16 am
so let say I have 10 physics rectangle and 1 other physics platform, and I want to destroy the rectangle everytime the rectangle collides with it.. I use: table.remove(table, i) it works fine for non physics object.. but on physics object.. 1 or more object will dissapear.. anyone know how to fix it ?
Code: Select all
function love.load()
love.physics.setMeter(64)
world = love.physics.newWorld(0, 9.8 * 64, true)
Nblock = {}
--adding platform
Nplatform = {}
platform = {}
platform.body = love.physics.newBody(world, 0, 500, "static")
platform.shape = love.physics.newRectangleShape(700, 25, 1400, 50)
platform.fixture = love.physics.newFixture(platform.body, platform.shape, 1)
platform.fixture:setRestitution(0.5)
table.insert(Nplatform, platform)
--using begin contact function to handle collisions in world
world:setCallbacks(beginContact, endContact, preSolve, postSolve)
timeA = 0
end
function love.update(dt)
--updating world
world:update(dt)
--timer for spawning
if timeA < 50 then
timeA = timeA + 1
end
--spawning a physics block every 1 second
if love.keyboard.isDown("space") and timeA == 50 then
block = {}
block.body = love.physics.newBody(world, love.mouse.getX(), love.mouse.getY(), "dynamic")
block.shape = love.physics.newRectangleShape(25, 25, 50, 50)
block.fixture = love.physics.newFixture(block.body, block.shape, 1)
block.fixture:setRestitution(0.5)
table.insert(Nblock, block)
timeA = 0
end
end
function love.draw()
--drawing physics blocks
for i,v in pairs(Nblock) do
love.graphics.rectangle("fill", v.body:getX(), v.body:getY(), 50, 50)
end
--drawing plaform
for i,v in pairs(Nplatform) do
love.graphics.rectangle("fill", v.body:getX(), v.body:getY(), 1400, 50)
end
end
--destroy when the blocks collide with the platform, wont work..
function beginContact(a, b, coll)
for i,v in pairs(Nplatform) do
table.remove(Nblock, i)
end
end