Detecting collision : which solution is better ?

Hello there !

I’m kinda new in the Corona SDK world and I am starting my very first project.

My game needs to detect collisions between a lots of items, each collisions supposedly calling new functions (to add some graphical effects).

Right now, I’m only using one heavy listener,with a lot of conditions calling the functions.

But I was wondering if it was the good way to do it, since I could have set several “tiny” differents event listener.

So, which one is the best way to do it ? Or is there any other way ?

Hi @evans.randria,

In general, there are two approaches to collisions:

  1. You can apply collision detection to one or more objects individually, and then sense collision with “other” objects in the world. This approach is logical if you have, for example, one “player” character and you want to sense collisions on that player against basically anything else in the world… enemies, walls, bullets, etc. This method is very efficient when you’re dealing with that kind of scenario, because only the player needs to have a collision listener. The other objects don’t necessarily need to have collision listeners, unless they require their own separate collision detection with objects that are not the player.

  2. You can use a Runtime collision detection setup where essentially every object in the world senses collision with anything else, and in return (from the collision event) you’ll get the IDs of both objects involved in the collision. This scenario is logical when you have a ton of objects interacting with many other objects. I think this is the option you need for the scenario you describe.

Remember that you can vastly reduce your conditional logic in collisions by using collision filters. That will automatically make certain object types ignore collisions with other object types, so you don’t need to handle it with if-then statements.

http://forums.coronalabs.com/topic/2128-collision-filters-helper-chart/

Hope this helps,

Brent Sorrentino

Hi Brent !

Thank you for your answer ! I feel reassured since i used the second solution ! And I even used the Collision filters too, but I’ll keep your link, I’ll probably need it later.

Thanks again for your help !

(Oops… and I just noticed I started this post from my wrong account… sorry about that !)

Hi @evans.randria,

In general, there are two approaches to collisions:

  1. You can apply collision detection to one or more objects individually, and then sense collision with “other” objects in the world. This approach is logical if you have, for example, one “player” character and you want to sense collisions on that player against basically anything else in the world… enemies, walls, bullets, etc. This method is very efficient when you’re dealing with that kind of scenario, because only the player needs to have a collision listener. The other objects don’t necessarily need to have collision listeners, unless they require their own separate collision detection with objects that are not the player.

  2. You can use a Runtime collision detection setup where essentially every object in the world senses collision with anything else, and in return (from the collision event) you’ll get the IDs of both objects involved in the collision. This scenario is logical when you have a ton of objects interacting with many other objects. I think this is the option you need for the scenario you describe.

Remember that you can vastly reduce your conditional logic in collisions by using collision filters. That will automatically make certain object types ignore collisions with other object types, so you don’t need to handle it with if-then statements.

http://forums.coronalabs.com/topic/2128-collision-filters-helper-chart/

Hope this helps,

Brent Sorrentino

Hi Brent !

Thank you for your answer ! I feel reassured since i used the second solution ! And I even used the Collision filters too, but I’ll keep your link, I’ll probably need it later.

Thanks again for your help !

(Oops… and I just noticed I started this post from my wrong account… sorry about that !)