[Solved] Colliding with multiple objects!

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
PennilessOptimist
Prole
Posts: 3
Joined: Wed Jul 17, 2013 3:15 am
Location: Southern California, USA

[Solved] Colliding with multiple objects!

Post by PennilessOptimist »

Hello, community! I am new-ish to love (I've used it on and off the past two or so months) and I finally started a permanent project about 2 or 3 weeks ago. My game uses SECS for an object system and Hardon Collider for collision detection.
I've successfully implemented Hardon Collider mostly, except that I cannot think of a way to check multiple objects at once, without having to type every object out (which would be rather redundant and is clearly not an option). I've tried searching the forums, and even saw a few recent posts that had this same problem, but alas none of those threads could help me much!

Colliding isn't an issue, for if you modify the code for moving downwards in entity/Hero.lua to include:

Code: Select all

if love.keyboard.isDown("down") then
	if not self.mask:collidesWith(tilefield[1].mask) --including this bit here
	movementcode
	end --and this
then it clearly works with colliding on the top of tilefield[1] (that is the tile that stands out on the very bottom of the screen).
I was thinking that iterating through the tilefield and checking for the mask and stuff would work out, but I couldn't implement that properly.
I need your help, love community!
I've figured out most of the code for this on my own, as well as with researching, so I'm not sure how efficient my code is. Or rather, I'm not sure how much more efficient my code could be (probably a lot more), so please do tell me if I'm doing anything particularly badly.
Thank you in advance for your time! :crazy:
Controls are arrow keys for movement, and use the ~ button for console interactions.
Last edited by PennilessOptimist on Wed Jul 17, 2013 8:42 pm, edited 2 times in total.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Colliding with multiple objects!

Post by substitute541 »

Uh, couldn't you do:

Code: Select all

if love.keyboard.isDown("down") then
   for i=1, #tilefield do
      if not self.mask:collidesWith(tilefield[i].mask)
         movementcode
      end
   end
?
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
PennilessOptimist
Prole
Posts: 3
Joined: Wed Jul 17, 2013 3:15 am
Location: Southern California, USA

Re: Colliding with multiple objects!

Post by PennilessOptimist »

substitute541 wrote:Uh, couldn't you do:

Code: Select all

if love.keyboard.isDown("down") then
   for i=1, #tilefield do
      if not self.mask:collidesWith(tilefield[i].mask)
         movementcode
      end
   end
?
Alas, it doesn't! Not only does it not check the collision, but it also just makes the character zoom out of the screen (probably because its repeating the move code however many tiles there are in the tile field.) Iterators like this don't seem to work at all.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Colliding with multiple objects!

Post by Robin »

Code: Select all

function canmove()
   for i=1, #tilefield do
      if self.mask:collidesWith(tilefield[i].mask)
         return false
      end
   end
   return true
end

-- in love.update:
if love.keyboard.isDown("down") and canmove() then
    movementcode
end
EDIT: or:

Code: Select all

if love.keyboard.isDown("down") then
   for i=1, #tilefield do
      if not self.mask:collidesWith(tilefield[i].mask)
         movementcode
         break
      end
   end
Help us help you: attach a .love.
User avatar
PennilessOptimist
Prole
Posts: 3
Joined: Wed Jul 17, 2013 3:15 am
Location: Southern California, USA

Re: Colliding with multiple objects!

Post by PennilessOptimist »

Your first suggestion worked, Robin! Thank you very much!
Your second suggestion didn't work, though. I don't know why iterating through them like that doesn't work, but I suppose its a mystery I'll solve later.
Thank both of you very much for the help, though.
Cheers!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: [Solved] Colliding with multiple objects!

Post by Robin »

Oh, of course, brain fart. The second one moves when it doesn't collide with a single thingy, instead of not colliding with all the thingies.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: slime and 5 guests