Collision Detection on table of objects (without physics)

I am making a game in which there are multiple enemies (up to at least 3 at a time). I create the enemies using a table and I can detect collision with the main character already. Here is the issue:

Each enemy type has a different ID associated with it with different effects. When there are multiple enemies on the screen at a time and one collides with the main character, then the IDs for the enemies get switched and the wrong effect occurs. Any ideas as to why?

In the code, first I create the enemy then add it into the table. Then I check for collisions in a different function using a for loop.

I’m sorry I’m not able to help you, but I’d really like to know how you got the collision detection to work. I’ve been looking a this all day and following tutorials that it can find.

I’ve never been a fan of collision detection using physics when it’s the only reason you use physics.  It’s a lot of overhead, body creation, etc. just to get collision events.   I personally use more programmatic ways of doing this.   Please see this blog post I wrote a couple of years ago:

http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

It gives you a couple of different ways to determine if two rectangles are touching each other or to see if two circles are in the same space but it does require using a Runtime enterFrame listener or if you’re doing touch and drag you can do the detection in the moved phase of the touch handler.

These two methods are a couple of the easiest to implement.  There are also point in rectangle checks and more complex  polygon detection (though I didn’t get that working for Lua).

That just happens to be the tutorial that I actually used to solve my problem!

I ended up modifying it to fit my needs because I didn’t want to change majority of my code if I didn’t have to. The main part that solved my problem though was to add a flag to check when the collision is being tested, so that only one enemy collision could be checked at a time.

To test for collisions, I used two transparent rectangles (one for the front and one for the back) since I have enemies coming from both sides of the main character. This is the main piece of the code that I modified:

[lua]local left = (back.x - enemy.x) <= 10 and (back.x - enemy.x) >= -10
local right = (front.x - enemy.x) <= 10 and (front.x - enemy.x) >= -10

return (left or right)[/lua]

In the method I send in my enemies table item (enemies[a]) and it checks for each one at a time.

Performance isn’t the best though, as the enemies end up pausing in place on the screen, and I can’t figure out why this is or how to fix it. But it works great, thanks to that link in the answer above.

I’m sorry I’m not able to help you, but I’d really like to know how you got the collision detection to work. I’ve been looking a this all day and following tutorials that it can find.

I’ve never been a fan of collision detection using physics when it’s the only reason you use physics.  It’s a lot of overhead, body creation, etc. just to get collision events.   I personally use more programmatic ways of doing this.   Please see this blog post I wrote a couple of years ago:

http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/

It gives you a couple of different ways to determine if two rectangles are touching each other or to see if two circles are in the same space but it does require using a Runtime enterFrame listener or if you’re doing touch and drag you can do the detection in the moved phase of the touch handler.

These two methods are a couple of the easiest to implement.  There are also point in rectangle checks and more complex  polygon detection (though I didn’t get that working for Lua).

That just happens to be the tutorial that I actually used to solve my problem!

I ended up modifying it to fit my needs because I didn’t want to change majority of my code if I didn’t have to. The main part that solved my problem though was to add a flag to check when the collision is being tested, so that only one enemy collision could be checked at a time.

To test for collisions, I used two transparent rectangles (one for the front and one for the back) since I have enemies coming from both sides of the main character. This is the main piece of the code that I modified:

[lua]local left = (back.x - enemy.x) <= 10 and (back.x - enemy.x) >= -10
local right = (front.x - enemy.x) <= 10 and (front.x - enemy.x) >= -10

return (left or right)[/lua]

In the method I send in my enemies table item (enemies[a]) and it checks for each one at a time.

Performance isn’t the best though, as the enemies end up pausing in place on the screen, and I can’t figure out why this is or how to fix it. But it works great, thanks to that link in the answer above.