Why isn't this collision check working?

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.
Post Reply
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Why isn't this collision check working?

Post by veethree »

I'm remaking (again) my "pop" game, And i ran into some issues with a collision check. I'm checking for collisions on objects in a table, The problem is it only works for one of the objects if there are multiple. This is probably something really obvious..but i can't see it for whatever reason..

This is the code that checks for the collisions:

Code: Select all

function updateball(dt)
	for i=1, #balls do
		if checkcollision(balls[i].x, balls[i].y, 32, love.mouse.getX(), love.mouse.getY(), 1) then
			str = "true"
		else
			str = "false"
		end
	end
end
the updateball function is called in love.update.

Here's the checkcollision function:

Code: Select all

function checkcollision(x1, y1, s1, x2, y2, s2)
	if x1 > (x2 + s2) or (x1 + s1) < x2 then return false end
	if y1 > (y2 + s2) or (y1 + s1) < y2 then return false end
	return true
end
I believe that's all you need from the code, But i did include the whole löve file below.

If you wanna test it ingame you press q to add a new ball to the table.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Why isn't this collision check working?

Post by ivan »

You are overwriting the 'str' variable anytime a collision check fails.
Haven't tested your .love file but I suspect you would have to change the logic to something like:

Code: Select all

str = 'false'
for i = 1, #balls do
  if (collision) then
    str = 'true'
    break
  end
end
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Why isn't this collision check working?

Post by veethree »

ivan wrote:You are overwriting the 'str' variable anytime a collision check fails.
Haven't tested your .love file but I suspect you would have to change the logic to something like:

Code: Select all

str = 'false'
for i = 1, #balls do
  if (collision) then
    str = 'true'
    break
  end
end
Thanks man, That worked.
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 28 guests