GetContacts() 2d should be this simple - why isn't it?

        Collider2D grab = GrabHitbox.GetComponent<Collider2D>();
        ContactFilter2D filter = new ContactFilter2D();
        filter.useTriggers = true;
        filter.useLayerMask = true;
        filter.layerMask = LayerMask.NameToLayer("Playspace (Center)");      
       
        ContactPoint2D[] contacts = new ContactPoint2D[10];
        int contactCount = grab.GetContacts(filter, contacts);

        for ( int i = 0; i < contactCount; i++ )
        {
            var contact = contacts[i];
            if ( contact.otherCollider.tag == "CanGrab" )
                Debug.LogFormat("{0} can be grabbed.", contact.otherCollider.name );
        }

        return false;
    }

I’ve confirmed both the Grab collider and the target are both PolygonCollider2D - and are triggers… why doesn’t this return any contacts?

1 Like

It doesn’t move relative to the rigid body at all.

(This was relevant, because the Contacts array is cleared anytime the Collider is moved relative to the rigidbody)

It is simple but triggers do not produce contact points and they would never be used anyway. Use the overload of “GetContacts()” that returns Collider2D only which will include contacts with triggers. From your code, you should be using that anyway being as you don’t seem interested in them and formatting them to be returned to you costs which is wasteful if you’re not using them.

I have no idea what that means.

Also, this isn’t an experimental feature, it’s in 5.6 so please post on the general 2D or Physics forums from now on, thanks.

Oh, okay. I didn’t expect that GetContacts() would return 0 for one overload and > 0 for another. :x

Trying now

Okay. That worked!

1 Like

An overload is just a different function with the same name so it shouldn’t be unexpected. It’s why OnTrigger callbacks return the other collider only whereas OnCollision callbacks return a type that provides contact details etc.

I don’t want to spend a lot of energy defending my thickheadedness… but do you think that the discoverability of an extra version of GetContacts beats having something clearly named like GetIntersectingColliders() ?

A Contact is not a ContactPoint2D which I believe the assumption you’re operating under. A contact in physics is when two things touch/overlap. Unity uses the terminology of Triggers and Collisions which are treated differently however a contact is both of them.

“GetIntersectingColliders()” is just a verbose way of saying “GetContacts”. “GetContacts” does not mean “GetContactPoints”.

The documentation separates them all however the one that returns ContactPoint2D should state that this won’t include triggers as they do not have contact points.

1 Like

Yeah, the terminology confused me. I was under the impression Contact == ContactPoint2D

1 Like

I have updated the API docs to include a section clearly stating that ContactPoint2D will not be returned for contacts involving a trigger. I’ve added that to all the GetContacts docs and the ContactPoint2D docs as well.

Those changes won’t appear immediately online, they go through the normal process for changes so will take a little while.

2 Likes

Thank you. <3

1 Like