Collision detection priority

I have many objects in my game that each have a collision event.
When 2 objects that have their own collision events happen to collide with each other, only 1 of the onCollision functions are called.
Is there any way to force an item to be the primary object that will execute collision code when objects collide with it? I don’t want to put a bunch of collsion checks all over the place.

If not, is there a recommended way to do this?

thanks [import]uid: 114363 topic_id: 29054 reply_id: 329054[/import]

Hi there,

I believe the onCollision events will fire in the order in which the physics objects were created.

That said, I’d like to understand better what the problem is you’re looking to solve. If, for example, you’d like certain behavior to be fired for each object that has a collision event when the collision occurs, when the collision event is fired for whichever object is primary, you could simply fire that behavior for that object, and also for the [lua]event.other[/lua] parameter in the collision. Would that help your issue?

  • Andrew [import]uid: 109711 topic_id: 29054 reply_id: 116933[/import]

Thanks for the reply Andrew…

I have a ball on the screen and when it collides with anything, it should “win”. I wanted the collision function of the ball to handle everything it collides with it. I do use event.other to determine what the ball collided with.

All other physics objects have their own collision events that are fired when objects touch them (but not the ball). I use event.other here to determine what the object collided with.

What is strange is when 1 object type collides with the ball, the ball’s onCollision function is called. When another object type collides with the ball, the object’s onCollision is called (not the ball’s function). I can’t seem to figure out why, since I create the ball object first, and then create other object’s in a single function as they are needed.

I guess what I am looking to do is to have the ball’s onCollision function handle any collision between the ball and anything else. Also, if 2 physics object collide, how do I determine which object’s collision function is called?

I am using separate collision functions for each object type… would it be easier to have 1 onCollision function that handles every combination of object collision? Is there a preferred way to code all of these collisions?

Thanks again!
–john [import]uid: 114363 topic_id: 29054 reply_id: 116946[/import]

Hi John,

Thanks, I understand better now what you’re trying to do.

Yes, that does sound strange about the order in which the collision events are called. Some experimenting I did with a project of mine led me to think it was based on when the physics objects were created (or perhaps, when the collision listeners are added), but in your case it seems not to be true.

In any case, I would agree with your suggestion of using a single collision function to serve as the listener for all of the object types, rather than a different function for each type. Within the single function, you would then just execute different behavior depending on the types of objects involved in the collision. (You would have to store the type of the object as a property of the object when it’s created, so that you can refer to it in the collision function.)

  • Andrew [import]uid: 109711 topic_id: 29054 reply_id: 116950[/import]

Hi @schizoid2k,

As a rule of thumb, for both performance and prevention of unpredictable collisions, you should always minimize your collision listeners. If you have one ball, in your case, and that is the primary (only?) thing you need to actually get a returned reaction from, then only apply a listener to the ball and use “event.other” for whatever it collides with.

Let’s assume (in addition to the ball) that you have 100 “cubes” floating around that might or might not collide with each other. Unless you need a “cube against cube” reaction (i.e. they destroy each other, or trigger some event) you do NOT need listeners on them… only on the ball.

Not only are redundant collision listeners bad for performance, but they might also cause problems if more than one occurs on an object in the same game cycle.

Going with just 1 listener should also eliminate your issue with the “order” of objects colliding.

Best regards,
Brent [import]uid: 9747 topic_id: 29054 reply_id: 116959[/import]