Page 1 of 1

Multiple Entity Collsions

Posted: Sat Apr 04, 2015 7:29 pm
by Carracer12
I know it's probably been asked a million times but after looking through previous threads, I still don't understand how to do it. Basically I want to detect a collision between the player and a meteor and if it hits him, perform an action (Still not sure whether to do health or one hit = death) Any help would be appreciated!

Thanks

Carracer12

Re: Multiple Entity Collsions

Posted: Sat Apr 04, 2015 7:51 pm
by MicroMacro
Let me look at your code, and see what I can do.
I've downloaded the .löve file and do some stuff. :)

Re: Multiple Entity Collsions

Posted: Sat Apr 04, 2015 8:00 pm
by MicroMacro
AHAH! I did it! Ill post code when I get it fine tuned. :)

Re: Multiple Entity Collsions

Posted: Sat Apr 04, 2015 8:15 pm
by MicroMacro
Okay, so it's not perfect. I've commented where I've edited, and I've enabled the console so that you can see when it collides. It should print "dead" when it collides.

Re: Multiple Entity Collsions

Posted: Sat Apr 04, 2015 8:30 pm
by MadByte
There are several ways to acomplish this, depending on what kind of game you create.
The most basic and easiest way I can imagine is following:

Scenario: Player collides with multiply other objects(using boundingBoxs)
--------------------------------------------------------------
First, you need to set an extra boolean "isRemoved" or "isDestroyed" (something like that) for all of your objects.
then you update and draw them all togehter using a extra container table and a for loop. In the update loop you check if this boolean is already set to true. if not - keep updating the object. otherwise remove it from the container table.

Then for collision your player object get a ":intersects(object)" method or something like that. Go through all objects via another for loop and check if any of them is colliding with the player object. if an object collides with the player object then do whatever you want with it.

Here is this idea packed into an .love archive :P
collisionExample.love
WASD to move.

The example may look a bit complex if you are new to löve, but you'll see the benefits of this - i promise!
If you have any further question, feel free to ask. :)

Re: Multiple Entity Collsions

Posted: Mon Apr 06, 2015 10:35 am
by Carracer12
Ok, so I understand how MicroMacro did it however it only works about 50% of the time whenever the meteor hits him in the head, he can survive being hit in the leg apparently :P

I think I understand MadByte's method but am not entirely sure how to implement it, I think you are more or less doing the same thing but I assume the rest of it makes it more accurate? The reason I think you are doing the same thing is that you use the
self.x + self.w > object.x and self.x < object.x + object.w and self.y + self.h > object.y and self.y < object.y + object.h
There is just little bits that I don't get like why you need a line saying:
return self
I kinda wanna be able to do it how MadByte does because it seems to work 100% of the time (sin the example anyway)

Thanks for your assisstance so fart, any further help would be appreciated :)

Re: Multiple Entity Collsions

Posted: Mon Apr 06, 2015 12:00 pm
by MadByte
Carracer12 wrote:Ok, so I understand how MicroMacro did it however it only works about 50% of the time whenever the meteor hits him in the head, he can survive being hit in the leg apparently :P
Sounds like the collision box doesn't have the same size as the player.
Carracer12 wrote:The reason I think you are doing the same thing is that you use the
self.x + self.w > object.x and self.x < object.x + object.w and self.y + self.h > object.y and self.y < object.y + object.h
This is just the bounding box check.The difference between your code and mine is that I organised
everything a bit different.
Carracer12 wrote:I think you are more or less doing the same thing but I assume the rest of it makes it more accurate?
My method may not be more accurate, but in the long term its much easier to use and help to keep the code readable.
Carracer12 wrote:There is just little bits that I don't get like why you need a line saying:
return self
The "return self" passes the object table I created with the newPlayer function to a variable. That makes it possible to create different
"instances" of an object and use this variable name to call the object methods as in the following example:

Code: Select all

function newPlayer(name, x, y)
  local self = {}
  self.name = name
  self.x = x
  self.y = y

  function self:draw()
    love.graphics.print("Hi, I am "..self.name, self.x, self.y)
  end

  return self
end

function love.load()
  bob = newPlayer("Bob", 10, 10) -- this works because the newPlayer function return the self table
  carl = newPlayer("Carl", 10, 30)
end

function love.draw()
  bob:draw() -- draws "Hi, I am Bob" to 10, 10
  carl:draw() -- draws "Hi, I am Carl" to 10, 30
end


Re: Multiple Entity Collsions

Posted: Mon Apr 06, 2015 4:57 pm
by Carracer12
Okay cool, is there any way to make collsions more reliable?

Re: Multiple Entity Collsions

Posted: Mon Apr 06, 2015 5:34 pm
by MadByte
Carracer12 wrote:Okay cool, is there any way to make collsions more reliable?
Collisions are reliable if they get calculated right. In your game, you use the wrong width and height values to calculate
the players collision box. and you scaled your player graphics by 4, this need to be considered when setting the player.width
and player.height values.

Edit
Here you are. I rewrote the "spawn.lua" because it was unreadable and it had several problems. Also, I added "love.graphics.setDefaultFilter("nearest", "nearest")" to your main.lua. This make your graphics more pixelated.

You really need to organise your code better in future. It will get much easier to find bugs and add stuff then.
Good Luck.
MeteorsFixedCollisions.love

Re: Multiple Entity Collsions

Posted: Tue Apr 07, 2015 4:13 pm
by Carracer12
Ok, thanks, didn't think of that when scaling! My player.lua is a mess, not sure anyone could understand that but me, I'll try to organise in the future :P