Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How does ContactFilter2D work??

Discussion in 'Physics' started by lejean, May 11, 2017.

  1. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    200
    https://docs.unity3d.com/560/Documentation/ScriptReference/ContactFilter2D.html

    I don't understand this at all.
    I'm trying to use the getcontacts/overlapbox or the overlapcollider and I have no clue what they want me to provide.

    Let's say I have 3 overlapping boxes, how do I use any of the above to get a result with all the overlapping boxes?
    If anyone has a code example it would be most appreciated.

    Thx
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,825
    It's basically a way to avoid using OnEnter or OnStay calls because you can gather that data yourself instead. For example I use it to create my own physics based character controller.

    Is there a particular reason you need to filter and deal with lower level collision data? What is your actual issue, seeing as you don't seem to know why you are using this feature.

    The documentation explains how it can be used with everything. For example you want to gather some contacts from a rigidbody with collider potentially touching something? Use
    int count = rb.GetContacts(contactFilter, contacts);

    And then loop through the results. But really, explain your actual problem, there's probably much better ways.
     
  3. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    200
    I just need the best way to see which other 2d boxes are overlapping with a given box at any point in time.

    I was using triggerenter to add the detected object to a list, but then I saw those functions.
    It seemed like the better way to go about it, but I can't get it working to test if it's actually more useful or not.
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,825
  5. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    200
    K, I think I figured it out mate.
    I just didn't know what I needed to provide for (contactFilter, contacts).

    When I made an empty array for contacts it just didn't work or returned 0.
    But after I did contactFilter.nofilter() it started debugging the amount of boxes that were overlapping.

    Thanks for your help anyway, much appreciated.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,012
    It clearly says in the documentation that you need to provide an array that has the capacity to return results; arrays cannot be resized. If you pass an empty array you'll get zero results. Try giving the docs a read.

    For GetContacts to work, there has to be contacts available i.e. the colliders in question are to set contact each other. If you look at the docs you'll see you do not even have to pass the ContactFilter2D. There's also a GetContacts that only returns the Collider2D rather than all the ContactPoint2D which is wasted time if you don't need them.

    You can also check to see if a specific collider is overlapping (ignoring any current contacts) by using Collider2D.OverlapCollider.
     
  7. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    200
    I didn't say I didn't give it a size.
    The solution was I had to say nofilter() cause just declaring an empty contactfilter caused it to still filter the results.

    And I don't have to give a contactfilter for getcontacts(), but I do need to give one for OverlapCollider(), even if I don't want anything filtered.

    Obviously I don't need these functions if I don't want to filter anything, but I was just trying to make it log the correct results.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,012
    But you did which is why I pointed you to the docs when you said:
    An empty array is zero size so no results can be returned.

    Yes, a default C# struct sets fields to their defaults which isn't a problem apart from the property "useTriggers" which defaults to false therefore by default, a ContactFilter2D will filter out triggers. It won't perform any other type of filtering however.
     
  9. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    200
    I guess I worded it badly, I just meant declare an empty array like
    Code (CSharp):
    1. Collider2D[] collidersArray = new Collider2D[5];
    It returned 0 because of the default ContactFilter2D.

    Anyway I understand how it works now, thanks for all the help.
     
    MelvMay likes this.
  10. b1naryatr0phy

    b1naryatr0phy

    Joined:
    Mar 28, 2015
    Posts:
    4
    Wow...you wake up on the wrong side of the bed or are you always this passive aggressive with your end-users?
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,012
    Because I used the world clearly? Or because you read it in an aggressive voice perhaps?

    Also, is this your only input to this thread?
     
    Last edited: Jun 14, 2017
    hippocoder likes this.
  12. DevDeedSquire

    DevDeedSquire

    Joined:
    Nov 14, 2017
    Posts:
    4
    Hi Le
    Hi Lejean, I had some issue with the wording as well (whats with declaring it an INT to begin with???)

    its a bit of a brute force approach but I declared the "contactFilter" as a public variable then you can mess around with the settings in the unity editor,

    this is an example how I've used it in my code (work in progress)

    targetChecker.OverlapCollider(targetFilterLeft, targetsInRangeLeft);

    for (int i = 0; i < targetsInRangeLeft.Length; i++)
    {
    if (targetsInRangeLeft != null)
    {
    Debug.Log("Left Position= " + targetsInRangeLeft.transform.position + " Left Target= " + targetsInRangeLeft.name);
    System.Array.Clear(targetsInRangeLeft, i, targetsInRangeLeft.Length);
    }
    }

    upload_2017-11-22_6-28-40.png

    As you can see in the picture above I've got ready access to the various settings, these restrict what gets added to the array in the first place, i assume the boxes have a collider2D attached to them.

    @MelvMay the bold font in the first sentence made it seem passive aggressive, plus the fact that the manual is not massively informative as you seem to think, more like a size spec without the measurement type being stated(see tesco website)
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,012
    Sorry, I'm confused; what has the manual got to do with this post? I was discussing a very specific API reference (https://docs.unity3d.com/ScriptReference/Collider2D.GetContacts.html) and not the manual. I do not understand your Tesco/size reference either, sorry. Kind of wondering why you felt the need to mention me in a post from June too.

    This is the content that is used for all API calls that accept an array to populate which I emphasized was clearly stated, not aggressive in any way although almost anything can be read that way if wanted. Text sucks sometimes.
     
    Last edited: Nov 22, 2017
  14. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,825
    Smart people don't have time for wasting time on things like passive/aggressive. Admittedly this can come across being cold or give regular people the wrong impression. Don't expect it by default on this forum to be the case :)

    Any further questioning of the developers wording will have the post removed and the reply banned, as it's clearly not in the interests of solving problems in the thread and off topic. If you have an actual problem with how someone uses the English language in text, use the PM system or the report system.
     
  15. DevDeedSquire

    DevDeedSquire

    Joined:
    Nov 14, 2017
    Posts:
    4
    Not questioning, also wrote my post in a hurry this morning, sorry
    Was just saying I also read it as passive aggressive and MelvMay seemed unaware as to why anyone would see it as such, glad its not :) love Unity and have had great interactions with the staff and enjoy the great materials you put out.
    I meant the API sorry Melv (if I can call you that? :p) was just in a rush so didn't check it properly. It is useful for giving details of the item but as someone who isn't very clued up to exactly how Unity operates it isn't particularly useful for working out how to use it.
    Again not questioning just felt the need to clarify b1nary wasn't on his own reading the wrong intent of the emphasis :)