Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Shortest distance between point and Collider2d

Discussion in 'Physics' started by dotsquid, Mar 14, 2017.

  1. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    Hi everyone.
    I'd like to know if there is any way to find the shortest distance between a point and a Collider2d (of any shape)? Or, as alternative, a point on Collider2d closest to a given point.
    It seems there is no easy way to do that without some custom hardcore math. Which is probably not the case, because the solution must be fast as it's going to be called in real-time every frame.

    Funny (actually no) thing is that Box2d provides a useful b2Distance function which calculates the shortest distance between two shapes (or Collider2ds in Unity's terminology). But for some reason Unity does not expose such a useful utility.
     
  2. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    According to @MelvMay 's tweet it seems I just have to wait for 5.6.
    But I'll still be glad to here any solutions suitable for pre-5.6
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316
    Yes, it was indeed added in 5.6.0b7. It's now at a very late beta 5.6.0b11 and won't be long for final release.

    There are probably approximations you can do, certainly you can perform Collider2D.Cast to check where it would contact but that won't necessarily give you closest point, just first contact in a specific direction. There are probably other approximations you could do using overlapCircle but it'll only ever be that beyond implementing your own distance function.
     
  4. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    Hi, thanks for reply.

    Yeah, I've considered the options of approximated solutions, but unfortunately I need a precise one. So I'd be waiting for encouraging 5.6.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316
    Why wait, beta has been available to test for a long time!
     
  6. anant32tech

    anant32tech

    Joined:
    Apr 21, 2017
    Posts:
    4
    @MelvMay : I'm using Unity 5.5f. Are the functions "Collider2d.bounds.ClosestPoint()" and "Collider2d.bounds.SqrDistance()" not meant to address the shortest distance problem between a point and collider 2d ? They seem to work for the most part...but I was hoping to get your comments first before mentioning the issues with these functions :)
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316
    luchong12 likes this.
  8. anant32tech

    anant32tech

    Joined:
    Apr 21, 2017
    Posts:
    4
    Thanks for clarifying @MelvMay...AABB's new concept for me...guess we learn something new every time we talk to experts :)
     
  9. Nixaan

    Nixaan

    Joined:
    May 30, 2013
    Posts:
    118
    @MelvMay is the 2D version of ClosestPoint on the horizon?
     
  10. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    I believe you can use Physics2D.Distance / Collider2D.Distance / Rigidbody2D.Distance to get the closest point. These methods return ColliderDistance2D which has pointA and pointB variables. Exactly what you need.
     
  11. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    Are we sure about that? How would we know what it does internally? I too need the Collider2D.ClosestPoint method for something I'm doing right now, but it doesn't appear to be in yet...
     
  12. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    Usually in math by distance between lines, planes, convex hulls etc the minimal distance is meant.
    Anyway, https://twitter.com/melvmay/status/826465145450852352
     
  13. mr_president

    mr_president

    Joined:
    Jun 28, 2015
    Posts:
    23
    Was the equivalent of a Collider2D.ClosestPoint ever added? I don't believe Physics2D/Collider2D.Distance will work for me since I specifically need to check between a point and a collider, not between two colliders.
     
    Menion-Leah likes this.
  14. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    950
    kodo91 and shottogan like this.
  15. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    168
    Could this please be added to Unity?
    Wolfos's method works, but it requires creating a new GameObject every time a distance is calculated.
     
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316
    It has already been added to Unity in a future version (2019.1)
     
    chanon81 likes this.
  17. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    168
    That's wonderful! Thanks!
     
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316