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

General Question: "Manually" checking collisions

Discussion in 'Physics' started by plmx, Jul 3, 2016.

  1. plmx

    plmx

    Joined:
    Sep 10, 2015
    Posts:
    308
    Hi,

    I have two rather general question regarding collision checking. Both seem like common use cases to me so I am stumped as to why I can't seem to find the functionality for this so I suspect I am missing something or am going about this with the wrong mindset.

    The first question is that I have situations where I want to check, at a specific time, whether an arbitrary point is within an (arbitrary) gameObject (considering all of its colliders, which might not be simple box colliders, and might be rotated).

    I seem to be unable to find the functionality which allows me to check this, although Unity is clearly capable of performing such checks. I know I can do trigger checks with the OnTriggerXXX() methods; however, this means that I need to have two objects (instead of just one) and that the checking is performed continually although I really just need it in specific situations (and seldomly). Did I miss the API or is it just not there? If not, why not?

    The second question is a generalization of the first, namely I would like to know whether two game objects are either a) separate, b) overlap, or c) contained within another (again based on their colliders). Again, I seem to be unable to find the functionality which allows me to check this, and it seems I can't do this with triggers, too (because a trigger does not allow me to distinguish between b) and c) -- just between a) and b+c)).

    It would be very helpful if someone could clear this up for me :)

    Thanks!
     
    Last edited: Jul 3, 2016
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    I'll answer both questions at the same time for simplicity, but no, Unity does not expose those methods from PhysX to allow you to run those checks manually. You can either go down the rabbit hole and try to write them yourself, or you could use a third party Physics engine (someone here has Jitter up and running).

    If you don't care much about performance there are some pretty easy brute force methods of determining if a point is inside any type of collider. You essentially just take every face of the collider and check to see which side the point is on, and if it's "behind" the plane for every face then it is inside. (Note that not all mesh colliders actually have an "inside" and "outside")
     
    plmx likes this.
  3. plmx

    plmx

    Joined:
    Sep 10, 2015
    Posts:
    308
    Hi,

    thanks for your helpful answer!

    I can stop looking for these methods then. If someone from Unity is listening: Is there a problem in principle in exposing these methods? Can I request this feature somewhere? EDIT: Request is here: https://feedback.unity3d.com/suggestions/expose-methods-for-manual-collision-checking

    Looking at the faces seems like a viable alternative for me since I only need these checks from time to time, and they are not particularly time critical. Just for my understanding: To check for intersection of entire meshes (not just point-vs.-mesh), I'd have to check each vertex of one mesh against each triangle (triples in Mesh.triangles) of the other, right?

    Thanks!
     
    Last edited: Jul 22, 2016