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
Multiple Entity Collsions
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 9
- Joined: Mon Dec 08, 2014 8:24 am
Multiple Entity Collsions
- Attachments
-
Meteors.love
- (9.46 KiB) Downloaded 148 times
- MicroMacro
- Citizen
- Posts: 92
- Joined: Fri May 30, 2014 2:30 am
- Location: Boston, MA, USA
- Contact:
Re: Multiple Entity Collsions
Let me look at your code, and see what I can do.
I've downloaded the .löve file and do some stuff.
I've downloaded the .löve file and do some stuff.

https://github.com/ebernerd- where you can find all my work.
- MicroMacro
- Citizen
- Posts: 92
- Joined: Fri May 30, 2014 2:30 am
- Location: Boston, MA, USA
- Contact:
Re: Multiple Entity Collsions
AHAH! I did it! Ill post code when I get it fine tuned. 

https://github.com/ebernerd- where you can find all my work.
- MicroMacro
- Citizen
- Posts: 92
- Joined: Fri May 30, 2014 2:30 am
- Location: Boston, MA, USA
- Contact:
Re: Multiple Entity Collsions
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.
- Attachments
-
Meteors.love
- (9.67 KiB) Downloaded 158 times
https://github.com/ebernerd- where you can find all my work.
Re: Multiple Entity Collsions
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
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.
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

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.

-
- Prole
- Posts: 9
- Joined: Mon Dec 08, 2014 8:24 am
Re: Multiple Entity Collsions
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 
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
Thanks for your assisstance so fart, any further help would be appreciated

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
There is just little bits that I don't get like why you need a line saying: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
I kinda wanna be able to do it how MadByte does because it seems to work 100% of the time (sin the example anyway)return self
Thanks for your assisstance so fart, any further help would be appreciated

Re: Multiple Entity Collsions
Sounds like the collision box doesn't have the same size as the player.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
This is just the bounding box check.The difference between your code and mine is that I organisedCarracer12 wrote:The reason I think you are doing the same thing is that you use theself.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
everything a bit different.
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:I think you are more or less doing the same thing but I assume the rest of it makes it more accurate?
The "return self" passes the object table I created with the newPlayer function to a variable. That makes it possible to create differentCarracer12 wrote:There is just little bits that I don't get like why you need a line saying:return self
"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
-
- Prole
- Posts: 9
- Joined: Mon Dec 08, 2014 8:24 am
Re: Multiple Entity Collsions
Okay cool, is there any way to make collsions more reliable?
Re: Multiple Entity Collsions
Collisions are reliable if they get calculated right. In your game, you use the wrong width and height values to calculateCarracer12 wrote:Okay cool, is there any way to make collsions more reliable?
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.
-
- Prole
- Posts: 9
- Joined: Mon Dec 08, 2014 8:24 am
Re: Multiple Entity Collsions
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 

Who is online
Users browsing this forum: Bing [Bot], Semrush [Bot] and 5 guests