Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by hlw · Sep 27, 2017 at 03:41 PM · 2d gamepolygon collider 2d

Detect for every side of a polygon which way it is facing.

Example polygon image

  1. The polygon is a polygonCollider2D.

  2. Using Triangulator.cs from the unify wiki, it creates a mesh and populates it with triangles.

  3. Each point is a gameobject with a circleCollider.

  4. Each side of the polygon is a line renderer with a boxCollider attached to it (with the right sizes), it has references to the points it start and end with.

  5. The gameObject holding all of this 's position always is (0,0,0).

I would like to know the direction each line is facing (performance isn't an issue) of a polygon like the one in the example picture (to add for example grass all around the polygon that faces the direction outside).

I thought i could use simple raycasts, but i can't as lines sometimes intersect or even are entirely inside the polygon itself and would then instantly hit the polygon whichever the "outside" direction is.

How can I acheive such a thing?

example.png (172.2 kB)
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
1
Best Answer

Answer by JonPQ · Sep 27, 2017 at 04:04 PM

Your example is a little strange... as you have lines that cross over each other... which sort of makes what you are asking impossible, because some areas would both need grass and not have grass, depending on which lines you are choosing..... but there are always ways... For example, how is the area filled... looks like the opposite of that would be where you want the grass ? Draw grass everywhere, then draw this on top.

The usual way to check if a point is inside geometry... is to "test if a point is on one or other side of a line".... use cross product of two vectors... e.g for a triangle... (a nice closed polygon) take 1 edge (vector3) take another point in space, make a vector from there to, first point on your triangle edge, then do a cross product which gives you 3rd vector output... check its Z (+ve or -ve) to see if the point is on inside or outside of the line. If you do that with all edges of triangle... and they are all inside.... then the point is inside the triangle (works for any closed, non concave polygon)
Vector3 outVEc = Vector3.Cross( triangleEdgeVec, vecToPoint); Note:- you have to be consistent with points around triangle in same order (clockwise or anti-clock)

another option... if you only need grass along the edge of the line... and if the lines are all generated in a consistent ordering of vertices... say clockwise... you can trivially generate a vector at 90 degrees to the edge, that is always pointing out.... given vector = x,y. 90 degree vector = (-y , x) or *-1 to get vector pointing opposite (in rather than out) You could then make quads along the outer edge, that you put grass material on.

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 hlw · Sep 27, 2017 at 05:42 PM 0
Share

The points composing the polygon are sorted in a list, the first point draw a line ending at the second point, and so on, until the very last point draws a line ending at the first point. (Grass was just an example, if fact i will use the line to create an 8 vertices mesh (it has a starting material, an ending material, and a middle repeating material), i really can't draw that everywhere before putting the polygon on top.)

Actually, when lines cross each other, it must have grass, looking like "two mountains, one in front of the other"(I'm pretty sure this would look ugly, but i got to make sure any polygon will work as it is a map editor designed to be used by players).

The area is filled by turning the collider into a mesh and creating triangles, (actually, i can still access to the position of the 3 points composing each triangle). Every Line (edge of the polygon) is in fact one of the 3 edge of one and only one existing triangle.

I guess by creating a line perpendicular to and crossing by the middle the said edge of the triangle , i can find a point out of the triangle using your method, and use that line to find the exact rotation needed for my "grass".

(About your second option, by putting a 90° degrees on the line, isn't there a one-in-two chance it gives me the inside direction ?

Thanks a lot for your answer, i didn't know where I had to look into. Should i accept this answer or does the answer need to be 100% straight to the answer for me to accept it?

avatar image JonPQ hlw · Sep 27, 2017 at 06:17 PM 0
Share

You are welcome. sup to you to decide to accept or not. re: 50% chance of 90 degree vector going either way. You might be right... I;d have to test to confirm.... Here is a better way than using (-y,x) though. Do Cross product of 2 vectors... your polygon edge, and a hard coded vector pointing out of the screen (or straight into the screen) e.g. in unity that would be Vector3.forward then your 90 degree vector result will be flat on the same plane as your line... and always correct direction (as long as your geometry is consistent clockwise or anti clockwise) Vector3 vec90 = Vector3.Cross( triangleEdge, Vector3.forward ); Note:- when you do cross product of 2 vectors... (that define a plane) the result is always at 90 degrees to that plane. very handy. its how normals are generated.

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.

Follow this Question

Answers Answers and Comments

75 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Polygon Collider 2d doesnt work properly 3 Answers

Two Polygon2D Collider do not Collide with Each Other (Solved) 2 Answers

Add Force if GameObject is detected in a Polygon Collider 2D 1 Answer

Can I use the Sprite Mode Polygon Editor to outline multiple sprites in one image atlas? Ifso, howso? 0 Answers

Is PolygonCollider2D a legit way to check for mouse clicks in irregular shaped sprites? 1 Answer


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