Problem when a table collides with a table
Posted: Sat Aug 13, 2022 8:36 pm
When checking collisions between two tables, and then removing the objects that collided from their corresponding table, the next one in that table moves it's y position by -something; also I'm using the classic library for classes
I check collisions by putting this in love.update()
then v:collisionbullets is this
(b is the bullet)
And all of that works just as it should, except for the table.remove part
Basically if it removes the first object from the table, then the now first (which before the removal was second) has it's y value shifted slightly, or atleast that's how I understand it
I kinda resolved it by modyfying v:collisionbullets like so:
However x varies depending on the height of the object, I believe (e.g an object with the height of 50 is shifted by -4).
I'm not sure what this is and I do realize this is a fairly long post, but if you would like to help, I'll appreciate all of it, maybe the problem is in the library, or maybe it's in me, also I hope I provided all the needed information
I check collisions by putting this in love.update()
Code: Select all
for i,v in pairs(enemies) do
for g,h in pairs(bullets) do
v:collisionbullets(h, i, g)
end
end
Code: Select all
function Enemy:collisionbullets(b, i, g)
if self.y + self.height >= b.y and self.y <= b.y + b.height
and self.x <= b.x + b.width and self.x + self.width >= b.x then
table.remove(enemies, i)
table.remove(bullets, g)
end
end
And all of that works just as it should, except for the table.remove part
Basically if it removes the first object from the table, then the now first (which before the removal was second) has it's y value shifted slightly, or atleast that's how I understand it
I kinda resolved it by modyfying v:collisionbullets like so:
Code: Select all
function Enemy1:collisionbullets(b, i, g)
if self.y + self.height >= b.y and self.y <= b.y + b.height
and self.x <= b.x + b.width and self.x + self.width >= b.x then
table.remove(enemies, i)
table.remove(bullets, g)
for i,v in ipairs(enemies) do
if i == 1 then
v.y = v.y+x --x is the shifted value--
end
end
end
end
I'm not sure what this is and I do realize this is a fairly long post, but if you would like to help, I'll appreciate all of it, maybe the problem is in the library, or maybe it's in me, also I hope I provided all the needed information