• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ibyte · Jan 12, 2011 at 04:23 PM · 2dspritedetectionedge

How to detect the distance between two edges (2D sprites)

2D - orthographic camera setup.

In Unity I have created one sprite object which is a rectangle (320 x 480) and I have created several other sprites (128 x 128 each) arranged in a circle around the rectangle.

If I determined a line from the center of the rectangular sprite to the centre of one of the square sprites, how can I detect at which vector(s) the line crosses the edge of each sprite so I can then determine the distance between those two points.

Math is a four letter word to me :)

Help

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jesse Anders · Jan 12, 2011 at 04:44 PM

First, note that this isn't really a Unity question (as there's nothing Unity-specific about it). I'm sure you'll get some good answers here, but for this sort of question you might have as good or better luck on a general game development forum such as gamedev.net.

Anyway, computing the shortest distance between two axis-aligned rectangles isn't too difficult. There are really only two cases you have to consider: the first case is when each box lies fully within a 'corner' Voronoi region of the other box, and the second case is when they don't (in other words, the inverse of the first case).

Here's an example of the first case:

---- | | ----

   ----
   |  |
   ----

In this case, the closest points are the box corners corresponding to the regions in question, and the shortest distance is the distance between those two points.

Here's an example of the second case:

----
|  |  ----
|  |  |  |
----  |  |
      ----

In this case the shortest distance is the perpendicular distance between the closest edges (there is no unique pair of closest points).

The test itself then reduces to classifying one box in relation to the other to determine which Voronoi region(s) of the other box it intersects, and then computing the distance accordingly. The implementation can be a little involved due to the number of specific cases that have to be handled, but the algorithm itself is fairly straightforward.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ibyte · Jan 12, 2011 at 06:06 PM 0
Share

Hi Jesse, thanks for the reply. I agree the question is not Unity specific but certainly applicable to game development :) I updated the question slightly.

avatar image Jesse Anders · Jan 12, 2011 at 08:08 PM 0
Share

Ok, the updated question is a different question than the first one. What you have now is essentially a line intersection problem. How to find the intersection points between a linear component (line, ray, or segment) and an axis-aligned box is pretty well documented online. I can tell you that it's been discussed many times on GDNet, so if you search the forums there for e.g. 'ray aabb intersection' (or similar phrases), you should be able to find some good references and perhaps some example code.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Collision detection not working properly with 2D sprites 1 Answer

Bounds.extent relation to localScale 2D 0 Answers

2D Character getting a weapon. Changing it's sprites/animations 0 Answers

2d Spaceship editor? 0 Answers

In-Game 2D Sprite Building System 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges