1

I went through the documentation and I can find no way to achieve the simplest contact management using the built-in physics engine.

I have a situation where 2D collisions should be ignored when for example 2 objects are traveling in similar directions at similar speed (Basically when the distance of their velocity vectors subtraction is too short)...

These object are in layers which define the other types of objects they should collide with in normal circumstances. The number of objects intersecting (and not colliding) can increase arbitrarily. So changing layers is not a viable option.

Using regular box2d I would use the b2ContactListener.PreSolve to filter the objects based on their masks and additional parameters depending on the masks. What is the equivalent with Unity's 2D API?

9
  • 1
    Have you looked up the methods of OnCollision___2D and OnTrigger___2D?? You could change the collision layer for different directions and collisions will automatically happen.
    – Savlon
    Jul 24, 2014 at 7:48
  • @Savlon : To my understanding OnCollision2D is triggered after collision resolution... Am I wrong? If not, is there a way to cancel the result when it is triggered? I already use layers for 4 different types of objects and 4 different players. I have an arbitrary number of objects and unless My understanding of layers is wrong I can't see how what you suggest could be achieved with layers.
    – Coyote
    Jul 24, 2014 at 8:34
  • What happens if you disable the corresponding Rigidbody components in OnCollisionEnter2D()? Does it give the results you are looking for?
    – S.C.
    Jul 24, 2014 at 9:19
  • @AldourCheng rigidbody2D.active = false makes objects simply inactive. coll.rigidbody.collisionDetectionMode = CollisionDetectionMode2D.None produces no visible effect. If you meant deactivating the colliders then no. Because then the objects with disabled colliders will not collide with other mutually excluded objects moving the other way, or walls, or other objects they should hit. Is there a way to cancel a collision in OnCollisionEnter2D?
    – Coyote
    Jul 24, 2014 at 9:45
  • @Coyote Do you mean you want to temporarily disable the object from being affected by the built in physics engine? According to the manual, Rigidbody is responsible for this. So what if you take it out from physics control?
    – S.C.
    Jul 24, 2014 at 10:08

1 Answer 1

2

The only way I'm aware off to change collision behavior during runtime, is to disable collisions between two layers or between two colliders.

If your objects are already on different layers, you can disable (and enable) collisions between them via Physics2D.IgnoreLayerCollision().

If you don't want to disable whole layers, you can specify two colliders that should ignore each other with Physics2D.IgnoreCollision()

Since there is no method called before the collision happens, you either need to check yourself which objects could collide and enable or disable the collision or use OnCollisionEnter2D() and disable the collision afterwards. For the second way you would need to store the velocities before the collision and set them again after the collision happened.

To add some kind of pre-check, you can add a larger second collider around the object and set it as trigger. During OnTriggerEnter2D() you can check if the actual collision should happen.

5
  • So far I use Physics2D.IgnoreCollision(). But some objects have multiple colliders and the process even if automated is not very interesting as then I have to add special triggers to check if the objects are currently ignoring each other if yes remove them once they crossed... and then... How do you un-ignore collisions?
    – Coyote
    Jul 25, 2014 at 10:00
  • @Coyote to re-enable collisions use the third parameter: IgnoreCollision(collider1, collider2, false) Jul 25, 2014 at 10:05
  • This will not work either. When objects collide putting the velocity back after the collision will also cancel the changes in velocity which happened with other objects, and the positions are also wrong.
    – Coyote
    Aug 7, 2014 at 9:15
  • @Coyote if they are wrong, you obviously set them wrong. As already stated in the answer, you need to save their values before the collision happens. Maybe it's easier to write your own collision behavior and just use the trigger function provided by Unity. Without knowing more about your project, it's impossible to say what kind of approach might be the best. Aug 7, 2014 at 17:56
  • When other components add forces and update velocity then the cached values are not updated. Basically I have to make all other components aware of the caching system for this workaround to start working... Objects which don't require the workaround keep working as intended, objects with the workaround and without other components/objects changing their forces are not behaving properly. I'm still baffled by the absence of a pre-solver callback in unity...
    – Coyote
    Aug 22, 2014 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.