Page 1 of 1

Problem when a table collides with a table

Posted: Sat Aug 13, 2022 8:36 pm
by galaxified
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

Re: Problem when a table collides with a table

Posted: Sat Aug 13, 2022 8:52 pm
by darkfrei
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.

Re: Problem when a table collides with a table

Posted: Sat Aug 13, 2022 9:30 pm
by ReFreezed
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).