Page 2 of 2

Re: Object Collision

Posted: Thu Nov 17, 2022 1:16 pm
by darkfrei
fridays18 wrote: Thu Nov 17, 2022 1:14 pm
ooo okay so fore example

Code: Select all

for i,v in ipairs(listOfBullets) do
	for i2, v2 in ipairs(listOfEnemies) do
		local isCollision = basicCollisionFunction (v.x,v.y,v.w,v.h, v2.x,v2.y,v2.w,v2.h)
	end
end
something like this?

Yes, but also w and h
https://love2d.org/wiki/BoundingBox.lua

Re: Object Collision

Posted: Thu Nov 17, 2022 1:20 pm
by fridays18
Just checked on my project and worked perfect tysm for all the help!

Exact code in case anyone ever needs to use it:

{code}
--when bullet and enemy touch
for i,v in ipairs(listOfBullets) do
for i2, v2 in ipairs(listOfEnemies) do
BuEn = detectCollision(v.x,v2.x,v.y,v2.y,20)
if BuEn == true then
table.remove(listOfBullets, i)
table.remove(listOfEnemies, i2)
end
end


--collision function
function detectCollision(x1, x2, y1, y2, sens)
if math.abs(x1 - x2) < sens and math.abs(y1 - y2) < sens then
return true
else
return false
end
end
{/code}

And the tutorial I used to learn class and object stuff is this (https://sheepolution.com/learn/book/14)

Thank you for everyone who helped in this post!