When I delete a bullet or rock their ghosts seem to be still floating around and the ships bounce away from empty places.
I am aware you can not simply use table.remove or = nil while looping through a table because it fücks up indezes, getn() etc.
So how can this be solved?
In a new version I mark bullets for deleting and then do that in a second loop, like this:
Code: Select all
local bullet_delete_count = 0
for i,v in ipairs(bullets) do
local xs, ys = v.b:getLinearVelocity()
--check for various reasons a bullet might need to be removed
if ((xs*xs) + (ys*ys) < bullet_min_speed or v.status=="HIT" or is_offscreen (v.b:getX(), v.b:getY()) == true) then --status = "HIT" is set in collision callin after colliding with a rock
bullet_delete_count = bullet_delete_count +1
--table.remove (bullets, i) <--would break stuff
v.status = "DELETE"
end
end
---delete loop for bullets----
for bla = 1, bullet_delete_count, 1 do
for i,v in ipairs(bullets) do
if (v.status == "DELETE") then
table.remove (bullets, i)
v.b:destroy() --adding or remove
v.s = nil -- this 2 lines has no effet
bullet_delete_count = bullet_delete_count - 1
break
end
end
end
This partly works (bullets are removed from game) but the "invisible ghost" problem remains. Does this technic maybe create holes in the array/memory leaks/whatever that can cause that?
Also I noticed that getBodyCount() does not decrease after a bullet gets deleted.
This seems to be true for all kind of bodies?
Test this small example:
Code: Select all
local bodies = {}
function love.load()
world = love.physics.newWorld(2000, 2000)
end
function love.update(dt)
if love.keyboard.isDown("a") then
local new_b = love.physics.newBody(world, 100, 100)
table.insert (bodies,new_b)
end
if love.keyboard.isDown("d") then
table.remove (bodies)
end
end
function love.draw()
love.graphics.print("#bodies:" .. #bodies, 200, 210)
love.graphics.print("world:getBodyCount():" .. world:getBodyCount(), 200, 230)
end
wiki says "Get the number of bodies in the world." I read that as "number of bodies currently active in this world"
Does it mean "number of bodies that once very active in this world"?
Or more likely, what am I doing wrong with deleting the body?
Oh the text got very long now. I hope somebody understands the problem and can help
edit
Code: Select all
v.b:setX(-666) v.b:setY(-666) --get the fuck away from me
v.b:setLinearVelocity (0,0) --stay the fuck away from me