## Problem when a table collides with a table

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
galaxified
Prole
Posts: 3
Joined: Sun Aug 07, 2022 4:21 am

### Problem when a table collides with a table

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()

Code: Select all

for i,v in pairs(enemies) do
for g,h in pairs(bullets) do
v:collisionbullets(h, i, g)
end
end
then v:collisionbullets is this

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
(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:

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
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
darkfrei
Party member
Posts: 815
Joined: Sat Feb 08, 2020 11:09 pm

### Re: Problem when a table collides with a table

Always iterate it backwards

Code: Select all

for iTarget = #targets, 1, -1 do
local target = targets[iTarget]
for iBullet = #bullets, 1, -1 do
local bullet = bullets[iBullet]
if isBulletToTargetCollision (bullet, target) then
-- any events here
table.remove (targets, iTarget)
table.remove (bullets, iBullet)
break -- do not check any bullet-to-target collision for this removed target
end
end
end
OR in the cycle set it as

Code: Select all

enemy.valid = false
bullet.valid = false
and ignore it without actual removing. After all collidings do two cycles (backwards) to remove all not valid objects.
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

### Re: Problem when a table collides with a table

In addition to darkfrei's answer, pairs() does not iterate over items in any particular order. (It's also for iterating over all fields in a table, numeric or not. See Lua's manual.) You should be using ipairs() when you want to iterate from index 1 and forward (but not in this case, as darkfrei is explaining).
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."

### Who is online

Users browsing this forum: No registered users and 26 guests