Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Collision - retrieving number of contact points

Discussion in 'Scripting' started by fabi_s, Dec 6, 2021.

  1. fabi_s

    fabi_s

    Joined:
    Jan 25, 2021
    Posts:
    10
    Hello people,

    I am trying to retrieve the number of contact points during a collision event but I am not sure I am using the collision.contacts correctly. I always get 4 contact points in any position I place my objects. My assumption (see pic) is that the number of contact points should increase if the position of the objects change....


    Intersections.png

    Code (CSharp):
    1. void OnCollisionStay(Collision collision)
    2.     {
    3.         //Detect if block has moved
    4.         currentPos = transform.position;
    5.         currentRot = transform.rotation;
    6.         if (currentPos != lastPos || currentRot != lastRot)
    7.         {
    8.             verticesList.Clear();
    9.             lastPos = currentPos;
    10.         }
    11.         //Debug.Log(collision.contacts.Length);
    12.         foreach (ContactPoint contact in collision.contacts)
    13.         {
    14.             verticesList.Add(contact.point);
    15.         }
    16.         Debug.Log(collision.contacts.Length);
    17.     }
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Why are you assuming the corners of an object would produce another contact point? That doesn't seem right to me.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    That's the wrong part.

    Collision points are not some set of points used purely as a geometric test utility might return them; each has utility when retrieving them from a physics engine. That utility is the points at which impulses/forces are applied to keep physics shapes separated.

    For instance, in 2D physics a Circle contacting any other kind of shape only ever produces a single contact point as that is the point an impulse is applied to keep it separated. For polygon/polygon it's only ever two. In Unity a "Collider" can be composed of multiple physics shapes so you can get more than what is described here. For instance a PolygonCollider2D produces unlimited physics shapes from its outline but each only returns a maximum of two contact points.

    I'm not too familiar with the limits on 3D but something similar will still apply. They are created by the physics engine for itself to keep physics shapes separated and denote places where impulses are applied. You can read them but they're not created for any other purpose.
     
    fabi_s likes this.
  4. fabi_s

    fabi_s

    Joined:
    Jan 25, 2021
    Posts:
    10
    Thanks for the explanation. So if I get it right, 'ContactPoints' are not geometry references but they represent parameters for the physics engine. So what would be the best approach to visualise and calculate the overlap area of two objects? My approach was to determine the contact points and create a mesh from that intersection but this seems not to be the case any longer....
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    Yes, absolutely this. You as a game developer are demoted to being a spectator at that point (pun intended). :) This is why you'll often see devs saying points that start explosions are in the "wrong place" because it's not a point they'd "logically" expect.

    You'd need to use some kind of constructive solid geometry utility to calculate operations between these solids (OR, NOT) etc. There are a bunch of these floating around including on the Unity Asset Store, some free, some not but I'm not an expert on what utility a lot of these provide beyond world building itself.
     
    fabi_s likes this.
  6. fabi_s

    fabi_s

    Joined:
    Jan 25, 2021
    Posts:
    10
    Thanks a lot for the suggestions and clarification. I will follow up on this problem by searching for some other possibilities. Ciao!