- Home /
Detect for every side of a polygon which way it is facing.
The polygon is a polygonCollider2D.
Using Triangulator.cs from the unify wiki, it creates a mesh and populates it with triangles.
Each point is a gameobject with a circleCollider.
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.
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?
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.
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?
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.