Object Collision

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

Re: Object Collision

Post 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
Last edited by darkfrei on Thu Nov 17, 2022 1:21 pm, edited 2 times in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
fridays18
Citizen
Posts: 90
Joined: Tue Nov 01, 2022 3:24 pm

Re: Object Collision

Post 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!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests